[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

@ -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);

View File

@ -21,13 +21,13 @@ 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
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);