Loss message on Socket Async mode

R

rockdale

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();
}
 
P

Peter Duniho

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top