|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Loss message in Socket Async modeDid not find a .Net Communication forum to post. I am soing a client/server message system and using C#.net socket and Async mode. Pretty much as described in article http://www.developerfusion.co.uk/show/3997/3/ But I have a big issue with this approach and I am not what did I do wrong. the client send message so quick that when I loss the second message. I need to call endReceive in my Callback function and process the first message, now the second message is arrived and since I have not call beginReceive, then, I loss the second message. Am I doing wrong or is that unavoidable? Thanks in advance -rockdale -------------------------------------------------------------- source code from the article is attached. byte[] m_DataBuffer = new byte [1024]; IAsyncResult m_asynResult; public AsyncCallback pfnCallBack ; public Socket m_socClient; // create the socket... public void OnConnect() { m_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp ); // get the remote IP address... IPAddress ip = IPAddress.Parse ("10.10.120.122"); int iPortNo = 8221; //create the end point IPEndPoint ipEnd = new IPEndPoint (ip.Address,iPortNo); //connect to the remote host... m_socClient.Connect ( ipEnd ); //watch for data ( asynchronously )... WaitForData(); } public void WaitForData() { if ( pfnCallBack == null ) pfnCallBack = new AsyncCallback (OnDataReceived); // now start to listen for any data... m_asynResult = m_socClient.BeginReceive (m_DataBuffer,0,m_DataBuffer.Length,SocketFlags.None,pfnCallBack,null); } public void OnDataReceived(IAsyncResult asyn) { //end receive... int iRx = 0 ; iRx = m_socClient.EndReceive (asyn); char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(m_DataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); WaitForData(); }
Show quote
"rockdale" <rockdale.gr***@gmail.com> wrote in message It's not only not unavoidable, it's impossible. When you are using TCP, news:1163774892.010384.35690@m73g2000cwd.googlegroups.com... > [...] > But I have a big issue with this approach and I am not what did I do > wrong. > > the client send message so quick that when I loss the second message. > > I need to call endReceive in my Callback function and process the first > message, now the second message is arrived and since I have not call > beginReceive, then, I loss the second message. > > Am I doing wrong or is that unavoidable? delivery of the data, in the correct order, is guaranteed unless some error occurs on the network connection. I think it's more likely that you don't understand that TCP doesn't know anything about "messages". This is probably the number one mistake people new to network programming make, especially those who haven't read the documentation carefully. TCP is a byte stream. You put bytes in one end, they come out the other. There is no guarantee that the bytes will be "chunked" in the same arrangement when receiving as they were when sending. They will always be in the correct order, but the divisions between the groups of data may be in different places, or removed altogether, or it may even happen that new divisions are inserted. In your case, probably the first and second transmissions are being combined into a single transmission before being sent. Then your receiving code receives them both at the same time. Given that I see nothing in your receiving code that attempts to figure out where one ends and the next starts, this seems especially likely to me. Pete Thanks for the reply and sorry for the double posts.
Yes, I am very new to network programming. And the problem I have is exactly what you suspected. I read the bytes based on my msg size (stored in the 8th byte in my message header) and then I stopped, I should continue to read the rest bytes since the second message is just followed as you said. Thanks -rockdale Peter Duniho wrote: Show quote > "rockdale" <rockdale.gr***@gmail.com> wrote in message > news:1163774892.010384.35690@m73g2000cwd.googlegroups.com... > > [...] > > But I have a big issue with this approach and I am not what did I do > > wrong. > > > > the client send message so quick that when I loss the second message. > > > > I need to call endReceive in my Callback function and process the first > > message, now the second message is arrived and since I have not call > > beginReceive, then, I loss the second message. > > > > Am I doing wrong or is that unavoidable? > > > It's not only not unavoidable, it's impossible. When you are using TCP, > delivery of the data, in the correct order, is guaranteed unless some error > occurs on the network connection. > > I think it's more likely that you don't understand that TCP doesn't know > anything about "messages". This is probably the number one mistake people > new to network programming make, especially those who haven't read the > documentation carefully. > > TCP is a byte stream. You put bytes in one end, they come out the other. > There is no guarantee that the bytes will be "chunked" in the same > arrangement when receiving as they were when sending. They will always be > in the correct order, but the divisions between the groups of data may be in > different places, or removed altogether, or it may even happen that new > divisions are inserted. > > In your case, probably the first and second transmissions are being combined > into a single transmission before being sent. Then your receiving code > receives them both at the same time. Given that I see nothing in your > receiving code that attempts to figure out where one ends and the next > starts, this seems especially likely to me. > > Pete |
|||||||||||||||||||||||