Asynchronous Client/Server messages
-
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. -
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.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
-
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
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
-
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
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
-
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
Thanks, works beautifully, I figured the problem was the Async sending but I wasn't sure.