I am writing an application that uses asynchronous sockets to get data over ethernet from embedded devices, up to 30 concurrent devices.(These devices are written in C). My application implements an asychronous socket server while the embedded devices are the clients When the data comes in over the socket it is eventually passed into a message queue. A separate thread in the app is responsible for taking the data off of the queue and then processing it. This thread and/or others will need to be able to send data back to the distributed embedded devices. The problem i am having is how to achieve this. I'm not sure how these threads can pass data to or communicate with the asynchonous socket server to send data back to the clients. The MSDN example has the socket server immediately sending data back to all clients connected to it. (App is a chat spplication) This is not what i want to do though as I have explained above. I know there socket server has an asychronous send method. Do i use this some way? If so how do I use it with the other threads in my app? I'd appreciate any advice that anyone could give me. Thanks
Macca
Loading
GeoffPosted Jul 27, 2006, 8:06 PM
The first problem is, in an async socket environment any request that is made by a device/client is going to be made on a different thread then the response.
My client request and received methods looks like this
If you where to do it from a worker thread on the server you would have to pass the client socket into the queue with the message so that the reply goes back to the right client. (FYI: I have tried this method and could not get it to work consistantly)
///
/// Request an object from the server
///
/// Object that indicates to the server the desired response
/// The type for which the request should wait
/// Number of milliseconds the request should wait for a response (-1 indefinatly)
public object Request(object data, Type responseType, int millisecondsTimeout)
{
// Reset the notification service
receivedRequestedData.Reset();
this.expectedResponse = responseType;
// Send the data to the server
if (Send(data) == 0)
throw new ApplicationException("Unable to sent the request");
// Block the thread until a reply is received or the request timesout 5 seconds
// When the reply is received it will be set to the object this.response
if (!receivedRequestedData.WaitOne(millisecondsTimeout, false))
_receivedError++;
this.expectedResponse = null;
// Return the received object
return this.response; // Response is set by the OnDataReceived method
}
private void OnDataReceived(IAsyncResult asyncResult)
{
...
object data = Deserialize(objectBuffer);
if (data.GetType() == this.ExpectedResponseType)
{
this.response = data;
receivedRequestData.Set(); // Threading.AutoResetEvent
}
**** This is the line that causes the application to loose sync ****
**** If I comment out this line the application works flawlessly even @ a receipt every 100ms ****
TriggerEvent(DataReceived, this, new DataInterchangeEventArgs(data, length));
...
}