/* ========================================================================== */ /* */ /* client.c */ /* (c) 2008 jocelyne.lecomte@alcatel-lucent.fr */ /* */ /* Description */ /* */ /* ========================================================================== */ #include #include #include #include #include "client.h" /*******************************************************************************/ /* Initialize connection to one address/port */ /*******************************************************************************/ static int init_connection(const char *address, const int port, SOCKADDR_IN *sin) { /* 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 */ struct hostent *hostinfo; hostinfo = gethostbyname(address); fprintf(stdout, "search for address\n"); /* can't find host */ if (hostinfo == NULL) { fprintf (stderr, "Unknown host %s.\n", address); exit(EXIT_FAILURE); } /* OK */ sin->sin_addr = *(IN_ADDR *)hostinfo->h_addr; sin->sin_port = htons(port); sin->sin_family = AF_INET; 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 server */ /*******************************************************************************/ static int read_server(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 server\n"); buffer[n] = 0; return n; } /*******************************************************************************/ /* write data to server */ /*******************************************************************************/ static void write_server(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 server\n"); } /*******************************************************************************/ /* etablish communication */ /*******************************************************************************/ void comm(const char *address, const int port) { SOCKADDR_IN sin = {0}; fprintf(stdout, "Comm function()\n"); SOCKET sock = init_connection(address, port, &sin); char buffer[BUF_SIZE]; fd_set readfs; char msg[30] = "Hello from Jocelyne !\n"; fprintf(stdout, msg); write_server(sock, &sin, msg); 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 = 1; timeout.tv_usec = 01; if (select(sock+1, &readfs, NULL, NULL, &timeout)==-1) { perror("select()"); exit(errno); } /* something from standard input */ if (FD_ISSET(STDIN_FILENO, &readfs)) { fgets(buffer, BUF_SIZE-1, stdin); { char *p = NULL; p = strstr(buffer, "\n"); if (p != NULL) { *p = 0; } else { /* fclean */ buffer[BUF_SIZE-1] = 0; } } write_server(sock, &sin, buffer); } else if (FD_ISSET(sock, &readfs)) { int n=read_server(sock, &sin, buffer); /* if server is down */ if (n==0) { printf("Server disconnected !\n"); break; } puts(buffer); } } end_connection(sock); }