sysdis-tp/server.c

116 lines
2.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-05 21:17:48 +00:00
int port; // chosen port
2017-02-01 18:11:53 +00:00
// Socket
2017-02-08 10:44:10 +00:00
int listensock, datasock;
2017-02-01 18:11:53 +00:00
/* (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
2017-02-05 21:17:48 +00:00
/* [2] Manage arguments
=========================================================*/
/* (1) Manage arguments count */
if( argc < 2 ){
printf("Missing arguments\nUsage: server port [message]\n");
printf("port = 0 : automatic port\n");
return 1;
}
/* (2) Manage port argument */
if( sscanf(argv[1], "%d", &port) <= 0 ){
printf("argument error: port must be a valid integer\n");
printf("port = 0 : automatic port\n");
return 1;
}
/* (3) Manage optional @message argument */
if( argc >= 3 ){
if( strlen(argv[2]) > BUFSIZE || sscanf(argv[2], "%s", to_send) <= 0 ){
printf("argument error: message must be a string (max: %d characters long)\n", BUFSIZE);
return 1;
}
}else
strcpy(to_send, "server default message");
2017-02-01 18:11:53 +00:00
2017-02-05 21:17:48 +00:00
/* [3] Create socket
2017-02-01 18:11:53 +00:00
=========================================================*/
/* (1) Create socket */
2017-02-08 10:44:10 +00:00
listensock = xbind(port);
2017-02-01 18:11:53 +00:00
/* (1-) Manage error */
2017-02-08 10:44:10 +00:00
if( listensock == -1 ){
2017-02-01 18:11:53 +00:00
perror("erreur création socket");
exit(1);
}
2017-02-08 10:44:10 +00:00
/* [4] Wait for client
2017-02-01 18:11:53 +00:00
=========================================================*/
2017-02-08 10:44:10 +00:00
/* (1) Listen to incoming connection */
datasock = xlisten(listensock, &addr_client);
/* (2) Manage error */
if( datasock == -1 ){
perror("erreur connection client");
exit(1);
}
2017-02-01 18:11:53 +00:00
2017-02-08 10:44:10 +00:00
/* [5] Wait for data
=========================================================*/
/* (1) wait/read data */
bytes = xread(datasock, to_recv, BUFSIZE);
/* (2) Manage error */
if( bytes == -1 ){
2017-02-08 10:44:10 +00:00
perror("erreur reception paquet");
2017-02-01 18:11:53 +00:00
exit(1);
}
2017-02-08 10:44:10 +00:00
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-08 10:44:10 +00:00
2017-02-01 18:11:53 +00:00
2017-02-08 10:44:10 +00:00
/* [6] Send response
2017-02-01 18:11:53 +00:00
=========================================================*/
2017-02-05 21:17:48 +00:00
/* (1) Send response */
2017-02-08 10:44:10 +00:00
bytes = xwrite(datasock, to_send);
2017-02-05 21:17:48 +00:00
/* (2) Manage error */
if( bytes == -1 ){
2017-02-01 18:11:53 +00:00
perror("erreur envoi réponse");
exit(1);
}
2017-02-05 21:17:48 +00:00
/* [6] Close socket
2017-02-01 18:11:53 +00:00
=========================================================*/
2017-02-08 10:44:10 +00:00
close(datasock);
close(listensock);
2017-02-05 21:17:48 +00:00
return 0;
2017-02-01 18:11:53 +00:00
}