Results 1 to 1 of 1
Hi. I have recently start programming at socket layer and I am trying to implement a simple program which allow server and client to exchance messages. I am using the ...
- 03-19-2009 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 1
Server/Client Asynchronous communication
Hi. I have recently start programming at socket layer and I am trying to implement a simple program which allow server and client to exchance messages. I am using the select() system call to monitor the keyboard and the socket but I am having some trouble: basically if I start sending a message from the server to the client the client will receive the message and display it but when I type something from the client to the server the message is not displayed; if I start the code again and this time I start sending a message from the client to the server the server will receive the message and display it but when I type something from the server to the client the message is not displayed. Please find here attached a synthetized version of my code (server.c - client.c code is similar). The socket seems to be stuck in one operation only (either reading or writing). Would it be possible, for example, to reset the socket???
Kind regards
Code:#include <sys/types.h> #include <sys/time.h> #include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> #include <signal.h> #include "msgFormat.h" #include "protNet.h" #include "net.h" #include "error.h" #include "memAlloc.h" #include "missingProtos.h" #define STREAMS_QUEUED 5 /* no. of streams to be queued */ #define KEYBOARD 0 /* signal handler for SIGPIPE. In case that the client suddenly dies on us */ static void clientDied(void) { appError("Server found a break in communication, bye..."); } /* reads a c-string from the keyboard, then assembles a message in m */ static int getUserMessage(Message *m) { char buf[BUFSIZ]; if (fgets(buf, BUFSIZ, stdin) == 0) return 0; m->dataLength = strlen(buf) + 1; if ((m->data = memAlloc(m->dataLength)) == 0) sysError("Server: could not get memory for message"); strcpy(m->data, buf); return 1; } /* reads messages from sd, then displays these until end of input */ static void displayMessages(int sd) { int r; /* return code */ Message m; /* where messages will be kept */ /* reads a c-string and displays it */ while ((r = receiveMessage(sd, &m)) != 0) { if (r == -1) sysError("Server could not read message"); printf("%s", m.data); memFree(m.data); } } /* Usage: server localPort */ int main(int ac, char *av[]) { u_short port; /* local port */ Message m; /* to store messages */ int r; /* return code */ int listenSd; /* listening socket descriptor */ int streamSd; /* stream socket descriptor */ int maxSds; /* maximum socket descriptor */ Id other; /* who has connected, not used */ fd_set readSds; /* descriptors waiting for input */ if (ac < 2) appError("Usage: server localPort"); port = atoi(av[1]); /* get port and turn to integer */ signal(SIGPIPE, clientDied); if ((listenSd = getListeningSocket(port, STREAMS_QUEUED)) == -1) sysError("Server could not obtain a listening socket"); if ((streamSd = acceptByteStreamSocket(listenSd, &other)) == -1) sysError("Server could not accept a stream"); /* listening socket is no longer required so it can be discarded */ close(listenSd); fprintf(stderr, "Connection established send messages\n\n"); maxSds = (KEYBOARD > streamSd) ? KEYBOARD : streamSd; /* get the max descriptor */ for (;;) { FD_ZERO(&readSds); /* clear bit mask */ FD_SET(KEYBOARD, &readSds); /* monitor activity on keyboard */ FD_SET(streamSd, &readSds); /* monitor activity on streamSd */ /* poll for input activity on keyboard and streamSd only */ r = select(maxSds + 1, &readSds, NULL, NULL, NULL); if (r > 0) { if (FD_ISSET(KEYBOARD, &readSds)) { /* is the keyboard active? */ fprintf(stderr, "\nKEYBOARD\n"); while (getUserMessage(&m) != 0) { if (sendMessage(streamSd, &m) == -1) sysError("Client: could not send a message"); memFree(m.data); } } if (FD_ISSET(streamSd, &readSds)) { /* is the socket streamSd active? */ fprintf(stderr, "\nSOCKET\n"); displayMessages(streamSd); } } if (r == -1) fprintf(stderr, "\n\nSelect failed"); if (r == 0) /* server closed the socket */ break; } close(streamSd); return 0; }


Reply With Quote