Click here to Skip to main content
15,888,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have developed a single server/multiple client TCP Application.

The client consists of x number of threads each thread doing processing on its own data and then sending the data over TCP socket to the Server for displaying.

The Server is basically a GUI having a window. Server receves data from the client and displays it.

Now, the problem is that since there are 40 threads inside the client and each thread wants to send data, how can I achieve this using one connected socket?

My Suggestion:

My approach was to create a data structure inside each of the 40 threads in which data to be sent will be maintained. A separate Send Thread with one connected socket on client side is then created. This thread will read data from data structure of first thread, send it over the socket and then read the data from second thread and so on.

Confusions:

but I am not sure how would this be implemented as I am new to all this? :( What if a thread is writing to data structure and the Send Thread tries to read the data at the same time. I am familiar with mutex, critical section etc but that sounds too complex for my simple application.

Any other suggestions/comments other than my own suggestion are welcome. If you think my own approach is correct then please help me solving my confusions that I mentioned above.

Thanks a lot in advance :)

Edit:

Can I put I timer on Send Thread and after a specific time the Send Thread suspends thread#1(so that it can access its data structure without any synchronization issues), reads data from its data structure, sends it over the tcp Socket, and resumes Thread#1 back, then it suspends Thread#2, reads data from its data structure, sends it over the tcp Socket, and resumes Thread#2 back and so on.
Posted

1 solution

Using one socket for many threads isn't best idea.
My suggestion:
Each client thread opens own connection, server thread is listening.
With this solution you can find the end of client thread with error (socket dies).
And you cannot distribute opened socket to client threads at start time.
JiMi
 
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