2017-02-01 18:11:53 +00:00
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]){
|
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
/* [1] Initialization + arguments
|
2017-02-01 18:11:53 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) Socket information */
|
2017-02-05 14:31:44 +00:00
|
|
|
struct sockaddr_in server_addr; // server info
|
2017-02-08 10:44:10 +00:00
|
|
|
int sock, // socket
|
|
|
|
port; // chosen port
|
2017-02-05 21:17:48 +00:00
|
|
|
char hostname[24]; // chosen hostname
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
/* (2) Misc. information */
|
2017-02-05 14:31:44 +00:00
|
|
|
int bytes; // transfer count
|
2017-02-22 11:35:41 +00:00
|
|
|
char* to_send = (char*) malloc( sizeof(requete)+sizeof(int)+1 );
|
|
|
|
long to_recv;
|
|
|
|
requete request;
|
|
|
|
int param;
|
2017-02-05 14:31:44 +00:00
|
|
|
|
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 < 3 ){
|
|
|
|
printf("Missing arguments\nUsage: client hostname port [message]\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Manage @hostname argument */
|
|
|
|
if( strlen(argv[1]) > 24 || sscanf(argv[1], "%s", hostname) <= 0 ){
|
|
|
|
printf("argument error: hostname must be a string (max: 24 characters long)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Manage port argument */
|
|
|
|
if( sscanf(argv[2], "%d", &port) <= 0 ){
|
|
|
|
printf("argument error: port must be a valid integer\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Manage optional @message argument */
|
|
|
|
if( argc >= 4 ){
|
|
|
|
|
|
|
|
if( strlen(argv[3]) > BUFSIZE || sscanf(argv[3], "%s", to_send) <= 0 ){
|
|
|
|
printf("argument error: message must be a string (max: %d characters long)\n", BUFSIZE);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-02-01 18:11:53 +00:00
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
}else
|
|
|
|
strcpy(to_send, "client default message");
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-22 11:35:41 +00:00
|
|
|
/* [3] Create TCP socket and get target data
|
2017-02-01 18:11:53 +00:00
|
|
|
=========================================================*/
|
2017-02-05 21:17:48 +00:00
|
|
|
sock = xconnect(hostname, port, &server_addr);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
/* (1-) Manage error */
|
|
|
|
if( sock == -1 ){
|
2017-02-08 10:44:10 +00:00
|
|
|
perror("erreur connection server");
|
2017-02-01 18:11:53 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-22 11:35:41 +00:00
|
|
|
request.type = FACTO;
|
|
|
|
//size of the request isn't important
|
|
|
|
request.taille = sizeof(int);
|
|
|
|
|
|
|
|
param = 2;
|
|
|
|
|
|
|
|
memcpy(to_send,&request,sizeof(requete));
|
|
|
|
memcpy(to_send+sizeof(requete),¶m,sizeof(int));
|
|
|
|
|
|
|
|
bytes = write(sock,to_send,sizeof(requete)+sizeof(int));
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
param = 3;
|
|
|
|
memcpy(to_send+sizeof(requete),¶m,sizeof(int));
|
|
|
|
|
|
|
|
bytes = write(sock,to_send,sizeof(requete)+sizeof(int));
|
|
|
|
|
2017-02-05 14:31:44 +00:00
|
|
|
if( bytes == -1 ){
|
2017-02-22 11:35:41 +00:00
|
|
|
perror("erreur envoi requete");
|
2017-02-01 18:11:53 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
/* [5] Wait for response
|
2017-02-01 18:11:53 +00:00
|
|
|
=========================================================*/
|
2017-02-05 11:08:02 +00:00
|
|
|
/* (1) Wait for response */
|
2017-02-22 11:35:41 +00:00
|
|
|
bytes = recv(sock, &to_recv, sizeof(long),0);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
/* (2) Check if received successfully (and consistently) */
|
2017-02-05 14:31:44 +00:00
|
|
|
if( bytes == -1 ){
|
2017-02-01 18:11:53 +00:00
|
|
|
perror("erreur réponse serveur");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) log */
|
2017-02-22 11:35:41 +00:00
|
|
|
printf("*** received : '%ld'\n", to_recv);
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
|
2017-02-05 21:17:48 +00:00
|
|
|
/* [6] Close socket
|
2017-02-01 18:11:53 +00:00
|
|
|
=========================================================*/
|
|
|
|
close(sock);
|
2017-02-22 11:35:41 +00:00
|
|
|
printf("Closing socket\n");
|
2017-02-01 18:11:53 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|