Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Is it possible to handle Multiple TCP Connections in a single thread?

Currently I am creating seperate threads for each TCP Connection. Data reading and writing for a particular connection is done in the respective threads. This application is meant to run on an embedded board[ARM9].

But we want to make the same application run on another board, that has a customer specific operating system which manages only single thread. By using the 'select' we were able to do it, but we are missing some of the request. Is there any way to handle these requests parallely.
Posted
Updated 7-Apr-10 10:47am
v4

Is it possible to handle Multiple TCP Connections in a single thread?


Absolutely... switch sockets into non-blocking mode and manage many connections within a single thread.
hSocket = socket(PF_INET, SOCK_STREAM, 0);
fcntl(hSocket, F_SETFL, O_NONBLOCK);

Then use a function to wait until one of your sockets changes status, something like select(). Since you mentioned ARM9 you need to check your SDK's documentation. If it supports normal socket functions (Berkeley style networking) there is a fair chance your embedded OS will support it too.

For more details and example source code how this could be done on a Linux/Windows platform see:
Beej's guide to networking programming (synchronous I/O multiplexing) and Winsock Programmer's FAQ (select-based server).

Hope this helps!
 
Share this answer
 
I'll recommend that you consider using a threadpool. Let the main thread accept connections, but the threadpool do the work.
 
Share this answer
 
Please ignore: This is a repost and I've wiped it off.
 
Share this answer
 
v3
Is there any other way to manage the TCP Connections, other than Thread Pool?
The application is going to run in an embedded board, that is having a custom operating system that can't manage more than one thread? But our application need to manage multiple connection also..
 
Share this answer
 
wrote:
The application is going to run in an embedded board, that is having a custom operating system that can't manage more than one thread? But our application need to manage multiple connection also..


You said in your original query that you are using one thread per connection. Now you say that you cannot create or manage more than one thread, which is highly contradictory. Please edit your original question (click on improve question) and include all relevant information.

DO NOT post anything related to your query as an answer!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900