Socket Connections
-
I've been reading about sockets for the past two days. I've got a few questions, mostly relating to the differences between synchronous and asynchronous behavior. I'm writing the following application: Data Server: Must be able to have multiple clients connected. Once a client has connected, it must be able to recieve data from that client and return data to that client at all times. It will need to be able to recieve and processing data from more than one client at a time. Data Client: The client simply connects, then sends data either at intervals (1-4 XML formated data strings per second), or manually when a user chooses. The data needs to be sent to the server (while connected) and then the client needs to wait for a response. From what I've read, using the TcpListener/TcpClient interface is synchronous: If there are multiple clients connected - if one client is sending data, another cannot be sending data. But if I use a threaded socket interface using asynchornous methods, then data from multiple clients can be recieved at the same time. Am I understanding this correctly? Can someone recommend a good solution for this? Do I need to use the asynchronous socket methods? Do I need to use threads? I've found many examples of tcp/client interfaces, but most of the authors don't explain the benefits/limitations of their code - so it's hard to know if I should base my work from what they're presenting. Does anyone know of a good tutorial that will meet my needs for this project? ... as always, Thanks for the help.
-
I've been reading about sockets for the past two days. I've got a few questions, mostly relating to the differences between synchronous and asynchronous behavior. I'm writing the following application: Data Server: Must be able to have multiple clients connected. Once a client has connected, it must be able to recieve data from that client and return data to that client at all times. It will need to be able to recieve and processing data from more than one client at a time. Data Client: The client simply connects, then sends data either at intervals (1-4 XML formated data strings per second), or manually when a user chooses. The data needs to be sent to the server (while connected) and then the client needs to wait for a response. From what I've read, using the TcpListener/TcpClient interface is synchronous: If there are multiple clients connected - if one client is sending data, another cannot be sending data. But if I use a threaded socket interface using asynchornous methods, then data from multiple clients can be recieved at the same time. Am I understanding this correctly? Can someone recommend a good solution for this? Do I need to use the asynchronous socket methods? Do I need to use threads? I've found many examples of tcp/client interfaces, but most of the authors don't explain the benefits/limitations of their code - so it's hard to know if I should base my work from what they're presenting. Does anyone know of a good tutorial that will meet my needs for this project? ... as always, Thanks for the help.
I have recently written something similar to you Data Server, and it works as follows. In the main thread I spawn a new thread which just listens (using
TcpListener
) for incoming TCP connections. When a new connection arrives theTcpListener
passes aSocket
object on the way out ofAcceptSocket
. I pass this socket to a ThreadPool worker thread which will handle the rest of the communication for that connection, meanwhile the listener thread loops back and waits for another socket connection. That provides a straight-foward multithreaded server approach - the server can communicate with several clients at the same time with each conversation in isolation from the others. As far as the client goes, that should be just straight forward socket stuff using eitherSocket
orTcpClient
. It's gone midnight here and I'm going to bed, but if you'd like me to send some sample source code through drop me a line and I'll do it tomorrow. Rob Philpott.