#define MAX_MSG_LEN 1000 #define ERROR -1 #include #include #include #include #include #include #include #include #include #include #include #include #include int newSd; struct sockaddr_in cliAddr, servAddr; int sd; int isaclient=0; /*Set to 1 when a client connects*/ int send_message (char msg[MAX_MSG_LEN]); void *get_messages (void *arg); void processcmd(char cmd[MAX_MSG_LEN]); /*======================================================================*/ /*======================================================================*/ int main (void) { pthread_t mThreadId ; int status ; /*Creates a socket*/ sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) { perror("cannot open socket "); return ERROR; } /* bind server port */ servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(9999); if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) { perror("cannot bind port "); return ERROR; } listen(sd,5); /*Create a thread for the message handeling*/ status = pthread_create(&mThreadId, NULL, get_messages, NULL) ; if (status != 0){ printf("pthread_create failed, error %d", status) ; } /*Starts waiting for the termination of the thread*/ status = pthread_join(mThreadId, NULL) ; if (status != 0) { printf("pthread_join failed, error %d", status) ; } return 0; } /*======================================================================*/ /*======================================================================*/ void *get_messages (void *arg) { /* get_messages keeps reading the standard input. It identifies commands and call procedure responding to these commands. */ /*--------------------------------------------------------------------*/ int cliLen; int i; static int k,inaline=0; int nrec; static char rcv_msg[MAX_MSG_LEN]; char cmd[MAX_MSG_LEN]; char msg[MAX_MSG_LEN]; /*Client waiting loop*/ while (1) { cliLen = sizeof(cliAddr); newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen); if(newSd<0) { perror("cannot accept connection "); return ERROR; } printf(" Connection opened by client\n"); isaclient=1; /*Message waiting loop*/ while(1) { nrec = recv(newSd, rcv_msg, MAX_MSG_LEN, 0); /* wait for data */ if (nrec<0) { perror(" cannot receive data "); return ERROR; } else if (nrec==0) { printf(" Connection closed by client\n"); isaclient=0; break; } processcmd(rcv_msg); } } } /*======================================================================*/ /*======================================================================*/ void processcmd(char cmd[MAX_MSG_LEN]) { /* get_messages keeps reading the standard input. It identifies commands and call procedure responding to these commands. */ /*--------------------------------------------------------------------*/ printf("%s\n",cmd); printf("Response:"); char msg[MAX_MSG_LEN]; fgets(msg,MAX_MSG_LEN,stdin); send_message(msg); return; } /*======================================================================*/ /*======================================================================*/ int send_message (char msg[MAX_MSG_LEN]){ /*--------------------------------------------------------------------*/ char msg2[MAX_MSG_LEN]; if (isaclient) { msg[strlen(msg)-1]='\0'; write(newSd,msg,strlen(msg)); } return 0; } /*======================================================================*/