dgamma3
dgamma3

Reputation: 2371

create socket/thread in a loop

I am struggling to try to create sockets via a for loop; the loop will create a thread for each socket.

this is my current code which works just fine:

input; ./client1 2344 or ./client1 2343. (2344 , and 2343 are ports).

to handle to sockets, with a thread each:

    struct sockaddr_in serv; /* socket info about our server */
    struct sockaddr_in serv2; /* socket info about our server */
    int mysocket;            /* socket used to listen for incoming connections */
    int mysocket2;

    memset(&serv, 0, sizeof(serv));           /* zero the struct before filling the fields */
    serv.sin_family = AF_INET;                /* set the type of connection to TCP/IP */
    serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
    serv.sin_port = htons(PORTNUM);          
    memset(&serv2, 0, sizeof(serv2));           /* zero the struct before filling the fields */
    serv2.sin_family = AF_INET;                /* set the type of connection to TCP/IP */
    serv2.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
    serv2.sin_port = htons(PORTNUM2);           /* set the server port number */
    // see http://stackoverflow.com/questions/15976137/local-host-server-socket-program-not-working

    mysocket = socket(AF_INET, SOCK_STREAM, 0);    
    mysocket2 = socket(AF_INET, SOCK_STREAM, 0);

    int optVal = 1;
    if (setsockopt(mysocket, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(int)) < 0) {
        perror("Error setting socket option");
        exit(1);
    }    
    if (setsockopt(mysocket2, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(int)) < 0) {
        perror("Error setting socket option");
        exit(1);
    }


    if (bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr_in))){
        perror("Error binding socket to port");
        exit(1);
    }
    if (bind(mysocket2, (struct sockaddr *)&serv2, sizeof(struct sockaddr_in))){
        perror("Error binding socket to port");
        exit(1);
    }
    /* start listening, allowing a queue of up to 1 pending connection */
    listen(mysocket, SOMAXCONN);
    listen(mysocket2, SOMAXCONN);

    pthread_t inc_x_thread1;
    pthread_t inc_x_thread2;

    pthread_create(&inc_x_thread1, NULL, run, &mysocket);
    pthread_create(&inc_x_thread2, NULL, run, &mysocket2);
    pthread_join(inc_x_thread1, NULL);
    pthread_join(inc_x_thread2, NULL);

the following code is my attempt at re factoring the code, in a for loop:

    int socketDeckPair = 0;
    int threads = 2; 
    pthread_t* tid[2];
    char* ports[] = {"2343", "2344"};

    //This is where the information about the incoming connection will
    for(;socketDeckPair < 2; socketDeckPair=socketDeckPair+1){
        struct sockaddr_in serv; /* socket info about our server */

        int mysocket;            /* socket used to listen for incoming connections */

        memset(&serv, 0, sizeof(serv));           /* zero the struct before filling the fields */
        serv.sin_family = AF_INET;                /* set the type of connection to TCP/IP */
        serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
        serv.sin_port = htons(atoi(ports[socketDeckPair]));           /* set the server port number */

        mysocket = socket(AF_INET, SOCK_STREAM, 0);

        int optVal = 1;
        if (setsockopt(mysocket, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(int)) < 0) {
            perror("Error setting socket option");
            exit(1);
        }

        if (bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr_in))){
            perror("Error binding socket to port");
            exit(1);
        }

        listen(mysocket, SOMAXCONN);

        pthread_create(&tid[socketDeckPair], NULL, run, &mysocket);
    }
    int i = 0;
    for(; i < threads; i++){
        pthread_join(tid[i], NULL);
    }

now if i were to try run ./client1 2344, I get strange results. every time I run the ./client1 the thread id's alternate:

f77fe700
f6dfd700
f77fe700
f6dfd700
f77fe700

What am I doing wrong?

thanks daniel

Upvotes: 1

Views: 854

Answers (1)

whoan
whoan

Reputation: 8531

  • Problem 1:

    pthread_t* tid[2];
    

    must be:

    pthread_t tid[2];
    

  • Problem 2:
    In the following line inside the for loop:

    pthread_create(&tid[socketDeckPair], NULL, run, &mysocket);
    

    you are using the local variable mysocket which goes out of scope when the code reachs the next } (i.e.: the next line).
    You are passing the address of the variable, the one which became obsolete in the next }

Upvotes: 2

Related Questions