2017-02-01 18:11:53 +00:00
|
|
|
#include "server.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]){
|
|
|
|
|
|
|
|
/* [1] Initialisation
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Socket info */
|
|
|
|
// Client
|
|
|
|
static struct sockaddr_in addr_client;
|
|
|
|
// Client identifier
|
2017-02-02 10:18:34 +00:00
|
|
|
struct hostent *host_client = NULL;
|
|
|
|
char client_ip[20];
|
2017-02-01 18:11:53 +00:00
|
|
|
// Socket
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Misc. information */
|
|
|
|
// response
|
|
|
|
char *response = "bien recu";
|
|
|
|
// reception buffer
|
|
|
|
char buffer[BUFSIZE];
|
|
|
|
// received buffer
|
|
|
|
char *received;
|
|
|
|
// length of received/sent data
|
|
|
|
int buf_len;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Create socket
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Create socket */
|
2017-02-05 11:08:02 +00:00
|
|
|
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
|
|
|
|
=========================================================*/
|
2017-02-05 11:08:02 +00:00
|
|
|
buf_len = xlisten(sock, (struct sockaddr*) &addr_client, buffer, BUFSIZE);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (1-) Manage error */
|
|
|
|
if( buf_len == -1 ){
|
|
|
|
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);
|
|
|
|
|
2017-02-01 18:11:53 +00:00
|
|
|
/* (2) Fetch client information */
|
|
|
|
// récupère nom de la machine émettrice des données
|
2017-02-02 10:18:34 +00:00
|
|
|
host_client = gethostbyaddr( &addr_client.sin_addr, // ) client addr (filled)
|
|
|
|
/* ( */ sizeof(struct in_addr), // ) sizeof addr
|
|
|
|
/* ( */ AF_INET ); // ipv4
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
/* (2-) Manage error */
|
|
|
|
if( host_client == NULL ){
|
|
|
|
perror("erreur gethostbyaddr");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Store message */
|
|
|
|
received = (char *) malloc(buf_len * sizeof(char));
|
|
|
|
memcpy(received, buffer, buf_len);
|
|
|
|
|
2017-02-02 10:18:34 +00:00
|
|
|
printf( "recu message %s de %s:%d\n", received, client_ip, ntohs(addr_client.sin_port) );
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
/* [4] Send response
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Send response */
|
2017-02-05 11:08:02 +00:00
|
|
|
xsend(sock, response, (struct sockaddr*)&addr_client);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
/* (1-) Manage error */
|
|
|
|
if( buf_len == -1 ){
|
|
|
|
perror("erreur envoi réponse");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [5] Close socket
|
|
|
|
=========================================================*/
|
|
|
|
close(sock);
|
|
|
|
}
|