sysdis-tp/client.c

79 lines
1.7 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 */
// Server socket address
static struct sockaddr_in server_addr;
// Local socket
int sock;
/* (2) Misc. information */
// Response
char *msg = "some message\0";
2017-02-01 18:11:53 +00:00
// Reception buffer
char buffer[BUFSIZE] = {'\0'};
2017-02-01 18:11:53 +00:00
// Will contain the response
char *response;
// Will contain the length of received/sent data
int nb_bytes;
/* [2] UPD socket creation
=========================================================*/
sock = xconnect("localhost", 4000, (struct sockaddr*) &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) Send message to server */
nb_bytes = xsend(sock, msg, (struct sockaddr*) &server_addr);
2017-02-01 18:11:53 +00:00
/* (2) Check if send succesfully */
if( nb_bytes == -1 ){
perror("erreur envoi message");
exit(1);
}
// log
printf("paquet envoyé, nb_bytes = %d\n",nb_bytes);
/* [4] Wait for response
=========================================================*/
/* (1) Wait for response */
nb_bytes = xlisten(sock, (struct sockaddr*) &server_addr, buffer, BUFSIZE);
2017-02-01 18:11:53 +00:00
/* (2) Check if received successfully (and consistently) */
if( nb_bytes == -1 ){
perror("erreur réponse serveur");
exit(1);
}
/* (3) Copy response into var */
response = (char*) malloc(nb_bytes * sizeof(char));
2017-02-01 18:11:53 +00:00
memcpy(response, buffer, nb_bytes);
/* (4) log */
printf("response recue du serveur : %s\n",response);
/* [5] Close socket
=========================================================*/
close(sock);
return 0;
}