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,11 +4,13 @@
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
@ -16,11 +18,42 @@ int main(int argc, char* argv[]){
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);

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
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;
}

BIN
server.o

Binary file not shown.