Welcome Guest ( Login | Register )



All times are UTC - 7 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Reconnecting a TCPclient on a X2 Pro
PostPosted: Mon Sep 17, 2018 3:54 am 

Joined: Mon Sep 17, 2018 2:50 am
Posts: 4

Offline
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:
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:
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


 Profile  
 
 Post subject: Re: Reconnecting a TCPclient on a X2 Pro
PostPosted: Wed Sep 19, 2018 2:42 am 

Joined: Mon Sep 17, 2018 2:50 am
Posts: 4

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


 Profile  
 
 Post subject: Re: Reconnecting a TCPclient on a X2 Pro
PostPosted: Thu Sep 20, 2018 6:40 am 

Joined: Mon Jun 11, 2012 2:10 pm
Posts: 120

Offline
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


 Profile  
 
 Post subject: Re: Reconnecting a TCPclient on a X2 Pro
PostPosted: Tue Sep 25, 2018 8:14 am 

Joined: Mon Sep 17, 2018 2:50 am
Posts: 4

Offline
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.


 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 7 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: