diff --git a/client b/client index 7d4df29..c2d7765 100755 Binary files a/client and b/client differ diff --git a/client.c b/client.c index e110ecc..48ced06 100644 --- a/client.c +++ b/client.c @@ -4,23 +4,56 @@ int main(int argc, char* argv[]){ - /* [1] Initialisation + /* [1] Initialization + arguments =========================================================*/ /* (1) Socket information */ struct sockaddr_in server_addr; // server info int sock; // socket + int port; // chosen port + char hostname[24]; // chosen hostname /* (2) Misc. information */ int bytes; // transfer count char* to_send = (char*) malloc( BUFSIZE * sizeof(char) ); char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) ); - - - /* [2] Create UPD socket and get target data + /* [2] Manage arguments =========================================================*/ - sock = xconnect("localhost", 4000, &server_addr); + /* (1) Manage arguments count */ + if( argc < 3 ){ + printf("Missing arguments\nUsage: client hostname port [message]\n"); + return 1; + } + + /* (2) Manage @hostname argument */ + if( strlen(argv[1]) > 24 || sscanf(argv[1], "%s", hostname) <= 0 ){ + printf("argument error: hostname must be a string (max: 24 characters long)\n"); + return 1; + } + + /* (3) Manage port argument */ + if( sscanf(argv[2], "%d", &port) <= 0 ){ + printf("argument error: port must be a valid integer\n"); + return 1; + } + + /* (4) Manage optional @message argument */ + if( argc >= 4 ){ + + if( strlen(argv[3]) > BUFSIZE || sscanf(argv[3], "%s", to_send) <= 0 ){ + printf("argument error: message must be a string (max: %d characters long)\n", BUFSIZE); + return 1; + } + + }else + strcpy(to_send, "client default message"); + + + + /* [3] Create UPD socket and get target data + =========================================================*/ + sock = xconnect(hostname, port, &server_addr); /* (1-) Manage error */ if( sock == -1 ){ @@ -29,15 +62,12 @@ int main(int argc, char* argv[]){ } - /* [3] Send message + /* [4] Send message =========================================================*/ - /* (1) Set message to send */ - strcpy(to_send, "client message"); - - /* (2) Send message to server */ + /* (1) Send message to server */ bytes = xsend(sock, to_send, &server_addr); - /* (3) Check if send succesfully */ + /* (2) Check if send succesfully */ if( bytes == -1 ){ perror("erreur envoi message"); exit(1); @@ -47,7 +77,7 @@ int main(int argc, char* argv[]){ - /* [4] Wait for response + /* [5] Wait for response =========================================================*/ /* (1) Wait for response */ bytes = xlisten(sock, &server_addr, to_recv, BUFSIZE); @@ -62,7 +92,7 @@ int main(int argc, char* argv[]){ printf("*** received : '%s'\n", to_recv); - /* [5] Close socket + /* [6] Close socket =========================================================*/ close(sock); diff --git a/client.o b/client.o index d109e27..8cae374 100644 Binary files a/client.o and b/client.o differ diff --git a/server b/server index f6d3c26..7c26bea 100755 Binary files a/server and b/server differ diff --git a/server.c b/server.c index 916f900..f40639f 100644 --- a/server.c +++ b/server.c @@ -10,6 +10,7 @@ int main(int argc, char* argv[]){ // Client struct sockaddr_in addr_client; char client_ip[20]; + int port; // chosen port // Socket int sock; @@ -20,11 +21,40 @@ int main(int argc, char* argv[]){ int bytes; // transfer count + /* [2] Manage arguments + =========================================================*/ + /* (1) Manage arguments count */ + if( argc < 2 ){ + printf("Missing arguments\nUsage: server port [message]\n"); + printf("port = 0 : automatic port\n"); + return 1; + } - /* [2] Create socket + /* (2) Manage port argument */ + if( sscanf(argv[1], "%d", &port) <= 0 ){ + printf("argument error: port must be a valid integer\n"); + printf("port = 0 : automatic port\n"); + return 1; + } + + /* (3) Manage optional @message argument */ + if( argc >= 3 ){ + + if( strlen(argv[2]) > BUFSIZE || sscanf(argv[2], "%s", to_send) <= 0 ){ + printf("argument error: message must be a string (max: %d characters long)\n", BUFSIZE); + return 1; + } + + }else + strcpy(to_send, "server default message"); + + + + + /* [3] Create socket =========================================================*/ /* (1) Create socket */ - sock = xbind(4000); + sock = xbind(port); /* (1-) Manage error */ if( sock == -1 ){ @@ -33,7 +63,7 @@ int main(int argc, char* argv[]){ } - /* [3] Wait for client message + /* [4] Wait for client message =========================================================*/ /* (1) Listen to data */ bytes = xlisten(sock, &addr_client, to_recv, BUFSIZE); @@ -50,21 +80,21 @@ int main(int argc, char* argv[]){ printf("*** received '%s' (%d bytes) from %s:%d\n", to_recv, bytes, client_ip, ntohs(addr_client.sin_port)); - /* [4] Send response + /* [5] Send response =========================================================*/ - /* (1) Set response data */ - strcpy(to_send, "server response"); - - /* (2) Send response */ + /* (1) Send response */ bytes = xsend(sock, to_send, &addr_client); - /* (3) Manage error */ + /* (2) Manage error */ if( bytes == -1 ){ perror("erreur envoi réponse"); exit(1); } - /* [5] Close socket + + /* [6] Close socket =========================================================*/ close(sock); + + return 0; } diff --git a/server.o b/server.o index dfff7f1..4e516f9 100644 Binary files a/server.o and b/server.o differ