#include "server.h" int main(int argc, char* argv[]){ /* [1] Initialisation =========================================================*/ /* (1) Socket info */ // Client static struct sockaddr_in addr_client; // Client identifier struct hostent *host_client = NULL; char client_ip[20]; // 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 */ sock = xbind(4000); /* (1-) Manage error */ if( sock == -1 ){ perror("erreur création socket"); exit(1); } /* [3] Wait for client message =========================================================*/ buf_len = xlisten(sock, (struct sockaddr*) &addr_client, buffer, BUFSIZE); /* (1-) Manage error */ if( buf_len == -1 ){ perror("erreur réception paquet"); exit(1); } // Get client ip inet_ntop(AF_INET, &(addr_client.sin_addr), client_ip, 20); /* (2) Fetch client information */ // récupère nom de la machine émettrice des données host_client = gethostbyaddr( &addr_client.sin_addr, // ) client addr (filled) /* ( */ sizeof(struct in_addr), // ) sizeof addr /* ( */ AF_INET ); // ipv4 /* (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); printf( "recu message %s de %s:%d\n", received, client_ip, ntohs(addr_client.sin_port) ); /* [4] Send response =========================================================*/ /* (1) Send response */ xsend(sock, response, (struct sockaddr*)&addr_client); /* (1-) Manage error */ if( buf_len == -1 ){ perror("erreur envoi réponse"); exit(1); } /* [5] Close socket =========================================================*/ close(sock); }