Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

threads, sockets, and synchronization

Status
Not open for further replies.

bardley

Programmer
May 8, 2001
121
0
0
US
Hi all.

I'm building kind of a hub-and-spokes type application with a central server and several remote clients that speak only to the server.

On a client, there are several (5-6) threads that all need to talk to the server at varying times. To make life easier, I made a SocketHandle class to wrap the socket operations, and each thread has a reference to the SocketHandle. The server only listens on one port, say 1234, and the threads can call the SendData method of the SocketHandle to send data to the server. Any thread that sends data also expects an acknowledgement from the server, and will listen on the open socket for a response for a certain time before it gives up and closes the socket.

Question 1: I built the SendData method as synchronized, so that only one socket would be open at a time. But, I assume (from my limited networking knowledge - hehe) that opening a socket would choose a different high-level port each time, so could every thread theoretically open a socket at once and the communications not interfere with each other? In other words, wouldn't the TCP sessions naturally make sure that the responses get sorted out to the correct senders? If so, do I need synchronization of the SendData method at all? (the point was to keep signals from getting crossed between threads)

Question 2: The clients will also need a server socket listening on the same port (say 1234) for the server to "push" data down every once in a while. Would that server socket interfere with the communications of the client threads? And can I guarantee that if a client is listening on 1234 that one of the threads won't choose 1234 to send on? Or does that matter?

Sorry so long-winded. Feel free to get me back by being long-winded in response :)

TIA,
Brad

Brad Gunsalus
bardley90@hotmail.com
 
Question 1:
The TCP/IP stack will keep communications across socket connections isolated for you.

Question 2:
Your design seems overly complex. Why have two connections for each client? Why not just one and the server can push data across it whenever you need it to?


-pete
[sub]I just can't seem to get back my IntelliSense[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top