Ok, here is the fix to make the samples work under C# 2005 without exceptions.
In the SocketServer project modify the SocketServer class as follows:
1. Add the following delegate declaration:
// This delegate enables asynchronous calls for setting
// the text property on a TextBox control.
delegate void AppendTextCallback(string text);
2. Add the following method:
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// AppendTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly.
private void AppendRxText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.txtDataRx.InvokeRequired)
{
AppendTextCallback d = new AppendTextCallback(AppendRxText);
this.Invoke(d, new object[] { text });
}
else
{
txtDataRx.Text = txtDataRx.Text + text;
}
}
3. Replace the OnDataReceived method with the following, or make the minor one line code change
as commented in the method below if you prefer:
public void OnDataReceived(IAsyncResult asyn)
{
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;
//end receive...
int iRx = 0 ;
iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
// Old offending line
//txtDataRx.Text = txtDataRx.Text + szData;
AppendRxText(szData);
WaitForData(m_socWorker );
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}