Main Site Documentation

How to close and dispose of remote endpoint sockets FEZ Panda II and Connect


#1

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
http://code.tinyclr.com/project/281/simple-tcp-server-with-fez-connect/

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?


#2

hello,

I had also this problem with Fez domino; these small devices only supports 4 sockets and you will not be notified everytime a client close the connection. So we have a problem.

I solve my problem in the following way:
… in the ProcessRequests method(running in a different thread of the main program) …

private void ProcessRequests()
{
int bytesRead;
byte[] rxbuffer ;
string comStatus; // INOP, ALIVE

   using (m_clientSocket)
    {
     while (true)
       {
          try
          {
             // Espera pela recepão de um pedido do cliente
             if (!m_clientSocket.Poll(5* c_microsecondsPerSecond, SelectMode.SelectRead)) // wait 5secs 
           return;

the code youe need …

But all depends in the aplication you have; when you do the return and because of the ‘using (m_clientSocket)’ the socket will be closed,

I hope this can help you
Regards