LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I programatically determine if a TCP port is still open

Solved!
Go to solution

I am using STM to pass communication between an HMI and a reat-time target. Everything works great unless the TCP connection is terminated on either end for whatever reason. How can I programitcally poll the port to see if the connection is still valid with out a read or write to the port?

Doug Ferguson
0 Kudos
Message 1 of 4
(4,665 Views)
Solution
Accepted by topic author DougFerguson

LabVIEW doesn't have any sort of TCP "polling" VI. You'd have to perform a TCP-related operation and get the expected error to indicate the connection has been lost. What did you want to do once you found out whether the connection is no longer valid? Did you want to open a new connection and start using that one? If so, the best way to handle this is by trapping error conditions within your code. If you get the "connection terminated" error you'd open a new connection and retry the operation. I don't know how your code is structured, so I couldn't really tell you the best way to implement this within your code.

0 Kudos
Message 2 of 4
(4,661 Views)

I was already doing that. Once I read/write I can then see if the connection is still valid and I reopen if it has closed. However, I was wondering if there was another way to poll the port without reading or writing to it.

Doug Ferguson
0 Kudos
Message 3 of 4
(4,651 Views)

I think you can do a read of 0 bytes, which should return immediately and return an error if the connection was properly closed.  I don't believe it will ever return a timeout error.  It will not tell you if the connection failed in some other way, like a disconnected network cable - in order for an error to occur there you need to try sending real data out.

0 Kudos
Message 4 of 4
(4,643 Views)