sysdis-tp/client.c

71 lines
1.5 KiB
C

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