Hi
My problem is I have various clients connecting at different times to a Panda II with Connect shield
I got inspiration from this example
[url]http://code.tinyclr.com/project/281/simple-tcp-server-with-fez-connect/[/url]
In my void Main() I have:
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
localEndPoint = new IPEndPoint(new IPAddress(Encoding.UTF8.GetBytes(ipAddress)), serverPort);
serverSocket.Bind(localEndPoint);
serverSocket.Listen(1);
while (true)
{
Socket clientSocket = serverSocket.Accept();
new ProcessClientRequest(clientSocket, nmeaReader1);
}
And my ProcessClientRequest
public ProcessClientRequest(Socket clientSocket, NmeaReader nmeaReader)
{
this.clientSocket = clientSocket;
this.nmeaReader = nmeaReader;
this.nmeaReader.SentenceRecieved += new NmeaReader.SentenceRecievedEventHandler(nmeaReader_SentenceRecieved);
Thread.Sleep(Timeout.Infinite);
}
void nmeaReader_SentenceRecieved(NmeaReader nmeaReader, NmeaReader.SentenceRecievedArgs sentenceRecievedArgs)
{
byte[] buf = sentenceRecievedArgs.Sentence;
this.clientSocket.Send(buf);
Debug.Print("Network send: " + new string(Encoding.UTF8.GetChars(buf)));
}
My problem is when a client disconnects that I can not kill the thread and close and free up the socket I think - when a client disconnects I get an execption like this:
A first chance exception of type ‘System.Exception’ occurred in GHIElectronics.NETMF.W5100.dll
An unhandled exception of type ‘System.Exception’ occurred in GHIElectronics.NETMF.W5100.dll
Additional information: Socket is closed
Uncaught exception
#### Exception System.Exception - 0x00000000 (5) ####
#### Message: Socket is closed
#### GHIElectronics.NETMF.Net.SocketNative::send [IP: 0062] ####
#### GHIElectronics.NETMF.Net.Sockets.Socket::Send [IP: 0029] ####
#### GHIElectronics.NETMF.Net.Sockets.Socket::Send [IP: 0010] ####
#### FEZ_Touch_Example.ProcessClientRequest::nmeaReader_SentenceRecieved [IP: 000f] ####
#### FEZ_Touch_Example.NmeaReader::InvokeSentenceRecieved [IP: 0015] ####
#### FEZ_Touch_Example.NmeaReader::SerialPortDataReceived [IP: 0076] ####
#### System.IO.Ports.SerialPort::DataEventHandler [IP: 0012] ####
I have tried to catch it and do Thread.CurrentThread.Abort() etc … what would be the right solution?