/* ========================================================================== */ /* */ /* server.c */ /* (c) 2008 jocelyne.lecomte@alcatel-lucent.fr */ /* */ /* Description */ /* */ /* ========================================================================== */ #include #include #include #include #include "server.h" /*******************************************************************************/ /* Initialize connection */ /*******************************************************************************/ static int init_connection(void) { /* UDP protocol, so SOCK_DGRAM */ SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); fprintf(stdout, "create socket\n"); /* ERROR */ if (sock == INVALID_SOCKET) { perror("socket()"); exit(errno); } /* OK */ SOCKADDR_IN sin ={0}; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(PORT); sin.sin_family = AF_INET; if (bind(sock, (SOCKADDR *) &sin, sizeof sin) == SOCKET_ERROR) { perror("bind()"); exit(errno); } fprintf(stdout, "socket OK\n"); return sock; } /*******************************************************************************/ /* close socket */ /*******************************************************************************/ static void end_connection(SOCKET sock) { close(sock); fprintf(stdout, "close socket\n"); } /*******************************************************************************/ /* read data from client */ /*******************************************************************************/ static int read_client(SOCKET sock, SOCKADDR_IN *sin, char *buffer) { int n=0; size_t sinsize = sizeof *sin; /* error in reception */ if ((n = recvfrom(sock, buffer, BUF_SIZE-1, 0, (SOCKADDR*)sin, &sinsize))<0) { perror("recvfrom()"); exit(errno); } /* OK */ fprintf(stdout, "read socket from client\n"); buffer[n] = 0; return n; } /*******************************************************************************/ /* write data to client */ /*******************************************************************************/ static void write_client(SOCKET sock, SOCKADDR_IN *sin, const char* buffer) { /* error in sending msg */ if (sendto(sock, buffer, strlen(buffer), 0, (SOCKADDR*)sin, sizeof *sin)<0) { perror("sendto()"); exit(errno); } fprintf(stdout, "write socket to client\n"); } /*******************************************************************************/ /* etablish communication */ /*******************************************************************************/ void comm(void) { SOCKET sock = init_connection(); char buffer[BUF_SIZE]; /* index of array */ int actual = 0; int max = sock; /* array for clients */ Client clients[MAX_CLIENTS]; fd_set readfs; while(1) { /* set the descripor to null */ FD_ZERO(&readfs); /* add STDIN_FILENO */ FD_SET(STDIN_FILENO, &readfs); /* add the socket */ FD_SET(sock, &readfs); /* management of modication of descriptors */ struct timeval timeout; timeout.tv_sec = 20; timeout.tv_usec = 0; fprintf(stdout, "before select()\n"); if (select(max+1, &readfs, NULL, NULL, &timeout)==-1) /* fprintf(stdout, "before select()\n"); if (select(max+1, &readfs, NULL, NULL, NULL)==-1) */ { perror("select()"); exit(errno); } fprintf(stdout, "after select()\n"); /* something from standard input */ if (FD_ISSET(STDIN_FILENO, &readfs)) { /* stop process when type on keyboard */ break; } else if (FD_ISSET(sock, &readfs)) { /* new client */ SOCKADDR_IN csin = {0}; fprintf(stdout, "new client"); /* a client is talking */ read_client(sock, &csin, buffer); fprintf(stdout, buffer); /* write back to client */ write_client(sock, &csin, buffer); /* TODO */ } } end_connection(sock); } static void send_msg_to_all_clients(SOCKET sock, Client *clients, Client *client, int actual, const char *buffer, char from_server) { fprintf(stderr, "Not yet implemented\n"); } static void remove_client(Client *clients, int to_remove, int *actual) { fprintf(stderr, "Not yet implemented\n"); } static check_if_client_exist(Client *clients, SOCKADDR_IN *csin, int actual) { fprintf(stderr, "Not yet implemented\n"); return 0; } static Client* get_client(Client *clients, SOCKADDR_IN *csin, int actual) { fprintf(stderr, "Not yet implemented\n"); return NULL; }