Argument management

This commit is contained in:
xdrm-brackets 2017-02-05 22:17:48 +01:00
parent 08ea46ca34
commit 03b786779a
6 changed files with 83 additions and 23 deletions

BIN
client

Binary file not shown.

View File

@ -4,23 +4,56 @@
int main(int argc, char* argv[]){ int main(int argc, char* argv[]){
/* [1] Initialisation /* [1] Initialization + arguments
=========================================================*/ =========================================================*/
/* (1) Socket information */ /* (1) Socket information */
struct sockaddr_in server_addr; // server info struct sockaddr_in server_addr; // server info
int sock; // socket int sock; // socket
int port; // chosen port
char hostname[24]; // chosen hostname
/* (2) Misc. information */ /* (2) Misc. information */
int bytes; // transfer count int bytes; // transfer count
char* to_send = (char*) malloc( BUFSIZE * sizeof(char) ); char* to_send = (char*) malloc( BUFSIZE * sizeof(char) );
char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) ); char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) );
/* [2] Manage arguments
/* [2] Create UPD socket and get target data
=========================================================*/ =========================================================*/
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 */ /* (1-) Manage error */
if( sock == -1 ){ if( sock == -1 ){
@ -29,15 +62,12 @@ int main(int argc, char* argv[]){
} }
/* [3] Send message /* [4] Send message
=========================================================*/ =========================================================*/
/* (1) Set message to send */ /* (1) Send message to server */
strcpy(to_send, "client message");
/* (2) Send message to server */
bytes = xsend(sock, to_send, &server_addr); bytes = xsend(sock, to_send, &server_addr);
/* (3) Check if send succesfully */ /* (2) Check if send succesfully */
if( bytes == -1 ){ if( bytes == -1 ){
perror("erreur envoi message"); perror("erreur envoi message");
exit(1); exit(1);
@ -47,7 +77,7 @@ int main(int argc, char* argv[]){
/* [4] Wait for response /* [5] Wait for response
=========================================================*/ =========================================================*/
/* (1) Wait for response */ /* (1) Wait for response */
bytes = xlisten(sock, &server_addr, to_recv, BUFSIZE); bytes = xlisten(sock, &server_addr, to_recv, BUFSIZE);
@ -62,7 +92,7 @@ int main(int argc, char* argv[]){
printf("*** received : '%s'\n", to_recv); printf("*** received : '%s'\n", to_recv);
/* [5] Close socket /* [6] Close socket
=========================================================*/ =========================================================*/
close(sock); close(sock);

BIN
client.o

Binary file not shown.

BIN
server

Binary file not shown.

View File

@ -10,6 +10,7 @@ int main(int argc, char* argv[]){
// Client // Client
struct sockaddr_in addr_client; struct sockaddr_in addr_client;
char client_ip[20]; char client_ip[20];
int port; // chosen port
// Socket // Socket
int sock; int sock;
@ -20,11 +21,40 @@ int main(int argc, char* argv[]){
int bytes; // transfer count 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 */ /* (1) Create socket */
sock = xbind(4000); sock = xbind(port);
/* (1-) Manage error */ /* (1-) Manage error */
if( sock == -1 ){ 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 */ /* (1) Listen to data */
bytes = xlisten(sock, &addr_client, to_recv, BUFSIZE); 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)); 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 */ /* (1) Send response */
strcpy(to_send, "server response");
/* (2) Send response */
bytes = xsend(sock, to_send, &addr_client); bytes = xsend(sock, to_send, &addr_client);
/* (3) Manage error */ /* (2) Manage error */
if( bytes == -1 ){ if( bytes == -1 ){
perror("erreur envoi réponse"); perror("erreur envoi réponse");
exit(1); exit(1);
} }
/* [5] Close socket
/* [6] Close socket
=========================================================*/ =========================================================*/
close(sock); close(sock);
return 0;
} }

BIN
server.o

Binary file not shown.