I am using the C# asynchronous socket functionality for a server, and
it appears to work fine if I just receive data and echo it back to the
client. The problem occurs when I try to handle sending data
independent of a receive. The send happens just fine, but when I
receive the next set of data from the client (in a size, size, data
set of packets), I lose however many packets of data I sent. For
example, a normal client send would include 3 separate packets of
data (p1-4 bytes, p2-4 bytes, p3-however many bytes are represented by
the size in p1). The server then echoes back the same data.
After sending one packet of data independent of receiving data, the
client sends the same 3 packets of data. However, the receive
callback is only entered twice.
It seems to be related to the beginReceive function, but I can't
figure out how. Prior to calling the sendTest, I have started a
beginReceive. Is this ended when I send data? I've tried adding a
beginReceive after the send, but this doesn't appear to help. Any
assistance would be greatly appreciated!
public void send(SocketData data)
{
SocketState ss = (SocketState)connectionTable[
data.getSocketHandle()];
send(ss, data.getXmlString());
clearSocketState(ref ss);
}
/// <summary>
/// This function creates a callback to send data.
/// </summary>
/// <param name="handler">The socket state from which to get
the
/// socket to send data.</param>
/// <param name="data">The data to send</param>
public void send(SocketState handler, string data)
{
// Convert the string data to byte data using big endian
encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
byte[] sizeBytes =
BitConverter.GetBytes((int)byteData.Length);
byte[] compressedBytes =
BitConverter.GetBytes((int)byteData.Length);
if(byteData.Length > compressionSize)
{
// compress the data
//recalculate the compressed byte length
compressedBytes =
BitConverter.GetBytes((int)byteData.Length);
}
byte[] sendBytes = new byte[byteData.Length +
sizeBytes.Length +
compressedBytes.Length];
// Add bytes for size and compressed size to the message
to be sent
sizeBytes.CopyTo(sendBytes, 0);
compressedBytes.CopyTo(sendBytes, sizeBytes.Length);
byteData.CopyTo(sendBytes, sizeBytes.Length +
compressedBytes.Length);
handler.buffer = sendBytes;
printBytes(sendBytes.Length, ref handler);
// Begin sending the data to the remote device.
handler.theSocket.Send(sendBytes);
//TODO: remove this after testing
Console.WriteLine("Sent {0} bytes to client.",
sendBytes.Length);
}
public void sendTest()
{
Console.WriteLine("sendTest entry point");
try
{
while(connectionTable.Count == 0)
{
// just spin here until there is a connection
}
IEnumerator ie = connectionTable.Keys.GetEnumerator();
ie.MoveNext();
SocketState ss =
(SocketState)connectionTable[ie.Current];
IntPtr si = ss.theSocket.Handle;
SocketData data = new SocketData(si,"testSend Data");
send(data);
data.setXmlString("testSend Data1");
// send(data);
data.setXmlString("testSend Data2");
// send(data);
}
catch (Exception e)
{
Console.WriteLine("Exception occurred: {0}, stack
trace {1}",
e.Message, e.StackTrace);
}
Console.WriteLine("Done");
}