Click to See Complete Forum and Search --> : Handling simultaneous connections


mambohoe81
January 19th, 2006, 12:07 PM
Besides the approach of using multithreading for multiple socket connections? Is there other way of handling it? I want to create a application where i can handle a few connections, like maximum 5 connections. I thought i may not need to use multithreading as i m a novice and i m not sure how multithreading will affect the rest of my application, so i was thinking if there is any other way out of this.

nkhambal
January 19th, 2006, 02:57 PM
Assuming your server uses TCP, you can use select() to handle multiple connections. Beej's guide has a sample code, showing how to do it. You can run select() on the server and add the server socket descriptor, you are listening on, in read FD_SET. As the select() returns, you can either,

1) Accept() new connections with accept() call which when returns, gives you a new socket decriptor for the new client. You need to again add this socket descriptor in the read FD_SET the select() is working on.

2) Or send/recv on one of the established connection's socket descriptor (returned by accept() in (1) ), which is set in read FD_SET, when select() returns.

In case, you are running a UDP server, I am afraid, you have to use threads as there is only one server socket and no client sockets since accept() won't work for UDP sockets. You can implement a fork() based solution but Ideally, thread based solution would work better since it has less overhead compared to fork(). You can run recvfrom() on the server socket and launch a new thread (up to a max limit as you want it) for everytime a recvfrom() returns. Implement an input queue for each thread to put the received packet from the given client for the thread to process. Similary implement an output queue.

Ofcourse since recvfrom() will return everytime a new packet is received on a the server socket, you have to implement some additional mechanism to check if the given client already has a thread running for it, by checking its source IP address and port.

You can also implment a complete queue based solution with UDP. Just keep one queue for all input packets and one queue for all output packets. Implement a dispatcher function which will process input queue in FIFO manner and update output queue for packets to be dispatched. The data structures for the queue will hold information related to the source and destination of the packet (IP address and Port). You need to run 3 threads max. One main thread running recvfrom() accepting the packets, validating and pushing them to the input queue. A second thread running on output queue, checking continuously if there are any packets to be send (using sendto()). And a Third thread running on both input and output queue as a dispatcher. It will check if there are any packets pending in the input queue to be processed. It proccess those packets and punts them to the output queue. The second thread mentioned above will then send these packets out. This solution will eliminate the need to launch one thread per client.


Thanks,