#include "client.h" void CLIENT_REQUEST(char* serverHost, char* serverPort, char* pRequest, char** pAnswer){ if( DEBUGMOD&HDR ) printf("====== CLIENT_REQUEST(%s, %s, %s, %s) ======\n\n", serverHost, serverPort, pRequest, *pAnswer); struct addrinfo hints; // contiendra le filtre/format struct addrinfo* addrinfo; // contiendra les infos int SOCKET, CONNECT; // file_desc(s) char BUFFER[maxBuffLen]; // BUFFER de communication // char pAnswer[maxBuffLen] = {0}; // on vide la réponse (contiendra la réponse du serveur) /* [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é 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"); /* [2] On récupère les infos =======================================================*/ getaddrinfo(serverHost, serverPort, &hints, &addrinfo); 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"); /* [3] Création de la socket =======================================================*/ SOCKET = socket(addrinfo->ai_family, addrinfo->ai_socktype, 0); if( DEBUGMOD&SCK ) printf("SOCKET = %d\n", SOCKET); // si erreur if( SOCKET == -1 ) return; /* [4] On établit la connection =======================================================*/ CONNECT = connect( SOCKET, addrinfo->ai_addr, addrinfo->ai_addrlen ); if( DEBUGMOD&SCK ) printf("CONNECT = %d\n", CONNECT); // si erreur if( CONNECT == -1 ) return; // on a plus besoin des infos de l'adresse freeaddrinfo(addrinfo); /* [5] On écrit sur la socket =======================================================*/ int nbSend = swrite(&SOCKET, pRequest); if( DEBUGMOD&BUF ) printf("nbSend: %d\n", nbSend); if( DEBUGMOD&BUF ) printf("Message: %s\n", pRequest); // si pas tout envoyé if( strlen(pRequest) != nbSend ) return; /* [6] On lit la réponse =======================================================*/ // memset(BUFFER, '\0', sizeof(BUFFER)); // on vide le buffer int nbRecup = sread(&SOCKET, BUFFER); *pAnswer = malloc( maxBuffLen ); strcpy(*pAnswer, BUFFER); if( DEBUGMOD&BUF ) printf("nbReceived: %d\n", nbRecup); if( DEBUGMOD&BUF ) printf("Message: %s\n", *pAnswer); /* [7] On ferme la connection =======================================================*/ int closeState = close(SOCKET); // if( closeState == -1 ) return; } void CONNECT_CLIENT(char* serverHost, char* serverPort, int* pSocket){ if( DEBUGMOD&HDR ) printf("====== INIT_CLIENT(%s, %s, %d) ======\n\n", serverHost, serverPort, *pSocket); struct addrinfo hints; // contiendra le filtre/format struct addrinfo* addrinfo; // contiendra les infos int CONNECT; // file_desc(s) char BUFFER[maxBuffLen]; // BUFFER de communication // char pAnswer[maxBuffLen] = {0}; // on vide la réponse (contiendra la réponse du serveur) /* [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é 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"); /* [2] On récupère les infos =======================================================*/ getaddrinfo(serverHost, serverPort, &hints, &addrinfo); 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"); /* [3] Création de la socket =======================================================*/ *pSocket = socket(addrinfo->ai_family, addrinfo->ai_socktype, 0); if( DEBUGMOD&SCK ) printf("SOCKET = %d\n", *pSocket); // si erreur if( *pSocket == -1 ) return; /* [4] On établit la connection =======================================================*/ CONNECT = connect( *pSocket, addrinfo->ai_addr, addrinfo->ai_addrlen ); if( DEBUGMOD&SCK ) printf("CONNECT = %d\n", CONNECT); // si erreur if( CONNECT == -1 ) return; // on a plus besoin des infos de l'adresse freeaddrinfo(addrinfo); } void CLIENT_SEND(int* pSocket, char* pRequest, char** pAnswer){ if( DEBUGMOD&HDR ) printf("====== CLIENT_SEND(%d, %s, %s) ======\n\n", *pSocket, pRequest, *pRequest); char BUFFER[maxBuffLen] = {0}; /* [5] On écrit sur la socket =======================================================*/ int nbSend = swrite(pSocket, pRequest); if( DEBUGMOD&BUF ) printf("nbSend: %d\n", nbSend); if( DEBUGMOD&BUF ) printf("Message: %s\n", pRequest); // si pas tout envoyé if( strlen(pRequest) != nbSend ) return; /* [6] On lit la réponse =======================================================*/ int nbRecup = WAIT_SOCKET_UPDATE(pSocket, BUFFER); *pAnswer = malloc( maxBuffLen ); strcpy(*pAnswer, BUFFER); if( DEBUGMOD&BUF ) printf("nbReceived: %d\n", nbRecup); if( DEBUGMOD&BUF ) printf("Message: %s\n", *pAnswer); }