sysdis-tp/server.c

71 lines
1.5 KiB
C
Raw Normal View History

2017-02-01 18:11:53 +00:00
#include "server.h"
int main(int argc, char* argv[]){
/* [1] Initialisation
=========================================================*/
/* (1) Socket info */
// Client
struct sockaddr_in addr_client;
2017-02-02 10:18:34 +00:00
char client_ip[20];
2017-02-01 18:11:53 +00:00
// Socket
int sock;
/* (2) Misc. information */
char* to_send = (char*) malloc( BUFSIZE * sizeof(char) );
char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) );
int bytes; // transfer count
2017-02-01 18:11:53 +00:00
/* [2] Create socket
=========================================================*/
/* (1) Create socket */
sock = xbind(4000);
2017-02-01 18:11:53 +00:00
/* (1-) Manage error */
if( sock == -1 ){
perror("erreur création socket");
exit(1);
}
/* [3] Wait for client message
=========================================================*/
/* (1) Listen to data */
bytes = xlisten(sock, &addr_client, to_recv, BUFSIZE);
2017-02-01 18:11:53 +00:00
/* (2) Manage error */
if( bytes == -1 ){
2017-02-01 18:11:53 +00:00
perror("erreur réception paquet");
exit(1);
}
2017-02-02 10:18:34 +00:00
// Get client ip
inet_ntop(AF_INET, &(addr_client.sin_addr), client_ip, 20);
printf("*** received '%s' (%d bytes) from %s:%d\n", to_recv, bytes, client_ip, ntohs(addr_client.sin_port));
2017-02-01 18:11:53 +00:00
/* [4] Send response
=========================================================*/
/* (1) Set response data */
strcpy(to_send, "server response");
/* (2) Send response */
bytes = xsend(sock, to_send, &addr_client);
/* (3) Manage error */
if( bytes == -1 ){
2017-02-01 18:11:53 +00:00
perror("erreur envoi réponse");
exit(1);
}
/* [5] Close socket
=========================================================*/
close(sock);
}