Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

Loss message in Socket Async mode

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rockdale - 17 Nov 2006 14:48 GMT
Hi, all

Did 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();
}
Peter Duniho - 17 Nov 2006 18:05 GMT
> [...]
> But I have a big issue with this approach and I am not what did I do
[quoted text clipped - 7 lines]
>
> 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
rockdale - 17 Nov 2006 20:03 GMT
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

> > [...]
> > But I have a big issue with this approach and I am not what did I do
[quoted text clipped - 31 lines]
>
> Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.