2017-02-01 18:11:53 +00:00
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]){
|
|
|
|
|
|
|
|
/* [1] Initialisation
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Socket information */
|
2017-02-05 14:31:44 +00:00
|
|
|
struct sockaddr_in server_addr; // server info
|
|
|
|
int sock; // socket
|
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 14:31:44 +00:00
|
|
|
/* [2] Create UPD socket and get target data
|
2017-02-01 18:11:53 +00:00
|
|
|
=========================================================*/
|
2017-02-05 14:31:44 +00:00
|
|
|
sock = xconnect("localhost", 4000, &server_addr);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
/* (1-) Manage error */
|
|
|
|
if( sock == -1 ){
|
|
|
|
perror("erreur création socket");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Send message
|
|
|
|
=========================================================*/
|
2017-02-05 14:31:44 +00:00
|
|
|
/* (1) Set message to send */
|
|
|
|
strcpy(to_send, "client message");
|
|
|
|
|
|
|
|
/* (2) Send message to server */
|
|
|
|
bytes = xsend(sock, to_send, &server_addr);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
2017-02-05 14:31:44 +00:00
|
|
|
/* (3) Check if send succesfully */
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] Wait for response
|
|
|
|
=========================================================*/
|
2017-02-05 11:08:02 +00:00
|
|
|
/* (1) Wait for response */
|
2017-02-05 14:31:44 +00:00
|
|
|
bytes = xlisten(sock, &server_addr, 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
|
|
|
|
|
|
|
|
|
|
|
/* [5] Close socket
|
|
|
|
=========================================================*/
|
|
|
|
close(sock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|