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 / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Loss message on Socket Async mode

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rockdale - 17 Nov 2006 14:49 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:07 GMT
And please do not multipost.  If you want to post the same message to
multiple newsgroups, learn to cross-post correctly.

See my answer to this same message in m.p.d.framework.

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.