sysdis-tp/client.c

71 lines
1.5 KiB
C
Raw Normal View History

2017-02-01 18:11:53 +00:00
#include "client.h"
int main(int argc, char* argv[]){
/* [1] Initialisation
=========================================================*/
/* (1) Socket information */
struct sockaddr_in server_addr; // server info
int sock; // socket
2017-02-01 18:11:53 +00:00
/* (2) Misc. information */
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
/* [2] Create UPD socket and get target data
2017-02-01 18:11:53 +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
=========================================================*/
/* (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
/* (3) Check if send succesfully */
if( bytes == -1 ){
2017-02-01 18:11:53 +00:00
perror("erreur envoi message");
exit(1);
}
DEBUG&& printf("*** sent %d bytes\n", bytes);
2017-02-01 18:11:53 +00:00
/* [4] Wait for response
=========================================================*/
/* (1) Wait for response */
bytes = xlisten(sock, &server_addr, to_recv, BUFSIZE);
2017-02-01 18:11:53 +00:00
/* (2) Check if received successfully (and consistently) */
if( bytes == -1 ){
2017-02-01 18:11:53 +00:00
perror("erreur réponse serveur");
exit(1);
}
/* (4) log */
printf("*** received : '%s'\n", to_recv);
2017-02-01 18:11:53 +00:00
/* [5] Close socket
=========================================================*/
close(sock);
return 0;
}