0
I have a socket client receiving xml messages that is working normally but in some moments it is necessary that the server sends me more than one message related to a request, and these messages are sent in a very small time interval something like hundredths and then these messages come all together which makes the client’s interpretation difficult. Is it possible that the socket can receive these separate messages ? My socket is asynchronous.
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
//state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
String received = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
if (received == keepAliveMsg)
{
Console.WriteLine("[" + DateTime.Now.ToString() + "] - " + "Keep Alive received.");
}
else
{
sendToXMLController(received);
//Console.WriteLine("[" + DateTime.Now.ToString() + "] - " + received + "\n");
}
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else {
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}