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
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,So cketType.Stream ,ProtocolType.T cp );
// 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,iPo rtNo);
//connect to the remote host...
m_socClient.Con nect ( 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.Beg inReceive
(m_DataBuffer,0 ,m_DataBuffer.L ength,SocketFla gs.None,pfnCall Back,null);
}
public void OnDataReceived( IAsyncResult asyn)
{
//end receive...
int iRx = 0 ;
iRx = m_socClient.End Receive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Dec oder d = System.Text.Enc oding.UTF8.GetD ecoder();
int charLen = d.GetChars(m_Da taBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(c hars);
WaitForData();
}
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
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,So cketType.Stream ,ProtocolType.T cp );
// 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,iPo rtNo);
//connect to the remote host...
m_socClient.Con nect ( 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.Beg inReceive
(m_DataBuffer,0 ,m_DataBuffer.L ength,SocketFla gs.None,pfnCall Back,null);
}
public void OnDataReceived( IAsyncResult asyn)
{
//end receive...
int iRx = 0 ;
iRx = m_socClient.End Receive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Dec oder d = System.Text.Enc oding.UTF8.GetD ecoder();
int charLen = d.GetChars(m_Da taBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(c hars);
WaitForData();
}
Comment