Reconnecting a TCPclient on a X2 Pro

Post Reply
pepijn_h
Posts: 6
Joined: Mon Sep 17, 2018 2:50 am

Reconnecting a TCPclient on a X2 Pro

Post by pepijn_h »

Hi iX,

I've created an HMI application for a X2 Pro 4 screen that communicates with a visual studio server running on a PC using a TCP connection. I wrote a script that connects to the server sets up a tcp client and a networkstream to read and write data to / from the server. All this works but now problems begin when disconnecting the client from the server using the following function:

Code: Select all

public void Disconnect()
		{
			if (tcpClient == null)		
			{	//Already disconnected
				return;
			}

			tcpClient.Close();
			
			GC.Collect(); //Not really needed because the class is a IDisposable

			//End listener thread 
			started = false;
		}
Using the tcpClient.Close should close the networkstream and socket of the tcpClient. To see if a client disconnected correctly I've created two events on the server side that show when a user connects or disconnects.
If I run the program on the simulator everything seems to work fine. I can connect and disconnect as many times as I want. But when the program is running on the X2 pro 4 the first time the user disconnects no disconnect event is triggered. The HMI program still runs and can even connect a second time without giving any errors.
Now the real problem occurs: The HMI freezes if the user once more disconnects and then connects. But now on the server side a disconnect event and connect event is triggered..

Is the HMI freezing because the first tcpclient isn't disconnected properly the first time? If so how can I disconnect the client properly? According to documentation the tcpClient.Close() should do the job.

Attached below is the connect to server code. (The listener thread is ended when the disconnect function is called by the bool started. )

Code: Select all

public bool ConnectToServer(string ipAddress, int port)
		{
			Disconnect();
			
			try
			{
				//Create a TCP client and a thread that listens for server messages
				tcpClient = new TcpClient(ipAddress, port);
				clientStream = tcpClient.GetStream();
				
				//started will signal the listener thread to start listening.
				started = true;
				listenerThread = new Thread(new ThreadStart(ListenForPackets));
				listenerThread.Start();

				
			}
			catch (ArgumentNullException e) 
			{
				Console.WriteLine("ArgumentNullException: {0}", e);
				return false;
			} 
			catch (SocketException e) 
			{
				Console.WriteLine("SocketException: {0}", e);
				return false;
			}
			catch (Exception e)
			{
				//Server not available right now, return false
				Console.WriteLine("Exception while connecting: {0}", e);
				return false;
			}
			
			if(tcpClient == null)
			{	
				Disconnect();
				return false;
			}
			return true;
		}
Kind regards and thanks in advance,

Pepijn

pepijn_h
Posts: 6
Joined: Mon Sep 17, 2018 2:50 am

Re: Reconnecting a TCPclient on a X2 Pro

Post by pepijn_h »

I solved one of the two problems! Because of TCP optimization some messages were added together and overflowed my Read-buffer length of the TCP client.. That was the reason for the screen freeze on the X2 and not on the simulator.
The other problem where the X2 doesn't disconnect according to the server is still unsolved. I've tested it using different clients and the server seems to notice all connects and disconnects of the different clients except for the first time the X2 disconnects. Some advice anyone?

AMitchneck
Posts: 137
Joined: Mon Jun 11, 2012 2:10 pm

Re: Reconnecting a TCPclient on a X2 Pro

Post by AMitchneck »

I haven't used a TCP client before, but for sockets before you call the close function it is recommended to call the socket.Shutdown(SocketShutdown.Both); method to handshake the closing of the socket. Maybe TCP client has something similar. I will note that sockets work differently on the compact framework for Windows CE (on the X2 platform) than the standard .net on your PC. For instance, on a PC I would usually call socket.Disconnect(false); after Shutdown(), but this method is not supported by the CF socket.
Adam M.
Controls Engineer
FlexEnergy

pepijn_h
Posts: 6
Joined: Mon Sep 17, 2018 2:50 am

Re: Reconnecting a TCPclient on a X2 Pro

Post by pepijn_h »

Thanks for the reaction Adam! I will look deeper into Windows CE and if there are differences in the TcpClient-Class and underlying classes in disconnecting compared to the normal frameworks for Windows.

Post Reply