LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

TCP Server with Multiple Ports

Is it possible to create a server with a range of ports open, using one TCP callback ?

I need to allow the server to have one or more clients connecting to it, the clients will all be running on the same host PC but using differnet ports. (NOT the same host PC as the server). I think this would be possible if I could identify which port had be connected to at the server, but I can't see any way of getting that information using any of the TCP libary functions.
0 Kudos
Message 1 of 3
(5,369 Views)
James,

A TCP server will be registered to listen in only 1 port, that is just the nature of TCP. To perform something like that you would need to have another architecture.


  1. You can register multiple servers in you application that are waiting for clients to connect to each server; you would have a separate callback for each port.

  2. You can register 1 single server and have the clients make connections from different ports (done automatically by Windows) to the same port in the server; you would need to implement some method to identify the "type" of connection that it was stablished(probably the first value sent is the "type").

  3. If all the clients run in the same application you can have 1 single connection and the create some sort of proto
    col to identify and separate each connection.

  4. Another thing to take into account when you have multiple connections will be execution time. If the rutines in your callback take a long time the other connections will be waiting for that callback to finish. If you don't see the performance that you expect you will need to program multuple threads (1 for each connection).

    I hope this helps,

    Regards,

    Juan Carlos
    N.I.
0 Kudos
Message 2 of 3
(5,369 Views)
James (if you are still interested)
Yes,
if you use the callback data you can transmit the port request, the documentation is a little confusing but it is in there(sort of , :):):) ...),
example (assume PortNum[] is an array of ports declared on the server:

regstat = RegisterTCPServer (PortNum[0], ServerTCPCB, (void*)PortNum[0]);
;;
on the client
regstat = ConnectToTCPServer (&tcpHandle[0], PortNum[0], tempBuf, ClientTCPCB, NULL, 5000);

and on the server pass the port number
int CVICALLBACK ServerTCPCB (unsigned handle, int event, int error,
void *callbackData)
{
..
switch (event)
{
case TCP_CONNECT:
if (handle == tcpHandle[0])
{

/* We already have one client, don't accept another... */
}
if (handle == tcpHandle[1])
{
} }
if (handle == tcpHandle[2])
{
} }
else
{
/* Handle this new client connection */
if ((int)callbackData == PortNum[0])
{
tcpHandle[0] = handle;
.......//

Demo code is attached in the zip file.
I tested it on three computers
one client (port1) was an NT4.0 machine with CVI 5.0
two client instances (ports 1 & 2) ran on XP under CVI 5.5
and the server was W2KP with CVI 6.0
this works, the only issue is how to do handshaking to get real speed(and that is a big issue as far as I know) I haven't been able to test it over a WAN.
hope this helps.
The example code has a lot of other stuff in it for testing (Sorry about that, time yo
u know....)
CMM.
0 Kudos
Message 3 of 3
(5,369 Views)