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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. IAsyncResult : how to check a message is completed

IAsyncResult : how to check a message is completed

Scheduled Pinned Locked Moved .NET (Core and Framework)
tutorialsysadmindebuggingjsonhelp
3 Posts 3 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.
  • G Offline
    G Offline
    Gian
    wrote on last edited by
    #1

    Hello, i'm developing a client-server application and now i need to fix my asynchronously receive of messages because sometimes the complete message is not received on a single OnDataReceived call. I've tried for example to send a 18000 bytes long message with a 40000 bytes long receive buffer but there're 2 incoming packets of 13000 and 5000 bytes. My OnDataReceive function is this public void OnDataReceived(IAsyncResult asyn) { try { // SocketPacket is a class with the buffer of packet SocketPacket socketData = (SocketPacket)asyn.AsyncState ; int iRx = 0 ; // Complete the BeginReceive() asynchronous call by EndReceive() method // which will return the number of characters written to the stream // by the client iRx = socketData.m_currentSocket.EndReceive (asyn); if(iRx > 0) { // this get client object/class data (nickname, receive buffer for this client, socket) ClientSocketType client = GetClientSocket(socketData.m_currentSocket); if(client != null) { // create a new receiving buffer for this server/client receive if(client.PacketBuffer == null) client.PacketBuffer = new MemoryStream(); // append received buffers to that buffer BinaryWriter binwrite = new BinaryWriter(client.PacketBuffer); binwrite.Write(socketData.dataBuffer, 0, iRx); // if packets for this message are finished process message // ?!? WORKING ? if(asyn.IsCompleted) { // prepare the stream and send to a parsing function MemoryStream stream = new MemoryStream(client.PacketBuffer.GetBuffer(), 0, client.PacketBuffer.GetBuffer().Length,true, false); ProcessMessage(socketData.m_currentSocket, stream ); // empty client receive buffer client.PacketBuffer = null; } } // Continue the waiting for data on the Socket WaitForData( socketData.m_currentSocket ); } else { ((SocketPacket)asyn.AsyncState).m_currentSocket.Close(); RemoveSocketFromList(((SocketPacket)asyn.AsyncState).m_currentSocket); AddLogMessage("Disconnected user"); } } catch (ArgumentNullException se) { AddLogMessage(se.Message ); } catch (ObjectDisposedException ) { System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n"); } catch

    M 1 Reply Last reply
    0
    • G Gian

      Hello, i'm developing a client-server application and now i need to fix my asynchronously receive of messages because sometimes the complete message is not received on a single OnDataReceived call. I've tried for example to send a 18000 bytes long message with a 40000 bytes long receive buffer but there're 2 incoming packets of 13000 and 5000 bytes. My OnDataReceive function is this public void OnDataReceived(IAsyncResult asyn) { try { // SocketPacket is a class with the buffer of packet SocketPacket socketData = (SocketPacket)asyn.AsyncState ; int iRx = 0 ; // Complete the BeginReceive() asynchronous call by EndReceive() method // which will return the number of characters written to the stream // by the client iRx = socketData.m_currentSocket.EndReceive (asyn); if(iRx > 0) { // this get client object/class data (nickname, receive buffer for this client, socket) ClientSocketType client = GetClientSocket(socketData.m_currentSocket); if(client != null) { // create a new receiving buffer for this server/client receive if(client.PacketBuffer == null) client.PacketBuffer = new MemoryStream(); // append received buffers to that buffer BinaryWriter binwrite = new BinaryWriter(client.PacketBuffer); binwrite.Write(socketData.dataBuffer, 0, iRx); // if packets for this message are finished process message // ?!? WORKING ? if(asyn.IsCompleted) { // prepare the stream and send to a parsing function MemoryStream stream = new MemoryStream(client.PacketBuffer.GetBuffer(), 0, client.PacketBuffer.GetBuffer().Length,true, false); ProcessMessage(socketData.m_currentSocket, stream ); // empty client receive buffer client.PacketBuffer = null; } } // Continue the waiting for data on the Socket WaitForData( socketData.m_currentSocket ); } else { ((SocketPacket)asyn.AsyncState).m_currentSocket.Close(); RemoveSocketFromList(((SocketPacket)asyn.AsyncState).m_currentSocket); AddLogMessage("Disconnected user"); } } catch (ArgumentNullException se) { AddLogMessage(se.Message ); } catch (ObjectDisposedException ) { System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n"); } catch

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      The TCP stack will not necessarily deliver a whole message like this. You have to be prepared to join message fragments together. You also have to be prepared for multiple messages to be concatenated. For more information, read the Winsock Programmer's FAQ[^]. Stability. What an interesting concept. -- Chris Maunder

      V 1 Reply Last reply
      0
      • M Mike Dimmick

        The TCP stack will not necessarily deliver a whole message like this. You have to be prepared to join message fragments together. You also have to be prepared for multiple messages to be concatenated. For more information, read the Winsock Programmer's FAQ[^]. Stability. What an interesting concept. -- Chris Maunder

        V Offline
        V Offline
        vinu netguy
        wrote on last edited by
        #3

        Hi mike, Thanks for giving me such a valuable information. Vinu.netguy :-D :-D

        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