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 <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 fileno; } 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);
fd_set *streams; FD_ZERO(streams);
// array of current client connections // - this may be replaced by blocks pointed to by select()'s fd array streamInfo* streams[MAXCLIENTS];
int i, stream, nStreams = 0;
// TODO: change to quantum-iteration model
// - using select()
// - server is in is in nodal ROOT loop
// - concurrent connections are stored and multiplexed in serverIterate loop
void network(node stream) {
FD_ZERO(streams); // why here?
// 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"); }



