I would like to have a TCP server in my application. It will go about its business as usual but in the background a TCP server is sitting waiting for a client connection. If such a connection exists, the normal code will send stuff on that connection otherwise it will just not send. Currently I can not see how to stop it blocking while it waits for a client (TCP Wait On Listener block). I have looked at the TCP server/clien example but it blocks in the same way.
I would appreciate advice on how I would structure such an application. I would also like to do the same thing on the client side. Go about business as usual and if a connection is able to be established at some stage then send some stuff on that connection otherwise just dont bother.
Solved! Go to Solution.
Go to the help menu, select Find Example, with the Task radio button selected (default), open the Networking folder, open the TCP & UDP folder, then double-click Simple TCP.lvproj.
By non-blocking, I suppose you mean timed out. By default, all TCP and UDP communication are timed out in LabVIEW.
You really should have your TCP communications is a seperate loop from the rest of your code. When you want something sent, use a queue to send data to the TCP loop of your code.
I think this is do-able, but I, for one, am a bit vague about exactly what you want to do. You might consider writing (dare I say it?) some Documentation describing what you are trying to accomplish. Do not worry about how to do whatever it is (for example, I wouldn't say "TCP Server", as that conjures up a "mechanism", but rather might say "Exchange information between two LabVIEW programs running on different PCs connected to a Local Area Network"), but describe (for yourself, mainly, but also helpful for us) the Players (e.g. LabVIEW programs running on different PCs, or two LabVIEW programs running on the same PC, or LabVIEW RealTime connection to RIO platform, etc.), what is being communicated (Data? Text? Web interaction?), and what you'd want to do about it.
I have some experience using Network Streams in LabVIEW RealTime. Here we definitely want the Host and Remote system to be in communication, so we generally have the Remote start by "listening" for a connection and only proceeding once all of the Connections have been established. Similarly, the Host, during its Initialization phase, "reaches out" to the Remote and essentially waits until Contact is established.
But there is no reason that the code couldn't be written with parallel loops trying to set up the connection and setting a Boolean "Connection is Live" flag. If you follow Crossrulz's suggestion and Enqueue messages, the Dequeuer could simply "toss" them if the Connection was dead. For the Listener routine, the Listener would set its own "Live" flag and enqueue data from the Sender. If the Listener flag said there was no connection, you don't try to Dequeue (as there is no "there" there).
OK... So, sorry I did not explain my question well enough!
What I would like to do is to understand how to create a labview program that can generate data and do other control but also as part of it has TCP server functionality. I do not want the server part to block the entire execution of the program waiting for client connectiions. This is a learning exercise so I dont have an application example I can give exacty, but try this:
Thee application must:
- Control some IO to manage an automated process
- Generate some data/results from measurments taken
- Provide a TCP server so that a client (then ultimately many clients) can connect at any arbitrary time and view measurement data.
- Dont make any assumptions the client is another labview program, it couldd be anything supporting a TCP connection (ignore networking details, for now I will use localhost and a TCP terminal program for testing).
- The part that generates results will push them to a queue for sending if there is at least one active client connection. The TCP server will look at the queue and format the results and send to all clients.
- I dont want the program blocked indefinitely while it waits for client connections.
I think it just workedd it out anyways... I used the multiple client TCP server example and added a parallel while loop with a short timeout. In that loop I put an indicator and numerically added one to it each iteration.
Can someone please clarify this for me: If I have a VI with two parallel loops. One has a TCP Wait On Listener with 1000ms timeout. The other just has a wait 100ms delay. Can you please confirm that the loop with the wait 100 ms delay will evaluate 10 times (roughly) while the other loop waits 1000ms for a client connection timeout? I think this is where I am struggling to understand labview. Do functions freeze entire execution of the VI for their timeout, or does it just delay evaluation of the containing structure annd go away and evaluate other functions that can be evaluated?
Thanks, that was what I needed to know. I have been struggling to get my head around the order of evaultaion in labview and how parallelism is achieved. I think I hve also been making some mistakes and having outputs of one part that is waiting as inputs to another part, causing it to have to wait on its inputs. I have been writing code for a long time now and keep trying to relate the diagrams back to written code but I think sometimes that isn't the best approach!