LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Detect TCP server drop

Is there a way for a TCP Client to determine or detect that a Server has dropped -- ie, cable disconnected, server application quit, etc? 

 

I have a client and server.  The client connects, then sends messages to the server, the server replies.  If the server drops, the client continues to send messages, using ClientTCPWrite, and does not get an error code in return.  Return value continues to be '0', as though there is success.

 

If I send a GetTCPPeerAddr(...) after the server is disconnected, the function returns '0' (success), and the IP address returned is the IP of the disconnected computer.

 

It does not look like there is anything in the TCP support library that can test the aliveness of a TCP Server.

 

0 Kudos
Message 1 of 8
(4,723 Views)

When you use ConnectToTCPServer function you need to pass callback function. and look for TCP_DISCONNECT event.

 

Here is help.

 

The name of the function to be called to process messages to your program as a TCP client.  A NULL pointer is not allowed.

The callback function must be of the following form:

    int (*tcpFuncPtr) (unsigned handle, int xType, int errCode,
                       void *callbackData);
 
The 'handle' parameter contains a value that is unique to each client-server connection.

The 'xType' parameter contains the TCP transaction type.  The following list specifies all of the transaction types that can be received by clients:

  TCP_DISCONNECT         
  TCP_DATAREADY          

The 'errCode' parameter is used only when the transaction type is TCP_DISCONNECT.

The following describes each transaction type.

TCP_DISCONNECT - Received when a server is requesting the termination of a connection, or when a connection is being terminated due to an error.  If the connection is being terminated due to an error, the 'errCode' parameter contains a negative error code.  Otherwise the 'errCode' parameter contains zero.  (See the help for the Status control for the list of error code values.)

TCP_DATAREADY - Received when the server has sent data via TCP to the client.  Your program, acting as the client, should call
ClientTCPRead to obtain the data.

Remarks:
--------
The return value of the callback function is ignored.

The callback function should be short and return as soon as possible.

Thanks.
CVI 2010
LabVIEW 2011 SP1
Vision Builder AI 2011 SP1
0 Kudos
Message 2 of 8
(4,720 Views)

test_man,

 

Apparently you did not read my post.  I have a client and server running properly on two computers, passing traffic.  I need to find a way for the client to detect that the server has dropped.  The client side TCP driver never issues a TCP_DISCONNECT event, and the TCPClientWrite function never returns an error, even if the Server network cable has been disconnecte.

 

Next idea?

0 Kudos
Message 3 of 8
(4,712 Views)

Hi hendra,

 

Actually, test_man is correct.

When the server drops/disconnects your client-side callback (which you pass to the connect function as a parameter) will automatically receive an event of type TCP_DISCONNECT.

I have used the same in a project of mine that is working in that sense.

 

If you put the following statement in the client callback, you will see what we mean:

 

switch (xType) 

{

case TCP_DATAREADY:

// code for handling data transfer from the server

break;

 

case TCP_DISCONNECT:

// code for handling server drop 

break: 

S. Eren BALCI
IMESTEK
0 Kudos
Message 4 of 8
(4,698 Views)

Eren and Test_man,

 

Pardon my prior terseness.  Perhaps I am trying to do something that the TCP driver is not capable of supporting..

 

I have the code to detect the TCP_DISCONNECT event in my callback.  It works fine if the Server disconnects the client.  It works fine if the Server application is quit, and the OS disconnects the client automatically.

 

It does NOT work if the Server computer network cable is disconnected, ie, some anomolous event.  If a cable is disconnected, the Client continues to believe it is connected, and there does not seem to be any way to detect that there is no Server out there.  Writing to the port appears successful all the time, even though the driver is writing to a disconnected cable.  It just seems to me there there ought to be some handshaking going on, so that a Write returns an error. 

 

The only thing I can think of here is to put a watchdog timer in the code, to wait some seconds after not hearing from the server, and then disconnect and try to reconnect.

 

 

0 Kudos
Message 5 of 8
(4,679 Views)

That can be a good approach.

 

You can send some "i-am-alive" packages from the server periodically.

Then the client can reset a timer at each alive package and rise and alert if the timer counts down to 0.

S. Eren BALCI
IMESTEK
0 Kudos
Message 6 of 8
(4,669 Views)

Hi,

I am new in LabVIEW and I am using TCP communication protocol to send mix Data(Numeric, boolean) from 5 client(5 PC) to the RT Target. We are sending different data from different Host PC to the RT target. I am using TCP listener in RT target and TCP open coneection in PCs. Here I am facing problem is that every time I have to run RT vi first and then host PCs VI. If I am running PC vi first I am getting error 63 occured at TCP open connection. Is there any solution for this?  Is there any possibility to communicate if I run the any one of the VI first?  

 

Also I am not able to make a connection , Disconnection and connect to all connection switch to connect the network(communication) between different PC to RT Target. If you have any idea about this, please let me know.

 

Thanks!

0 Kudos
Message 7 of 8
(4,163 Views)

Hello Srikant Kumar,

although this discussion addresses some TCP topics, it is strictly related to CVI language. Since you are using Labview, I suggest you post your questions to the appropriate forum.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 8 of 8
(4,156 Views)