1
votes

In a client/server socket program, I'm trying to implement multiple clients in the same server. For that I'm trying to use threads. but when I try to connect with the third terminal to the server, it causes segmentation fault. I'd like to figure out why.

Code:

...
pthread_t thread[50];
int count =0;
...
while(1){
    connsockfd = accept(...);

    int *sock = malloc(sizeof(int));
    *sock = connsockfd;

    if (pthread_create(thread[count], NULL, &thread_sec, sock) != 0){
            exit(EXIT_FAILURE);
    }
    count++;


}
Please provive a minimal reproducible example and maybe someone can helpTed Lyngmo
Just for clarification: The previous poster meant to use the word "provide" instead of "provive".Andreas Wenzel
You are leaking memory by reusing sock a couple of times. That shouldn't be the problem. I'm guessing the real problem is inside thread_sec, but since you're not showing that, we can't really know, can we?Cheatah