LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

TCP Client Reconnect Issue

Solved!
Go to solution

Hi,

 

I am writing a GUI application in LabWindows that connects to a TCP/IP Server on a RT Target (PXIe). I have no issues if I connect on startup in the main function. However, I have a TCP check function that is called via async timer that checks the status of the connection every 10s, and reconnects if needed.  When reconnected via this callback, I seem to have issues with the TCP Callback not being called when data is sent from the server to this client. It seems to register successfully and I cant send data to the server, but the callback never gets invoked when receiving data. I'm not too experienced with callbacks/threading, so go easy on me if it's something obvious.

 

Here is sample code fragments to get an idea of what I'm doing essentially:

 

int main (int argc, char *argv[])
{

    ...

    ConnectToTCPServerEx (&gDataConnection, TARGET_PORT, TARGET_ADDRESS, TCPDataCallback, 0, 5000, TCP_ANY_LOCAL_PORT);

    NewAsyncTimer(10,-1,1,TCPIPCheckCallBack,0);

    ...

}

 

int CVICALLBACK TCPIPCheckCallBack(int reserved, int timerId, int event, void *callbackData, int eventData1, int eventData2)
{

    ...

    if (!gTCPConnected)
        (ConnectToTCPServerEx (&gDataConnection, TARGET_PORT, TARGET_ADDRESS, TCPDataCallback, 0, 5000, TCP_ANY_LOCAL_PORT);

    ...

}

 

0 Kudos
Message 1 of 4
(3,275 Views)
Solution
Accepted by pcscricky

For the callbacks to be called, you may have to process the eventd from the same thread that established the connection. Since async threads have their own separate thread, even if the main thread processes event it cannot detect the TCP event which is originated from the async timer's thread.

That may be why you dont have problems when you establish connection from the main thread.

You can either use standart user interface timer control to check connection status or use the PostDeferredCall function to assign the connection operation back to main thread.

Hope this helps..

S. Eren BALCI
IMESTEK
0 Kudos
Message 2 of 4
(3,255 Views)

Thanks, I ended up using a standard UI Timer control which essentially had the same behavior (checks every x seconds), and it worked like a charm.

0 Kudos
Message 3 of 4
(3,229 Views)

I have this same issue except the code is running on a real time target.  My RTMain function calls a function that connects to the server.  This works and my callback fires.  If I need to reconnect, the host program sends a TCP command on a different connection notifying the real time program to reconnect to a watchdog TCP server.  The connection is made, but the same callback I used in the original connection no longer fires.  The reconnect code is called from a thread because I don't want it to impact other time-critical loops, whereas the original connection is not made from a thread because it is done in the setup process with other loops aren't running.  What would I need to do to get around this?

 

 

int WatchdogReconnect(void)
{
	int Response = 0;
	
	Terminal_Writeline("User reconnected to Watchdog.");								
	ClientTCPWrite (WatchdogTCPConnectionHandle, "DISCONNECT\r", 11, 1000);			
	DisconnectFromTCPServer (WatchdogTCPConnectionHandle);
	SleepUS(20000);
	Response = ConnectToTCPServer (&WatchdogTCPConnectionHandle, Watchdog.Port, Watchdog.IP, WatchdogReceive, NULL, 5000);
	DebugPrintf("Response: %d\r\n", Response);
	WatchdogTCPConnected = 1;
	ClientTCPWrite (WatchdogTCPConnectionHandle, "STATE|Realtime Reconnected\r", 27, 1000);	
	return WatchdogTCPConnected;
}

 

Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 4 of 4
(246 Views)