[Solved] How to create a server which creates a new thread for each client? [closed]


Basically, what you’re looking for is something like this :

#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>

void* handle_connection(void *arg)
{
    int client_sock = *(int*)arg;
    /* handle the connection using the socket... */
}

int main(void)
{
    /* do the necessary setup, i.e. bind() and listen()... */
    int client_sock;
    pthread_t client_threadid;
    while((client_sock = accept(server_sock, addr, addrlen)) != -1)
    {
        pthread_create(&client_threadid,NULL,handle_connection,&client_sock);
    }
}

This is a pretty basic skeleton for a server application which creates a different thread for every incoming client connection. If you don’t know what bind, listen, or accept is, then consult the second section of your local manual.

3

solved How to create a server which creates a new thread for each client? [closed]