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. Asynchronous Client/Server messages

Asynchronous Client/Server messages

Scheduled Pinned Locked Moved C#
helpsysadmindebuggingquestion
5 Posts 2 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.
  • M Offline
    M Offline
    methodincharge
    wrote on last edited by
    #1

    Yet again I am stuck. I've followed many code examples, most of which are wholly different and do not explain the small details I am lacking in my implementation. Basically what has happened is that, using the Async message sending I have implemented, the messages run together and mix data, which is obviously no good. I assumed that the Async BeginSend took care of this, but I guess I was wrong, either that or I did it wrong. The code below is my basic implementation: public void SendMessage(String msg) { try { if(m_sndCallBack == null) { m_sndCallBack = new AsyncCallback(OnDataSent); } SocketPacket packet = new SocketPacket(); packet.thisSocket = m_clientSocket; packet.dataBuffer = Encoding.UTF8.GetBytes(msg); m_clientSocket.BeginSend(packet.dataBuffer, 0, packet.dataBuffer.Length, SocketFlags.None, m_sndCallBack, packet); } catch(SocketException se) { Debug.WriteLine(se.Message); } } public void OnDataSent(IAsyncResult asyn) { SocketPacket theSockId = (SocketPacket)asyn.AsyncState; try { int iRx = theSockId.thisSocket.EndSend (asyn); theSockId.thisSocket = null; } catch (ObjectDisposedException ) { Debug.WriteLine("Web.OnDataReceived: Socket has been closed"); } catch(SocketException se) { Debug.WriteLine("Web.OnDataReceived: "+se.Message); } } Now my typical calling, which may be the problem? would be: ClientServer.SendMessage("CONNECT:username:password"); ... ClientServer.SendMessage("REQUEST:STATUS"); ClientServer.SendMessage("REQUEST:LIST"); and the server receives: CONNECT:username:passwordREQUEST:STATUS... Am I supposed to be waiting on the data to be sent? And if so, should I not even implement the sending using Async code? Any help would be greatly appreciated.

    S 1 Reply Last reply
    0
    • M methodincharge

      Yet again I am stuck. I've followed many code examples, most of which are wholly different and do not explain the small details I am lacking in my implementation. Basically what has happened is that, using the Async message sending I have implemented, the messages run together and mix data, which is obviously no good. I assumed that the Async BeginSend took care of this, but I guess I was wrong, either that or I did it wrong. The code below is my basic implementation: public void SendMessage(String msg) { try { if(m_sndCallBack == null) { m_sndCallBack = new AsyncCallback(OnDataSent); } SocketPacket packet = new SocketPacket(); packet.thisSocket = m_clientSocket; packet.dataBuffer = Encoding.UTF8.GetBytes(msg); m_clientSocket.BeginSend(packet.dataBuffer, 0, packet.dataBuffer.Length, SocketFlags.None, m_sndCallBack, packet); } catch(SocketException se) { Debug.WriteLine(se.Message); } } public void OnDataSent(IAsyncResult asyn) { SocketPacket theSockId = (SocketPacket)asyn.AsyncState; try { int iRx = theSockId.thisSocket.EndSend (asyn); theSockId.thisSocket = null; } catch (ObjectDisposedException ) { Debug.WriteLine("Web.OnDataReceived: Socket has been closed"); } catch(SocketException se) { Debug.WriteLine("Web.OnDataReceived: "+se.Message); } } Now my typical calling, which may be the problem? would be: ClientServer.SendMessage("CONNECT:username:password"); ... ClientServer.SendMessage("REQUEST:STATUS"); ClientServer.SendMessage("REQUEST:LIST"); and the server receives: CONNECT:username:passwordREQUEST:STATUS... Am I supposed to be waiting on the data to be sent? And if so, should I not even implement the sending using Async code? Any help would be greatly appreciated.

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      If your protocol demands that messages must be sent in a particular order, then you shouldn't use BeginSend. Each BeginSend will use a threadpool thread to run, so there is no guarantee as to the order in which the messages will actually be sent. Waiting till the callback completes before sending the next message will solve your problem. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      M 1 Reply Last reply
      0
      • S S Senthil Kumar

        If your protocol demands that messages must be sent in a particular order, then you shouldn't use BeginSend. Each BeginSend will use a threadpool thread to run, so there is no guarantee as to the order in which the messages will actually be sent. Waiting till the callback completes before sending the next message will solve your problem. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        M Offline
        M Offline
        methodincharge
        wrote on last edited by
        #3

        Where would the best place to implement the wait be? If only I could find GOOD async code to model my implementation hah... I am contemplating just writing synchronously and reading asynchronously because it doesnt really matter what order the messsages are received just that they are a single message. Thanks

        S 1 Reply Last reply
        0
        • M methodincharge

          Where would the best place to implement the wait be? If only I could find GOOD async code to model my implementation hah... I am contemplating just writing synchronously and reading asynchronously because it doesnt really matter what order the messsages are received just that they are a single message. Thanks

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          Is it OK if the server receives message like REQUEST:STATUSCONNECT:username:password... ?? That's what I meant by the ordering problem, your messages themselves could get mixed up. I'd typically implement this by creating my own thread and running a queue system there. All messages to be sent are posted to the queue and the queue synchronously processes them. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          M 1 Reply Last reply
          0
          • S S Senthil Kumar

            Is it OK if the server receives message like REQUEST:STATUSCONNECT:username:password... ?? That's what I meant by the ordering problem, your messages themselves could get mixed up. I'd typically implement this by creating my own thread and running a queue system there. All messages to be sent are posted to the queue and the queue synchronously processes them. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            M Offline
            M Offline
            methodincharge
            wrote on last edited by
            #5

            Thanks, works beautifully, I figured the problem was the Async sending but I wasn't sure.

            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