01-04-2010 02:38 PM
Hello I was wondering if there is some way to just open a port and receive all data sent to that port(No connection ID required). I have multiple sockets sending data to one port and I would like to receive data from all of them on on port in one thread. What would actually be perfect is a vi that just listens to a port and fills up an array(index 0 most recent tcp data packet received). so basically the following scenario...
start Server A listening on port 7000
ClientA sends A123 to port 7000
ClientB sends B123 to port 7000
ClientC sends C123 to port 7000
server A recieves A123 then B123 then C123 on port 1000 and generates the following array
C123
B123
A123
I bring this up because I am having too much trouble keeping up with multiple connection ID's. Anyways any recommendations are appreciated, thanks for the help.
side not...In Java this is relatively simple I just open a socket on a port and every client that sends data ends up going to that socket so appending the sockets buffer to a string ends up getting the data from all of the clients...but the whole connection ID thing in LabVIEW is causing me a great deal of confusion, so I would just like to avoid it at this point if possible, and just get all of the data from the port lumped together.
01-04-2010 02:50 PM
OK well I just realized that the above will not completely work for my application because I have to be able to send data back to whom ever sent the data in the first place, so I need to keep track of each connection...So perhaps could somebody just recommend an example that will display the multiple connection ID's. I have asked the question in other posts but I am still confused. Any help is appreciated. So in addition to the data above I would also need an array to keep track of the connection ID's like...
Start Server A listening on port 7000
ClientA sends A123 to port 7000 and receives connection ID 1
ClientB sends B123 to port 7000 and receives connection ID 2
ClientC sends C123 to port 7000 and receives connection ID 3
server A recieves A123 then B123 then C123 on port 1000 and generates the following array
C123 ID3
B123 ID2
A123 ID1
Or perhaps a cluster would be better so that the data types could be preserved, anyways I am still having trouble trying to get this to work, thanks again for any help.
01-04-2010 03:12 PM
One solution for you might be to use UDP instead of TCP. If you do that you can listen on a single port and get data from all your connections. Instead of tracking connections, whenever you receive a packet you also keep track of the IP address that sent it (this is one of the outputs of UDP Read), and use that address to send the response.
To do this with TCP/IP, take a look at the Multiple Connections - Server example, in the example finder under Networking -> TCP & UDP. It demonstrates one way to keep track of multiple TCP connections, although it doesn't deal with all the complexities. I once wrote code that handled this neatly but no longer have access to it to provide as an example; I can explain how it worked. Whenever any socket received data, it was converted to a variant and the connection ID was attached as an attribute (a cluster would work equally well here), then passed off to another loop for processing. When the processing loop completed it put the result into a queue, again as a variant with the same connection ID as an attribute. The sending loop retrieved the connection ID and sent the response. The sending and receiving loops were completely separate.
One complexity that is not handled in the example is how to handle connections that time out. You need to loop through each connection ID fairly quickly, checking for new data, if you want it to be responsive. It's quite likely that sometimes there will be no new data on one of the connections, generating an error that you'll need to ignore. However, you do want a way to close a connection if there's no data for a sufficiently long period of time. I handled this by putting each connection into a cluster that also contained a timestamp of the last packet received on that connection. Each time through the loop, if there was no new data and the time elapsed since the timestamp was greater than my timeout limit, I closed the connection and removed it from the array.
01-04-2010 03:31 PM