Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Windows Threading threads , +
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? Frown | :( 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 Smile | :)
 
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 30-May-13 21:34pm

1 solution

Rate this: bad
good
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.
JiMi
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 480
1 Maciej Los 290
2 Richard MacCutchan 225
3 BillWoodruff 185
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411028.1 | Last Updated 31 May 2013
Copyright © CodeProject, 1999-2014
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