handling multiple clients on c++ server

hello,

I have been programming c++ for about 5 years now
I Have recently started socket programming using the winsock2 library
I have run into a problem; handling multiple clients
take a look at this code
1
2
3
4
5
6
7
    while (listen(sock,SOMAXCONN)==SOCKET_ERROR); // while its not ready loop
    SOCKET client; //create socket
    int lin = sizeof(sin); 
    client = accept(sock,(sockaddr*)&sin,&lin); //accept the client
    char buf[200] = "testing\n";   
    send (client,buf,sizeof(buf),0);  //send the buffer to the client


I want my program to wait for clients and send them to another function which will send and receive data but at the same time i want it to keep looking for connections while its talking to sockets
How would i do this ?
Thank you in advance
accept() returns a new socket that represents the connection to one particular client.

You can process requests on this socket asyncronously with the rest of the program. This will allow you to communicate with the client in it's own time while freeing you to accept new connections.

This asyncrounous handling can be done in a seperate thread, or waiting on a set of sockets and processes each signalled socket one at a time, or a combination of these two methods.
Topic archived. No new replies allowed.