M
Mike P
I am using .Net Socket object to send and receive data between machines
(I am following the example from
http://www.developerfusion.com/show/3997/2/). However, once data has
been received, I need to raise an event to check what the data contains.
In my old VB6 code I was using a timer to do this. But trying to call
any sort of event after data has been received doesn't seem to work.
Here is my code :
private void OnDataReceived(IAsyncResult asyn)
{
//try
//{
if (blnDataFound == true)
{
tmrFindData.Enabled = false;
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
//end receive
int iRx = 0 ;
iRx = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
Decoder d = Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
String szData = new String(chars);
txtOutput.Text += szData;
WaitForData();
tmrDataSent.Start();
// tmrTimeOut.Start();
}
//}
//catch
//{
//error
//}
}
private void WaitForData()
{
try
{
if (pfnWorkerCallBack == null )
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket();
theSocPkt.thisSocket = socClient;
//now start to listen for any data
socClient.BeginReceive(theSocPkt.dataBuffer, 0,
theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack,
theSocPkt);
}
catch
{
//error
}
}
Any assistance would be really appreciated.
Cheers,
Mike
(I am following the example from
http://www.developerfusion.com/show/3997/2/). However, once data has
been received, I need to raise an event to check what the data contains.
In my old VB6 code I was using a timer to do this. But trying to call
any sort of event after data has been received doesn't seem to work.
Here is my code :
private void OnDataReceived(IAsyncResult asyn)
{
//try
//{
if (blnDataFound == true)
{
tmrFindData.Enabled = false;
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
//end receive
int iRx = 0 ;
iRx = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
Decoder d = Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
String szData = new String(chars);
txtOutput.Text += szData;
WaitForData();
tmrDataSent.Start();
// tmrTimeOut.Start();
}
//}
//catch
//{
//error
//}
}
private void WaitForData()
{
try
{
if (pfnWorkerCallBack == null )
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket();
theSocPkt.thisSocket = socClient;
//now start to listen for any data
socClient.BeginReceive(theSocPkt.dataBuffer, 0,
theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack,
theSocPkt);
}
catch
{
//error
}
}
Any assistance would be really appreciated.
Cheers,
Mike