LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I check that the TCP server, the client is connecting to has gone down?

I'm trying to write a routine on the client side which tries to reconnect when the TCP server goes down.
I am using the Lab Windows TCP functions.
Currently I'm checking for a less than 0 value returned on a ClientTCPRead call, but instead I see that when the TCP server goes down, my program just waits and does nothing. I have set my read timeout to 2 seconds.
Any suggestions would be greatly appreciated!
Thanks.
0 Kudos
Message 1 of 6
(5,140 Views)
If your TCP Server closes connection, the TCP Callback function you registered while connecting to the server gets automatically an event

//Connection to the server:
iErr = ConnectToTCPServer (&g_hconversation_client, iTelnetPortNumber, sIPAdresse, ClientTCPCB,
NULL, 5000) ;


// your CB function:
int CVICALLBACK ClientTCPCB (unsigned handle, int event, int error,
void *callbackData)
{
switch (event)
{
...
case TCP_DISCONNECT:
//your reacting code
sprintf receiveBuf, "TCP_DISCONNECTeceived. Server has closed connection.");
0 Kudos
Message 2 of 6
(5,141 Views)
That is exactly how I'm handling for a disconnect from the server, but for some reason, the client never gets the TCP_DISCONNECT event.

Is there a known bug in LabWindows CVI 6.0 ?
And also would there be any workaround to it? If I were using simple socket library functions I could just check if recv() returned a 0, but by using LabWindows functions i dont know if that would work..

Thanks!!
0 Kudos
Message 3 of 6
(5,140 Views)
Hi,
I am doing exactly same thing what you are trying to do. Here are my functions.

int CVICALLBACK Auto_Connect_To_Server(void *functionData)//This is a thread.
{
int status;
do
{
status = NC_Connect_To_Server(Server_name, Client_name);
ProcessSystemEvents();
}while(status != 0);//wait until connected

return 0;
}


int NC_Connect_To_Server(char *server_name, char *client_name)
{
int status;
DisableBreakOnLibraryErrors();

memcpy (Server_name, server_name, strlen(server_name));
memcpy (Client_name, client_name, strlen(client_name));

status = ConnectToTCPServer (&handle, PORT_NUMBER, Server_name, NC_ClientCB, 0, TIMEOUT);
if(status == 0)
{
return NC_PASS;
// Connected = 1;
}
else

{
return NC_FAIL; //Error occured
}

}
Thanks.
CVI 2010
LabVIEW 2011 SP1
Vision Builder AI 2011 SP1
0 Kudos
Message 4 of 6
(5,140 Views)
Sir,

I am using LabVIEW instead of LabVIEW CVI.

I am having one Server and one Client VI. I need to include an option such that if I start the client VI first, it should check whether the server is ON. If no, I should stop executing telling that Server is in OFF condition. How can i do it in LabVIEW using TCP-IP functions.

Kindly clear my doubt.

Thanks,
Ramkumar. D
Engineer.
Thanks,
Ramkumar.D
QuEST, INDIA.
0 Kudos
Message 5 of 6
(5,041 Views)
Hi Ramkumar,

If you want the Client to check if the Server is running, just set a timeout value for the "TCP Open Connection.vi" that is the maximum you want to try to establish a connection to the Server. If the server is not running, you will receive an error 63 (The network connection was refused by the server).

Have fun!
- Philip Courtois, Thinkbot Solutions

Thinkbot Solutions
0 Kudos
Message 6 of 6
(5,022 Views)