#include /* obligatory includes */ #include #include #include #include #include #include #include #include #include "sockets.h" void do_something(int); int main(int argc, char **argv) { int s, t; int portnum; if (argc != 2) { printf("%s \n", argv[0]); return -1; } sscanf(argv[1], "%d", &portnum); /* call this once to start server running on port PORTNUM */ if ((s= establish(portnum)) < 0) { /* plug in the phone */ perror("establish"); return 1; } /* call get_connection for each client connection you want to accept */ if ((t= get_connection(s)) < 0) { /* get a connection */ if (errno != EINTR) { /* EINTR might happen on accept(), */ perror("accept"); /* bad */ return 1; } } do_something(t); return 0; } /* this is the function that plays with the socket. it will be called * after getting a connection. */ void do_something(int s) { int x = 10; int y = 15; char buf[100]; printf("do something\n"); read_string(s, buf); printf("server received \"%s\" from socket %d\n", buf, s); sprintf(buf, "%d %d", x, y); write_data(s, buf, strlen(buf)+1); printf("server sent \"%s\" to socket %d\n", buf, s); }