From b245e6f6232ecb5b314d42c68dee7b9d8b9cd153 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 20 Apr 2017 11:49:11 +0200 Subject: [PATCH] [fix] Fixed conflicts between TCP server sockets (at thread launch each have its own socket, then it becomes the same, reference err) -> so memcpy the socket at launch) --- central-manager/central-manager.c | 4 ++-- central-manager/lib/local/handler.c | 15 ++++++--------- plane/plane.c | 12 +++++++----- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/central-manager/central-manager.c b/central-manager/central-manager.c index 5a330f5..3dbe250 100644 --- a/central-manager/central-manager.c +++ b/central-manager/central-manager.c @@ -37,8 +37,8 @@ int main(int argc, char* argv[]){ sgca.data = (struct plane*) malloc( sizeof(struct plane) ); /* 2. Variables locales */ - struct listen_arg tcp_listn_arg = { SERV_HOST, TCP_LIST, NULL, &managePlane }; - struct listen_arg udp_mcast_arg = { MCST_HOST, UDP_MCST, NULL, NULL }; + struct listen_arg tcp_listn_arg = { SERV_HOST, TCP_LIST, NULL, &managePlane }; + struct listen_arg udp_mcast_arg = { MCST_HOST, UDP_MCST, NULL, NULL }; struct listen_arg udp_vterm_arg = { MCST_VTER, UDP_VTER, &multicastTerminal, &manageViewTerm }; struct listen_arg udp_cterm_arg = { MCST_CTER, UDP_CTER, &multicastTerminal, &manageCtrlTerm }; diff --git a/central-manager/lib/local/handler.c b/central-manager/lib/local/handler.c index 101d9b5..c331203 100644 --- a/central-manager/lib/local/handler.c +++ b/central-manager/lib/local/handler.c @@ -39,14 +39,15 @@ void* managePlane(void* THREADABLE_ARGS){ int read, i, index, pindex; // compteurs char buffer[MAX_BUF_LEN]; // buffer struct plane data; // données de l'avion - struct sockaddr_in clientInfo; // Info client - socklen_t len; + int SOCKET; // Copie de la socket (évite les conflits de références) /* 2. On récupère les arguments */ struct handler_arg* arg = THREADABLE_ARGS; + memcpy(&SOCKET, &arg->socket, sizeof(int)); if( DEBUGMOD&HDR ) printf("===== managePlane(%p, %p, %d, %p) =====\n", (void*) arg->managers, (void*) arg->activeManagers, arg->socket, (void*) arg->sgca); + /* 3. On récupère le rang du thread parmi les "managers" */ index = -1; for( i = 0 ; i < MAX_TCP_THR ; i++ ) @@ -64,8 +65,7 @@ void* managePlane(void* THREADABLE_ARGS){ /* (2) Récupération de la requête ---------------------------------------------------------*/ /* 1. On lit sur la socket */ - len = sizeof(struct sockaddr_in); - read = recvfrom(arg->socket, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len); + read = recv(SOCKET, buffer, MAX_BUF_LEN, 0); /* 2.1. Si erreur reception (-1:erreur, 0:fermeture client propre) */ if( read <= 0 ){ @@ -149,10 +149,7 @@ void* managePlane(void* THREADABLE_ARGS){ /* (5) Envoi de la réponse ---------------------------------------------------------*/ - len = sizeof(struct sockaddr_in); - read = sendto(arg->socket, buffer, PLANE_LEN/sizeof(char) + 1, 0, (struct sockaddr*) &clientInfo, len); - - printf("send: %d\n", read); + read = send(SOCKET, buffer, PLANE_LEN/sizeof(char) + 1, 0); @@ -167,7 +164,7 @@ void* managePlane(void* THREADABLE_ARGS){ /* [6] Fermeture de la connection (SOCKET) =========================================================*/ printf("{tcp_com}(%d) Fermeture de la socket de communication!\n", index); - close(arg->socket); + close(SOCKET); diff --git a/plane/plane.c b/plane/plane.c index c2c4aed..84fdd69 100644 --- a/plane/plane.c +++ b/plane/plane.c @@ -21,14 +21,14 @@ struct control ctrl; int mcast_socket = -1; int commu_socket = -1; char buffer[MAX_BUF_LEN] = {0}; - +struct sockaddr_in sgca; int open_communication(){ /* 0. Initialisation des variables */ - struct sockaddr_in udp, tcp; // données des sockets - char buffer[MAX_BUF_LEN] = {0}; // buffer + struct sockaddr_in udp; // données des sockets + char buffer[MAX_BUF_LEN] = {0}; // buffer int status; struct bind_header request; @@ -70,6 +70,7 @@ int open_communication(){ // on indian-switch request.port = ntohs(request.port); + // printf("* hex("); for( int i = 0 ; i < BINDHDR_LEN/sizeof(char) ; i++ ) printf("\\x%02X", buffer[i]); printf(")\n"); printf("* received bind_header{flags = %d; addr = %s; port = %d;}\n\n", request.flags, request.addr, request.port); @@ -83,7 +84,7 @@ int open_communication(){ /* (1) Création socket TCP + connection ---------------------------------------------------------*/ /* 1. Création socket TCP + connection serveur */ - if( TCP_CONNECT(&commu_socket, request.addr, request.port, &tcp) < 0 ) + if( TCP_CONNECT(&commu_socket, request.addr, request.port, &sgca) < 0 ) return 0; return 1; @@ -131,10 +132,11 @@ void send_data(){ read = send(commu_socket, buffer, PLANE_DATA_LEN/sizeof(char) + 1, 0); - if( read < 0 ){ + if( read <= 0 ){ printf("Erreur d'envoi\n"); return; } + // printf("* hex("); for( int i = 0 ; i < PLANE_DATA_LEN/sizeof(char) ; i++ ) printf("\\x%02X", buffer[i]); printf(")\n"); /* 4. Récupération réponse */ read = recv(commu_socket, buffer, MAX_BUF_LEN, 0);