.net sockets

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
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
M

Mike P

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
 
M

Mike P

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
 

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