// CLIENT
typedef struct parameters {
int choice;
int from;
int to;
int dimFile;
char buffer[100];
}parameters;
void clientFunctions(int socketfd){
parameters* dataParameters=(parameters*)malloc(sizeof(parameters));
while(1) {
printf("Inserisci 1 se vuoi conoscere la dimensione del file\nInserisci 2 per leggere da file\nInserisci 3 per scrivere su file\nInserisci 0 per uscire\nInserito: ");
dataParameters->choice=getInt();
while(dataParameters->choice<0 || dataParameters->choice>3){
printf("Valore fuori dal range!\nInserire di nuovo:");
dataParameters->choice=getInt();
}
write(socketfd,dataParameters,sizeof(dataParameters));
if(dataParameters->choice==0){
printf("Esco.\n");
free(dataParameters);
break;
}
switch (dataParameters->choice){
case 1: dimension(socketfd);
break;
case 2: readFile(socketfd);
break;
case 3: writeFile(socketfd);
break;
}
}
}
int dimension(int socketfd){
parameters* par=(parameters*)malloc(sizeof(parameters));
read(socketfd, par, sizeof(par)); //non funziona
int dim = par->dimFile;
printf("%d\n\n",dim);
free(num);
return dim;
}
int main(){
signal(SIGPIPE, SIG_IGN);
int socketfd;
struct sockaddr_in servaddr;
// socket create and varification
socketfd = socket(AF_INET, SOCK_STREAM, 0);
if (socketfd == -1) {
printf("Socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
//inet_aton("127.0.0.1", &servaddr.sin_addr);
servaddr.sin_addr.s_addr = inet_addr(IP_PORT);
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(socketfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) {
printf("Connection with the server failed...\n");
exit(0);
}
else
printf("Connected to the server..\n");
// function for chat
clientFunctions(socketfd);
// close the socket
close(socketfd);
}
// SERVER
typedef struct parameters {
int choice;
int from;
int to;
int dimFile;
char buffer[100];
}parameters;
typedef struct management{
int fd;
parameters *par;
}management;
void* DimThread(void* arg){
management *man = ((management *) arg);
man->par->dimFile = 10;
write(man->fd, man->par, sizeof(man->par)); //non funziona
return NULL;
}
void* mainThread(void* arg){
int connfd = *((int *)arg);
pthread_t threadDim, threadRead, threadWrite;
parameters* par=(parameters*)malloc(sizeof(parameters));
management *man = (management *)malloc(sizeof(management));
man->fd = connfd;
while(1){
read(connfd, par, sizeof(par));
man->par = par;
printf("The choice is: %d\n", par->choice);
if(par->choice==1){
pthread_create(&threadDim,NULL,DimThread,man);
pthread_join(threadDim,NULL);
}
if(par->choice==2){
pthread_create(&threadRead,NULL,ReadThread,man);
pthread_join(threadRead,NULL);
}
if(par->choice==3){
pthread_create(&threadWrite,NULL,WriteThread,man);
pthread_join(threadWrite,NULL);
}
if(par->choice==0){
free(par);
free(man);
free(arg);
break;
}
}
return NULL;
}
int CreateSocket(){
int socketfd;
struct sockaddr_in serveraddr;
socketfd=socket(AF_INET,SOCK_STREAM,0);
if (socketfd == -1) {
printf("Socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=INADDR_ANY;
serveraddr.sin_port=htons(PORT);
// errore=fcntl(sock,F_SETFL,O_NONBLOCK); server non bloccante
//Bind del socket
if ((bind(socketfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr))) != 0) {
printf("Socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(socketfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
return socketfd;
}
int main(){
signal(SIGPIPE, SIG_IGN);
int socketfd, connfd, lenght;
struct sockaddr_in client;
pthread_t threadMain;
socketfd = CreateSocket();
lenght = sizeof(client);
while(1){
// Accept the data packet from client and verification
connfd = accept(socketfd, (struct sockaddr*)&client, (socklen_t*)&lenght);
if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
int* val=(int*)malloc(sizeof(int));
*val=connfd;
pthread_create(&threadMain,NULL,mainThread,val);
pthread_detach(threadMain);
}
// After chatting close the socket
close(socketfd);
}
Ti ho segnto con "//non funziona" i punti in cui faccio read e write nel client e nel server.
In questo caso passo la struttura con all'interno un int = 10 ma il client stampa 0.