Home > Blockchain >  concurrent server threads - giving binding error
concurrent server threads - giving binding error

Time:05-23

I am trying to create two threads that can listen at two different ports.

My logic is:

#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdbool.h>

typedef struct server_arg {
    int portNum;
} server_arg; 

void  *server_socket_creation(void *arg) {

server_arg *s = (server_arg*)arg;
int server_fd, new_socket;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    
 
    // Creating socket file descriptor
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))
        == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }
        int enable = 1;
        if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0) {
          perror("error");
        }
 
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(s->portNum);
 

    if (bind(server_fd, (struct sockaddr*)&address,
             sizeof(address))
        < 0) {
        perror("bind failed");
        
    }
    if (listen(server_fd, 3) < 0) {
        perror("listen");
        
    }
    if ((new_socket
         = accept(server_fd, (struct sockaddr*)&address,
                  (socklen_t*)&addrlen))
        < 0) {
        perror("accept");
    }
   
    printf("Server Connected\n");
    

}

int main(int argc, char const* argv[])
{
  

  server_arg *s = (server_arg*)malloc(sizeof(server_arg));
  pthread_t id_1;
  pthread_t id_2;
  s->portNum = htons(9191);
  pthread_create(&id_1,NULL,(void *)server_socket_creation,(void *)s);
  s->portNum = htons(6123);
  pthread_create(&id_2,NULL,(void *)server_socket_creation,(void *)s);
  pthread_detach(id_1);
  pthread_detach(id_2);
  
  
  
  
  pthread_exit(0);
}

error: binding error: Address Already in use

Now, after searching about this issue on stackoverflow:

I could find 2 reasons:

  1. Ports are already in use. (Means using same ports in this case) which is not the case here as you can see.
  2. server IP address is same. I think this is the issue here, but I can't think of any way to solve this.

Any suggestions would be appreciated.

CodePudding user response:

You have a single server_arg structure. You pass a pointer to this single server_arg structure to both threads.

There's a very significantly large chance that the assignment

s->portNum = htons(6123);

will happen before the first thread have copied the value with

address.sin_port = htons(s->portNum);

This is what is called a data-race. And as any data-race, the only looser is you as the programmer.

You need two structures, one for each thread. For example as an array:

server_arg s[2[ = {
    { 9191 },
    { 6123 }
};

Then pass &s[0] to the first thread, and &s[1] to the second.


There's also another problem with the port number, which should be quite easily seen when you put both the port assignments close to each other like I did above: You call htons twice.

  • Related