[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)

This commit is contained in:
xdrm-brackets 2017-04-20 11:49:11 +02:00
parent 095706b24f
commit b245e6f623
3 changed files with 15 additions and 16 deletions

View File

@ -37,8 +37,8 @@ int main(int argc, char* argv[]){
sgca.data = (struct plane*) malloc( sizeof(struct plane) ); sgca.data = (struct plane*) malloc( sizeof(struct plane) );
/* 2. Variables locales */ /* 2. Variables locales */
struct listen_arg tcp_listn_arg = { SERV_HOST, TCP_LIST, NULL, &managePlane }; 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_mcast_arg = { MCST_HOST, UDP_MCST, NULL, NULL };
struct listen_arg udp_vterm_arg = { MCST_VTER, UDP_VTER, &multicastTerminal, &manageViewTerm }; struct listen_arg udp_vterm_arg = { MCST_VTER, UDP_VTER, &multicastTerminal, &manageViewTerm };
struct listen_arg udp_cterm_arg = { MCST_CTER, UDP_CTER, &multicastTerminal, &manageCtrlTerm }; struct listen_arg udp_cterm_arg = { MCST_CTER, UDP_CTER, &multicastTerminal, &manageCtrlTerm };

View File

@ -39,14 +39,15 @@ void* managePlane(void* THREADABLE_ARGS){
int read, i, index, pindex; // compteurs int read, i, index, pindex; // compteurs
char buffer[MAX_BUF_LEN]; // buffer char buffer[MAX_BUF_LEN]; // buffer
struct plane data; // données de l'avion struct plane data; // données de l'avion
struct sockaddr_in clientInfo; // Info client int SOCKET; // Copie de la socket (évite les conflits de références)
socklen_t len;
/* 2. On récupère les arguments */ /* 2. On récupère les arguments */
struct handler_arg* arg = THREADABLE_ARGS; 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); 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" */ /* 3. On récupère le rang du thread parmi les "managers" */
index = -1; index = -1;
for( i = 0 ; i < MAX_TCP_THR ; i++ ) 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 /* (2) Récupération de la requête
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* 1. On lit sur la socket */ /* 1. On lit sur la socket */
len = sizeof(struct sockaddr_in); read = recv(SOCKET, buffer, MAX_BUF_LEN, 0);
read = recvfrom(arg->socket, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
/* 2.1. Si erreur reception (-1:erreur, 0:fermeture client propre) */ /* 2.1. Si erreur reception (-1:erreur, 0:fermeture client propre) */
if( read <= 0 ){ if( read <= 0 ){
@ -149,10 +149,7 @@ void* managePlane(void* THREADABLE_ARGS){
/* (5) Envoi de la réponse /* (5) Envoi de la réponse
---------------------------------------------------------*/ ---------------------------------------------------------*/
len = sizeof(struct sockaddr_in); read = send(SOCKET, buffer, PLANE_LEN/sizeof(char) + 1, 0);
read = sendto(arg->socket, buffer, PLANE_LEN/sizeof(char) + 1, 0, (struct sockaddr*) &clientInfo, len);
printf("send: %d\n", read);
@ -167,7 +164,7 @@ void* managePlane(void* THREADABLE_ARGS){
/* [6] Fermeture de la connection (SOCKET) /* [6] Fermeture de la connection (SOCKET)
=========================================================*/ =========================================================*/
printf("{tcp_com}(%d) Fermeture de la socket de communication!\n", index); printf("{tcp_com}(%d) Fermeture de la socket de communication!\n", index);
close(arg->socket); close(SOCKET);

View File

@ -21,14 +21,14 @@ struct control ctrl;
int mcast_socket = -1; int mcast_socket = -1;
int commu_socket = -1; int commu_socket = -1;
char buffer[MAX_BUF_LEN] = {0}; char buffer[MAX_BUF_LEN] = {0};
struct sockaddr_in sgca;
int open_communication(){ int open_communication(){
/* 0. Initialisation des variables */ /* 0. Initialisation des variables */
struct sockaddr_in udp, tcp; // données des sockets struct sockaddr_in udp; // données des sockets
char buffer[MAX_BUF_LEN] = {0}; // buffer char buffer[MAX_BUF_LEN] = {0}; // buffer
int status; int status;
struct bind_header request; struct bind_header request;
@ -70,6 +70,7 @@ int open_communication(){
// on indian-switch // on indian-switch
request.port = ntohs(request.port); 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); 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
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* 1. Création socket TCP + connection serveur */ /* 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 0;
return 1; return 1;
@ -131,10 +132,11 @@ void send_data(){
read = send(commu_socket, buffer, PLANE_DATA_LEN/sizeof(char) + 1, 0); read = send(commu_socket, buffer, PLANE_DATA_LEN/sizeof(char) + 1, 0);
if( read < 0 ){ if( read <= 0 ){
printf("Erreur d'envoi\n"); printf("Erreur d'envoi\n");
return; 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 */ /* 4. Récupération réponse */
read = recv(commu_socket, buffer, MAX_BUF_LEN, 0); read = recv(commu_socket, buffer, MAX_BUF_LEN, 0);