Difference between revisions of "Server.c"
(bit more done - not ready to compile yet) |
m |
||
| Line 5: | Line 5: | ||
#define LISTENPORT 2012 | #define LISTENPORT 2012 | ||
#define BACKLOG 10 | #define BACKLOG 10 | ||
| − | #define | + | #define PAKSIZE 128 // keep packet-size small for non-multithreaded design |
| + | #define BUFSIZE 10000 // max message size | ||
#define MAXCLIENTS 100 | #define MAXCLIENTS 100 | ||
#define MSG "stink ow!" | #define MSG "stink ow!" | ||
| Line 23: | Line 24: | ||
int processMessage(char* msg); | int processMessage(char* msg); | ||
| − | int | + | |
| + | // 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; | struct sockaddr_in my_addr, client_addr; | ||
int sa_in_size = sizeof(struct sockaddr_in); | int sa_in_size = sizeof(struct sockaddr_in); | ||
| − | char | + | 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 | // get a socket | ||
| + | int server; | ||
if ((server = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { | if ((server = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { | ||
perror("socket"); | perror("socket"); | ||
exit(1); | exit(1); | ||
} | } | ||
| − | |||
| − | |||
| − | |||
// make it reusable | // make it reusable | ||
| + | int sockopt_on = 1; | ||
if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &sockopt_on, sizeof(int)) < 0) { | if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &sockopt_on, sizeof(int)) < 0) { | ||
perror("setsockopt"); | perror("setsockopt"); | ||
exit(1); | exit(1); | ||
} | } | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
// bind our socket to the port | // bind our socket to the port | ||
| Line 63: | Line 65: | ||
} | } | ||
| − | // | + | // 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 | |
| − | + | fcntl(server, O_NONBLOCK); | |
| − | |||
// array of current client connections | // array of current client connections | ||
streamInfo* streams[MAXCLIENTS]; | streamInfo* streams[MAXCLIENTS]; | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| + | int i, stream, nStreams = 0; | ||
while(1) { | while(1) { | ||
// Wait for any incomming connection | // Wait for any incomming connection | ||
// O_NONBLOCK is set so, EAGAIN or EWOULDBLOCK returned if no requests | // 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 connection requests | ||
| + | } | ||
| + | else if (stream < 0) { | ||
perror("accept"); | perror("accept"); | ||
exit(1); | exit(1); | ||
| + | } | ||
| + | else { | ||
| + | // New stream, create input buffer etc | ||
| + | streamInfo si; | ||
| + | si.buf = malloc(BUFSIZE); | ||
| + | si.fileno = stream; | ||
| + | streams[nStreams++] = &si; | ||
} | } | ||
// loop thru streams and read a packet from any with data to read | // loop thru streams and read a packet from any with data to read | ||
for (i = 0; i < nStreams; i++) { | for (i = 0; i < nStreams; i++) { | ||
| − | + | char* buf = streams[i].buf; | |
| + | int stream = streams[i].fileno; | ||
} | } | ||
Revision as of 02:54, 3 July 2006
// good sock function ref at // http://www.opengroup.org/onlinepubs/009695399/idx/networking.html
// Set up socket and listening loop
- define LISTENPORT 2012
- define BACKLOG 10
- define PAKSIZE 128 // keep packet-size small for non-multithreaded design
- define BUFSIZE 10000 // max message size
- define MAXCLIENTS 100
- define MSG "stink ow!"
// - 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 <netinet/in.h>
- include <fcntl.h>
//#include <arpa/inet.h> //#include <netdb.h> //#include <unistd.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) { perror("socket"); exit(1); }
// make it reusable int sockopt_on = 1; if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &sockopt_on, sizeof(int)) < 0) { perror("setsockopt"); exit(1); }
// bind our socket to the port if (bind(server, (struct sockaddr *)&my_addr, sa_in_size) < 0) { perror("bind"); exit(1); }
// start listening for incoming connections if (listen(server, BACKLOG) < 0) { perror("listen"); exit(1); }
// 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 fcntl(server, O_NONBLOCK);
// array of current client connections streamInfo* streams[MAXCLIENTS];
int i, stream, nStreams = 0; while(1) {
// 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 connection requests } else if (stream < 0) { perror("accept"); exit(1); } else { // New stream, create input buffer etc streamInfo si; si.buf = malloc(BUFSIZE); si.fileno = stream; streams[nStreams++] = &si; }
// loop thru streams and read a packet from any with data to read for (i = 0; i < nStreams; i++) { char* buf = streams[i].buf; int stream = streams[i].fileno; }
// 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);
close(stream); // should close when no more data on this stream (somehow)
}
// 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) { perror("send"); } else printf("Sent message MSG\n"); }



