Server.c
// good sock function ref at // http://www.opengroup.org/onlinepubs/009695399/idx/networking.html
// Set up socket and listening loop
- define LISTENPORT 2012
- define PAKSIZE 128 // keep packet-size small for non-multithreaded design
- define BUFSIZE 10000 // max message size
- define MAXCLIENTS 100
// - todo: don't exit on connect errors, keep trying every 10s // Includes for socket (trying to use one source cpp for osx,win32,*ux)
- ifdef WINDOWS
- include <winsock.h>
- else
- include <sys/socket.h>
- include <sys/select.h>
- include <netinet/in.h>
- include <fcntl.h> // needed for O_NONBLOCK option on server
- include <sys/time.h> // for select()
//#include <arpa/inet.h> //#include <netdb.h>
- endif
int processMessage(char* msg);
// struct type to represent a currently connected stream typedef struct streamstruct { char* buf; int fd; } streamInfo;
// set up socket struct struct sockaddr_in my_addr, client_addr; int sa_in_size = sizeof(struct sockaddr_in); memset((char *)&my_addr, 0, sa_in_size); // zero the struct my_addr.sin_family = PF_INET; my_addr.sin_port = htons(LISTENPORT); my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// get a socket int server; if ((server = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) logAdd("socket() failed!");
// make it reusable int sockopt_on = 1; if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &sockopt_on, sizeof(int)) < 0) logAdd("setsockopt() failed!");
// bind our socket to the port if (bind(server, (struct sockaddr *)&my_addr, sa_in_size) < 0) logAdd("bind() failed!";
// start listening for incoming connections if (listen(server, MAXCLIENTS) < 0) logAdd("listen() failed!";
// make the server non-blocking so accept() returns straight away // - if no incoming requests, returns EAGAIN or EWOULDBLOCK state // - we need this for non-multithreaded design // - this may need to be done for each new incomming stream...? // - some examples use fcntl(server, F_SETFL, FNDELAY) or fcntl(server, F_SETFL, O_NONBLOCK); fcntl(server, O_NONBLOCK);
// Setup stuff for select() struct timeval timeout; fd_set *fdset;
// NOTE: CHANGE THIS TO USE NEW LINKED-LIST FUNCTIONS // array of current client connections // - this may be replaced by blocks pointed to by select()'s fd array streamInfo streams[MAXCLIENTS],si;
// - server is in is in nodal ROOT loop void network(node stream) {
int i, stream, nStreams = 0;
timeout.tv_sec = 1; // later use 0 for min time timeout.tv_usec = 0;
// Check for new connection FD_ZERO(fdset); FD_SET(server,fdset); if (select(maxfd+1, fdset, NULL, NULL, &timeout)>0) { // new connection }
// Data to receive? FD_ZERO(fdset); i = 0; while(si = streams[i++]) FD_SET(si.fd,fdset); if (select(maxfd+1, streams, NULL, NULL, &timeout)>0) { // read packet from each in fdset }
// Data to send? FD_ZERO(fdset); i = 0; while(si = streams[i++]) FD_SET(si.fd,fdset); if (select(maxfd+1, NULL, streams, NULL, &timeout)>0) { // write packet from each in fdset }
// ****** old loop ******
// Wait for any incomming connection // O_NONBLOCK is set so, EAGAIN or EWOULDBLOCK returned if no requests stream = accept(server, (struct sockaddr *)&client_addr, &sa_in_size) if (stream == EAGAIN) { // No new connection requests - read any available data from streams for (i = 0; i < nStreams; i++) { char* buf = streams[i].buf; int stream = streams[i].fileno; } } else if (stream == ECONNABORTED) { logAdd("Stream closed by client"); close(stream); } else if (stream < 0) logAdd("accept() failed!"); else { // New stream, create input buffer etc streamInfo si; si.buf = malloc(BUFSIZE); si.fileno = stream; streams[nStreams++] = &si; }
// log the connecter
// - should get info for this stream, incl buf
printf("got connection from %s\n", inet_ntoa(client_addr.sin_addr));
// get the reply // - should keep receiving until \r\n\0?, then call serverProcessMessage() if (recv(stream, buf, bufsize, 0) == -1) perror("recv"); else printf("The client says \"%s\"\n", buf);
}
// Parses a message content and responds to client int processMessage(char* msg) { // test if restart cmd first
// send response if (send(stream, MSG, strlen(MSG)+1, 0) == -1) logAdd("send() failed!"); else logAdd("Sent message"); }



