I applied the Server application for this publication here: http://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c8781#Client1
Amount: I use async Sockets ala BeginAccept (..), BeginReceive (..). My server is capable of handling multi-tenant clients, and everything works fine until the client performs two or more synchronous send operations, without waiting for some time. The client does not receive any errors and therefore is not notified that the server is not receiving a second message! If the client waits approx. 100 ms after the first send operation, everything works fine. I thought that when I use TCP, I can guarantee that the server will receive a message. (Except that there is an exception)! Could you give me a solution to fix this.
Here are the WaitForData (..) and OnDataReceive (..) methods that I implemented on the server
public void WaitForData(MyClient client)
{
try
{
if (pfnCallBack == null)
{
pfnCallBack = new AsyncCallback(OnDataReceived);
}
iarResult = client.Socket.BeginReceive(client.DataBuffer,
0, client.DataBuffer.Length,
SocketFlags.None,
pfnCallBack,
client);
}
catch (SocketException se)
{
MessageBox.Show("SocketException@WaitForData" + se.Message);
}
}
public void OnDataReceived(IAsyncResult asyn)
{
try
{
MyClient user= (MyClient)asyn.AsyncState;
int iRx = user.Socket.EndReceive(asyn);
byte[] receivedData = user.DataBuffer;
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(receivedData, 0, receivedData.Length);
memStream.Seek(0, SeekOrigin.Begin);
MyMessage msg = (MyMessage)binForm.Deserialize(memStream);
switch (msg.Command)
{
case (MyMessage.MyCommand.ConnId):
this.connId = (int) msg.MyObject;
tsslConnStatus.Text += " | ID: " + connId.ToString();
break;
case (MyMessage.MyCommand.Text):
MessageBox.Show(msg.MyObject.ToString());
break;
}
WaitForData(server);
}
catch (ObjectDisposedException ode)
{
MessageBox.Show("ObjectDisposedException@OnReceiveData" + ode.Message);
}
catch (SocketException se)
{
MessageBox.Show("SocketException@OnReceiveData" + se.Message);
}
}
CLIENT calls the synchronous SEND METWOD TWICE method or MORE! INSTANCEOF MyClient server
if (server.Socket.Connected)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, message);
MyMessage = new MyMessage(something);
server.Socket.Send(ms.ToArray());
}
, , , , !
, , !
Thanx!