Socket and ReadCallback
-
I have a problem with ReadCallback when I'm using sockets. Here's my problem: i'm writing an internet chat program. When user sends me a messages at large time intervals, everything is OK, but if he's flooding me, ReadCallback gets lots of calls. This causes that many Readcallback is calling and this causes overlapping messages in socket. Socket treats some messages as a one big message (they were sent as a separate messages). As a result of this the message is not easy readable... My ReadCallback begins with Monitor.Enter(RecQueue) and ends Monitor.Exit(RecQueue) [RecQueue = new Queue()]. How to force socket not to overlap multi messages into one? Here is my ReadCallback code: public void ReadCallback(IAsyncResult ar) { Monitor.Enter(RecQueue); StateObject state = (StateObject) ar.AsyncState; state.workSocket.Blocking = false; Socket handler = state.workSocket; handler.Blocking = false; int read; read = handler.EndReceive(ar); if (read > 0) //display a message { DisplayMessageProcedure("the message"); } else { //close the connection handler.Close(); } if (handler.Connected == true) { handler.BeginReceive(state.buffer,0,StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } Monitor.Exit(RecQueue); } public class StateObject { public Socket workSocket = null; public const int BufferSize = 1024; public byte[] buffer = new byte[BufferSize]; public StringBuilder sb = new StringBuilder(); } public void ListenProcedure() { IPAddress ipAd = IPAddress.Parse("0.0.0.0"); //all interfaces IPEndPoint localEP = new IPEndPoint(ipAd, System.Convert.ToInt32(PortEdit.Text)); sock = new Socket(localEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sock.Blocking = false; sock.Bind(localEP); sock.Listen(1); sock.BeginAccept(new AsyncCallback(AcceptCallback), sock); } HELP!!!