LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Value of the 'conversation Handle' parameter in ConnectToTCPServerEx

Solved!
Go to solution

Hello,

 

For connecting to a TCP server, the function ConnectToTCPServerEx returns a conversation handle (unsigned int) for ongoing communication with the server. The user passes this parameter by reference. This is what the documentation says about 'Conversation Handle'

 

conversationHandle unsigned integer The conversation handle that uniquely represents the connection between the server and the client. Note that zero (0) is a valid handle.

 

Currently, in my function which writes data to a TCP server, I want to check if that socket connection has already been established. So I check whether the handle is equal to zero:

 

if(conversationHandle == 0)             /* if not == zero, skip */

      ConnectToTCPServerEx();

 

// write data to server

 

But the documentation says that zero is a valid handle. So I'm thinking that there is really no way to check if it's already an active socket connection, because the handle itself may have a value of zero. Am I misunderstanding what the documentation is saying?

 

Thanks   

 

0 Kudos
Message 1 of 2
(1,076 Views)
Solution
Accepted by topic author TestEngineer11

Generally your way of programming is a little sloppy. It works sometimes for certain APIs where a canonical value is reserved to indicate an invalid handle and maybe -1 could be used as it is very unlikely to ever be used but the correct way for this API is to do following:

 

ConnectToTCPServer(Ex) returns a status value that indicates if the call was successful. It most likely won't touch the passed in handle value in that case, but to rely on that is also not robust code. Instead you should check for the returned status value and store it somewhere if you need to know if the server was already initialized.

 

In addition you might want to also check any return value for calls you make on that conversation handle to request status, read or write data etc. In case of most errors you usually want to close the connection and have your software reestablish the connection again by a new call to ConnectToTCPServer(Ex).

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 2
(1,052 Views)