Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Socket and ReadCallback

Socket and ReadCallback

Scheduled Pinned Locked Moved C#
helpiosdata-structurestutorialquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Szymon Pusz
    wrote on last edited by
    #1

    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!!!

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups