diff --git a/client b/client index 3975bb0..7d4df29 100755 Binary files a/client and b/client differ diff --git a/client.c b/client.c index 9ef5caf..e110ecc 100644 --- a/client.c +++ b/client.c @@ -7,26 +7,20 @@ int main(int argc, char* argv[]){ /* [1] Initialisation =========================================================*/ /* (1) Socket information */ - // Server socket address - static struct sockaddr_in server_addr; - // Local socket - int sock; + struct sockaddr_in server_addr; // server info + int sock; // socket /* (2) Misc. information */ - // Response - char *msg = "some message\0"; - // Reception buffer - char buffer[BUFSIZE] = {'\0'}; - // Will contain the response - char *response; - // Will contain the length of received/sent data - int nb_bytes; + int bytes; // transfer count + char* to_send = (char*) malloc( BUFSIZE * sizeof(char) ); + char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) ); + - /* [2] UPD socket creation + /* [2] Create UPD socket and get target data =========================================================*/ - sock = xconnect("localhost", 4000, (struct sockaddr*) &server_addr); + sock = xconnect("localhost", 4000, &server_addr); /* (1-) Manage error */ if( sock == -1 ){ @@ -37,37 +31,35 @@ int main(int argc, char* argv[]){ /* [3] Send message =========================================================*/ - /* (1) Send message to server */ - nb_bytes = xsend(sock, msg, (struct sockaddr*) &server_addr); + /* (1) Set message to send */ + strcpy(to_send, "client message"); - /* (2) Check if send succesfully */ - if( nb_bytes == -1 ){ + /* (2) Send message to server */ + bytes = xsend(sock, to_send, &server_addr); + + /* (3) Check if send succesfully */ + if( bytes == -1 ){ perror("erreur envoi message"); exit(1); } - // log - printf("paquet envoyé, nb_bytes = %d\n",nb_bytes); + DEBUG&& printf("*** sent %d bytes\n", bytes); /* [4] Wait for response =========================================================*/ /* (1) Wait for response */ - nb_bytes = xlisten(sock, (struct sockaddr*) &server_addr, buffer, BUFSIZE); + bytes = xlisten(sock, &server_addr, to_recv, BUFSIZE); /* (2) Check if received successfully (and consistently) */ - if( nb_bytes == -1 ){ + if( bytes == -1 ){ perror("erreur réponse serveur"); exit(1); } - /* (3) Copy response into var */ - response = (char*) malloc(nb_bytes * sizeof(char)); - memcpy(response, buffer, nb_bytes); - /* (4) log */ - printf("response recue du serveur : %s\n",response); + printf("*** received : '%s'\n", to_recv); /* [5] Close socket diff --git a/client.o b/client.o index d7c42ba..d109e27 100644 Binary files a/client.o and b/client.o differ diff --git a/lib.c b/lib.c index f2a8f62..b216e5f 100644 --- a/lib.c +++ b/lib.c @@ -80,14 +80,12 @@ int xbind(const int port){ -int xconnect(const char* hostname, const int port, struct sockaddr* serv){ +int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){ /* [0] Initialization =========================================================*/ int xsocket, bound; struct hostent *host; // data found by lookup - static struct sockaddr_in* serv_in; - serv_in = (void*) serv; /* [1] Create xsocket @@ -128,11 +126,11 @@ int xconnect(const char* hostname, const int port, struct sockaddr* serv){ DEBUG&& debug("xconnect", "building server info"); /* (2) Set server info (ipv4, port) */ - serv_in->sin_family = AF_INET; - serv_in->sin_port = htons(port); + serv->sin_family = AF_INET; + serv->sin_port = htons(port); /* (3) Copy server address */ - memcpy( &serv_in->sin_addr.s_addr, host->h_addr, host->h_length); + memcpy( &serv->sin_addr.s_addr, host->h_addr, host->h_length); DEBUG&& printf("built\n"); @@ -148,17 +146,17 @@ int xconnect(const char* hostname, const int port, struct sockaddr* serv){ -int xlisten(const int xsocket, const struct sockaddr *client, char* buffer, int bufsize){ +int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize){ /* [1] Wait for message through xsocket =========================================================*/ /* (1) Useful data + memory allocation */ socklen_t sock_len = sizeof(struct sockaddr_in); - buffer = (char*) malloc( sizeof(char) * bufsize ); + buffer = (char*) realloc(buffer, sizeof(char) * bufsize ); /* (2) Listen */ DEBUG&& debug("xlisten", "waiting for data"); - int read = recvfrom(xsocket, buffer, bufsize, 0, (struct sockaddr *) client, &sock_len); + int read = recvfrom(xsocket, buffer, bufsize / sizeof(char) + sizeof(char), 0, (struct sockaddr*) client, &sock_len); /* (3) Manage error */ if( read == -1 ){ @@ -168,8 +166,9 @@ int xlisten(const int xsocket, const struct sockaddr *client, char* buffer, int DEBUG&& printf("received\n"); - - + printf("[xlisten:received] '%s'\n", buffer); + + /* [2] Return number of read characters =========================================================*/ return read; @@ -180,16 +179,16 @@ int xlisten(const int xsocket, const struct sockaddr *client, char* buffer, int -int xsend(const int xsocket, char* buffer, struct sockaddr* target){ +int xsend(const int xsocket, char* buffer, struct sockaddr_in* target){ /* [1] Send buffer (message) to target =========================================================*/ /* (1) Useful data */ - socklen_t addr_len = sizeof(struct sockaddr_in); + size_t addr_len = sizeof(struct sockaddr_in); /* (2) Send data */ DEBUG&& debug("xsend", "sending data"); - int sent = sendto(xsocket, buffer, strlen(buffer), 0, target, addr_len); + int sent = sendto(xsocket, buffer, strlen(buffer)+1, 0, (struct sockaddr*) target, addr_len); /* (3) Manage error */ if( sent == -1 ){ @@ -200,6 +199,9 @@ int xsend(const int xsocket, char* buffer, struct sockaddr* target){ DEBUG&& printf("sent\n"); + printf("[xsend:sent] '%s'\n", buffer); + + /* [2] Return number of sent characters =========================================================*/ return sent; diff --git a/lib.h b/lib.h index 2265e30..cca2bff 100644 --- a/lib.h +++ b/lib.h @@ -31,7 +31,7 @@ int debug(const char* tag, const char* msg); int xbind(const int port); - int xconnect(const char* hostname, const int port, struct sockaddr* serv); - int xlisten(const int xsocket, const struct sockaddr *client, char* buffer, int bufsize); - int xsend(const int xsocket, char* buffer, struct sockaddr* target); + int xconnect(const char* hostname, const int port, struct sockaddr_in* serv); + int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize); + int xsend(const int xsocket, char* buffer, struct sockaddr_in* target); #endif diff --git a/lib.o b/lib.o index 78eba4d..aa27e38 100644 Binary files a/lib.o and b/lib.o differ diff --git a/makefile b/makefile index 0a49cce..7ca2720 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -CC=-Wall -Werror -g +CC=-Werror -g client.o: client.h client.c diff --git a/server b/server index 8bcf89c..f6d3c26 100755 Binary files a/server and b/server differ diff --git a/server.c b/server.c index c4f0c3f..916f900 100644 --- a/server.c +++ b/server.c @@ -8,23 +8,16 @@ int main(int argc, char* argv[]){ =========================================================*/ /* (1) Socket info */ // Client - static struct sockaddr_in addr_client; - // Client identifier - struct hostent *host_client = NULL; + struct sockaddr_in addr_client; char client_ip[20]; // Socket int sock; /* (2) Misc. information */ - // response - char *response = "bien recu"; - // reception buffer - char buffer[BUFSIZE]; - // received buffer - char *received; - // length of received/sent data - int buf_len; + char* to_send = (char*) malloc( BUFSIZE * sizeof(char) ); + char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) ); + int bytes; // transfer count @@ -42,11 +35,12 @@ int main(int argc, char* argv[]){ /* [3] Wait for client message =========================================================*/ - buf_len = xlisten(sock, (struct sockaddr*) &addr_client, buffer, BUFSIZE); + /* (1) Listen to data */ + bytes = xlisten(sock, &addr_client, to_recv, BUFSIZE); - /* (1-) Manage error */ - if( buf_len == -1 ){ + /* (2) Manage error */ + if( bytes == -1 ){ perror("erreur réception paquet"); exit(1); } @@ -54,31 +48,18 @@ int main(int argc, char* argv[]){ // Get client ip inet_ntop(AF_INET, &(addr_client.sin_addr), client_ip, 20); - /* (2) Fetch client information */ - // récupère nom de la machine émettrice des données - host_client = gethostbyaddr( &addr_client.sin_addr, // ) client addr (filled) - /* ( */ sizeof(struct in_addr), // ) sizeof addr - /* ( */ AF_INET ); // ipv4 - - /* (2-) Manage error */ - if( host_client == NULL ){ - perror("erreur gethostbyaddr"); - exit(1); - } - - /* (3) Store message */ - received = (char *) malloc(buf_len * sizeof(char)); - memcpy(received, buffer, buf_len); - - printf( "recu message %s de %s:%d\n", received, client_ip, ntohs(addr_client.sin_port) ); + printf("*** received '%s' (%d bytes) from %s:%d\n", to_recv, bytes, client_ip, ntohs(addr_client.sin_port)); /* [4] Send response =========================================================*/ - /* (1) Send response */ - xsend(sock, response, (struct sockaddr*)&addr_client); + /* (1) Set response data */ + strcpy(to_send, "server response"); + + /* (2) Send response */ + bytes = xsend(sock, to_send, &addr_client); - /* (1-) Manage error */ - if( buf_len == -1 ){ + /* (3) Manage error */ + if( bytes == -1 ){ perror("erreur envoi réponse"); exit(1); } diff --git a/server.o b/server.o index dca7da1..dfff7f1 100644 Binary files a/server.o and b/server.o differ