2015-12-14 20:48:19 +00:00
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
|
2015-12-16 20:38:39 +00:00
|
|
|
int CONNECT_CLIENT(char* serverHost, char* serverPort, int* pSocket){
|
|
|
|
if( DEBUGMOD&HDR ) printf("====== CONNECT_CLIENT(%s, %s, %d) ======\n\n", serverHost, serverPort, *pSocket);
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
struct addrinfo hints; // contiendra le filtre/format
|
|
|
|
struct addrinfo* addrinfo; // contiendra les infos
|
|
|
|
int CONNECT; // file_desc(s)
|
2015-12-16 20:38:39 +00:00
|
|
|
char BUFFER[maxBuffLen]; // BUFFER de communication
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
/* [1] On définit le filtre/format
|
|
|
|
=======================================================*/
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo)); // on vide le filtre
|
|
|
|
hints.ai_family = AF_UNSPEC; // Allow IPv4 ou IPv6
|
|
|
|
hints.ai_socktype = SOCK_STREAM; // TCP (SOCK_DGRAM = UDP)
|
|
|
|
hints.ai_flags = 0; // non spécifié
|
|
|
|
hints.ai_protocol = 0; // non spécifié
|
|
|
|
|
2015-12-16 10:02:05 +00:00
|
|
|
if( DEBUGMOD&SCK ) printf("============HINTS===========\n");
|
|
|
|
if( DEBUGMOD&SCK ) printf( "AI_FLAGS = %d\n", hints.ai_flags ); // int
|
|
|
|
if( DEBUGMOD&SCK ) printf( "AI_FAMILY = %d\n", hints.ai_family ); // int
|
|
|
|
if( DEBUGMOD&SCK ) printf( "AI_SOCKTYPE = %d\n", hints.ai_socktype ); // int
|
|
|
|
if( DEBUGMOD&SCK ) printf( "AI_PROTOCOL = %d\n", hints.ai_protocol ); // int
|
|
|
|
if( DEBUGMOD&SCK ) printf( "AI_ADDRLEN = %d\n", hints.ai_addrlen ); // int
|
|
|
|
if( DEBUGMOD&SCK ) printf("\n");
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] On récupère les infos
|
|
|
|
=======================================================*/
|
|
|
|
getaddrinfo(serverHost, serverPort, &hints, &addrinfo);
|
|
|
|
|
2015-12-16 10:02:05 +00:00
|
|
|
if( DEBUGMOD&SCK ) printf("=============RES============\n");
|
|
|
|
if( DEBUGMOD&SCK ) printf( "AI_FLAGS = %d\n", addrinfo->ai_flags ); // int
|
|
|
|
if( DEBUGMOD&SCK ) printf( "AI_FAMILY = %d\n", addrinfo->ai_family ); // int
|
|
|
|
if( DEBUGMOD&SCK ) printf( "AI_SOCKTYPE = %d\n", addrinfo->ai_socktype ); // int
|
|
|
|
if( DEBUGMOD&SCK ) printf( "AI_PROTOCOL = %d\n", addrinfo->ai_protocol ); // int
|
|
|
|
if( DEBUGMOD&SCK ) printf( "AI_ADDRLEN = %d\n", addrinfo->ai_addrlen ); // int
|
|
|
|
if( DEBUGMOD&SCK ) printf("\n");
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
/* [3] Création de la socket
|
|
|
|
=======================================================*/
|
|
|
|
*pSocket = socket(addrinfo->ai_family, addrinfo->ai_socktype, 0);
|
|
|
|
|
2015-12-16 10:02:05 +00:00
|
|
|
if( DEBUGMOD&SCK ) printf("SOCKET = %d\n", *pSocket);
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
// si erreur
|
2015-12-16 20:38:39 +00:00
|
|
|
if( *pSocket == -1 ) return -1;
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
/* [4] On établit la connection
|
|
|
|
=======================================================*/
|
|
|
|
CONNECT = connect(
|
|
|
|
*pSocket,
|
|
|
|
addrinfo->ai_addr,
|
|
|
|
addrinfo->ai_addrlen
|
|
|
|
);
|
|
|
|
|
2015-12-16 10:02:05 +00:00
|
|
|
if( DEBUGMOD&SCK ) printf("CONNECT = %d\n", CONNECT);
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
// si erreur
|
2015-12-16 20:38:39 +00:00
|
|
|
if( CONNECT == -1 ) return -1;
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
// on a plus besoin des infos de l'adresse
|
2015-12-16 20:38:39 +00:00
|
|
|
freeaddrinfo(addrinfo);
|
|
|
|
|
|
|
|
return *pSocket;
|
2015-12-14 20:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-16 23:34:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-16 20:38:39 +00:00
|
|
|
int CLIENT_SEND(int* pSocket, char* pRequest, char** pAnswer){
|
2015-12-16 10:11:11 +00:00
|
|
|
if( DEBUGMOD&HDR ) printf("====== CLIENT_SEND(%d, %s, %s) ======\n\n", *pSocket, pRequest, *pAnswer);
|
2015-12-16 10:02:05 +00:00
|
|
|
char BUFFER[maxBuffLen] = {0};
|
2015-12-14 20:48:19 +00:00
|
|
|
|
2015-12-16 23:34:24 +00:00
|
|
|
/* [1] On écrit sur la socket
|
2015-12-14 20:48:19 +00:00
|
|
|
=======================================================*/
|
|
|
|
int nbSend = swrite(pSocket, pRequest);
|
|
|
|
|
2015-12-16 10:02:05 +00:00
|
|
|
if( DEBUGMOD&BUF ) printf("nbSend: %d\n", nbSend);
|
|
|
|
if( DEBUGMOD&BUF ) printf("Message: %s\n", pRequest);
|
2015-12-14 20:48:19 +00:00
|
|
|
|
|
|
|
// si pas tout envoyé
|
2015-12-16 20:38:39 +00:00
|
|
|
if( strlen(pRequest) != nbSend ) return -1;
|
2015-12-14 20:48:19 +00:00
|
|
|
|
2015-12-16 23:34:24 +00:00
|
|
|
/* [2] On attends et lit la réponse
|
2015-12-14 20:48:19 +00:00
|
|
|
=======================================================*/
|
2015-12-14 23:02:56 +00:00
|
|
|
int nbRecup = WAIT_SOCKET_UPDATE(pSocket, BUFFER);
|
2015-12-14 20:48:19 +00:00
|
|
|
|
2015-12-16 23:34:24 +00:00
|
|
|
/* [3] On retourne la réponse par référence
|
|
|
|
=======================================================*/
|
2015-12-14 23:02:56 +00:00
|
|
|
*pAnswer = malloc( maxBuffLen );
|
2015-12-14 20:48:19 +00:00
|
|
|
strcpy(*pAnswer, BUFFER);
|
|
|
|
|
2015-12-16 10:02:05 +00:00
|
|
|
if( DEBUGMOD&BUF ) printf("nbReceived: %d\n", nbRecup);
|
|
|
|
if( DEBUGMOD&BUF ) printf("Message: %s\n", *pAnswer);
|
2015-12-14 20:48:19 +00:00
|
|
|
}
|