Click here to Skip to main content
13,053,994 members (65,449 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
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.


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 :)


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 30-May-13 20:34pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 31 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100