.net sockets

  • Thread starter Thread starter Mike P
  • Start date Start date
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
 
Mike,

I'm not quite sure why you have to use a timer. Since you are catching
an event that indicates that data has come in, why not just kick of the
processing there?
 
Nicholas,

The data that is being returned with usually be 3 or 4 separate strings
of about 100 characters, all sent shortly after each other (it's
basically a credit card authorisation and it sends strings telling you
what it is doing before the final string which tell you the outcome of
the transaction). I've changed the OnDataReceivedMethod by removing the
WaitForData call and putting in a call to my own procedure to look at
the string that has been sent. However, when I do this I get the error
'System.Threading.ThreadStopException : thread was being stopped'. Do I
need some sort of test to see if data is still being received before I
call my method?


Thanks,

Mike
 
Nicholas,

It seems to work OK when I send a string to my Test method rather than
trying to manipulate the value in the text box. Would this sort out the
problem? I have no experience of using delegates or threading yet, so
if I can avoid having to use them, I would like to do that.


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


Test(szData);
//tmrDataSent.Start();
// tmrTimeOut.Start();


}
//}
//catch
//{
//error
//}
}

Thanks,

Mike
 
Back
Top