Reconnecting a TCPclient on a X2 Pro
Posted: Mon Sep 17, 2018 3:54 am
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:
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. )
Kind regards and thanks in advance,
Pepijn
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;
}
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;
}
Pepijn