2017-02-01 18:11:53 +00:00
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]){
|
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
/* [1] Initialization + arguments
|
2017-02-01 18:11:53 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) Socket information */
|
2017-02-05 14:31:44 +00:00
|
|
|
struct sockaddr_in server_addr; // server info
|
2017-02-08 10:44:10 +00:00
|
|
|
int sock, // socket
|
|
|
|
port; // chosen port
|
2017-02-05 21:17:48 +00:00
|
|
|
char hostname[24]; // chosen hostname
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
/* (2) Misc. information */
|
2017-02-05 14:31:44 +00:00
|
|
|
int bytes; // transfer count
|
|
|
|
char* to_send = (char*) malloc( BUFSIZE * sizeof(char) );
|
|
|
|
char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) );
|
|
|
|
|
2017-02-01 18:11:53 +00:00
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
/* [2] Manage arguments
|
|
|
|
=========================================================*/
|
|
|
|
/* (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;
|
|
|
|
}
|
2017-02-01 18:11:53 +00:00
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
}else
|
|
|
|
strcpy(to_send, "client default message");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Create UPD socket and get target data
|
2017-02-01 18:11:53 +00:00
|
|
|
=========================================================*/
|
2017-02-05 21:17:48 +00:00
|
|
|
sock = xconnect(hostname, port, &server_addr);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
/* (1-) Manage error */
|
|
|
|
if( sock == -1 ){
|
2017-02-08 10:44:10 +00:00
|
|
|
perror("erreur connection server");
|
2017-02-01 18:11:53 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
/* [4] Send message
|
2017-02-01 18:11:53 +00:00
|
|
|
=========================================================*/
|
2017-02-05 21:17:48 +00:00
|
|
|
/* (1) Send message to server */
|
2017-02-08 10:44:10 +00:00
|
|
|
bytes = xwrite(sock, to_send);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
/* (2) Check if send succesfully */
|
2017-02-05 14:31:44 +00:00
|
|
|
if( bytes == -1 ){
|
2017-02-01 18:11:53 +00:00
|
|
|
perror("erreur envoi message");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-02-05 14:31:44 +00:00
|
|
|
DEBUG&& printf("*** sent %d bytes\n", bytes);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
/* [5] Wait for response
|
2017-02-01 18:11:53 +00:00
|
|
|
=========================================================*/
|
2017-02-05 11:08:02 +00:00
|
|
|
/* (1) Wait for response */
|
2017-02-08 10:44:10 +00:00
|
|
|
bytes = xread(sock, to_recv, BUFSIZE);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
/* (2) Check if received successfully (and consistently) */
|
2017-02-05 14:31:44 +00:00
|
|
|
if( bytes == -1 ){
|
2017-02-01 18:11:53 +00:00
|
|
|
perror("erreur réponse serveur");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) log */
|
2017-02-05 14:31:44 +00:00
|
|
|
printf("*** received : '%s'\n", to_recv);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
/* [6] Close socket
|
2017-02-01 18:11:53 +00:00
|
|
|
=========================================================*/
|
|
|
|
close(sock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|