sysdis-tp/client.c

79 lines
1.7 KiB
C

#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";
// Reception buffer
char buffer[BUFSIZE] = {'\0'};
// 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);
/* (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);
/* (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);
/* (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));
memcpy(response, buffer, nb_bytes);
/* (4) log */
printf("response recue du serveur : %s\n",response);
/* [5] Close socket
=========================================================*/
close(sock);
return 0;
}