LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

How to create non blocking TCP server?

Solved!
Go to solution

Hi,

 

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.

 

Regards

 

Ashley

 

0 Kudos
Message 1 of 9
(1,912 Views)

Explain in more detail what is blocked?
Or attach simple code with demonstration of your trouble.

0 Kudos
Message 2 of 9
(1,889 Views)

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. 

Marc Dubois
HaroTek LLC
www.harotek.com

new_CLA_100x100.pngnew_CTD_100x100.pngnew_instructor_100x100.pngNI_Alliance_Partner_Logo_243x76.jpg

0 Kudos
Message 3 of 9
(1,873 Views)

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.


There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
Message 4 of 9
(1,862 Views)

Ashley,

 

     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).

 

Bob Schor

0 Kudos
Message 5 of 9
(1,834 Views)

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?

 

Regards

 

Ashley

 

0 Kudos
Message 6 of 9
(1,797 Views)
Solution
Accepted by topic author ashes.man
04-02-2016 11:38 PM
Parallel code in LabVIEW usually runs in parallel. This is even true of a wait - if, inside a loop, you wait 100 ms and have other code in parallel, it doesn't delay the execution of the other code, rather it runs the wait and the other code at the same time. When both have finished, then the loop can proceed to the next iteration. So yes, if you have one loop with a 1000ms wait, and another with a 100ms wait, and the waits take longer to execute than any parallel code in the loops, then the 100ms loop will run approximately 10 times as many times as the loop with the 1000ms wait.

I recommend that you put the Wait on Listener in a separate loop, and also have another parallel loop that handles communication once a connection is established. Then you can use a -1 (forever) timeout on the Wait on Listener, and that loop will almost never run or use resources except when a connection is first established. To kill that loop, close the listener somewhere that doesn't depend on the Wait on Listener loop executing. When a new connection is established, put the connection in a queue, where the communication loop can pick it up.
Message 7 of 9
(1,776 Views)

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!

0 Kudos
Message 8 of 9
(1,764 Views)

 

 

 

0 Kudos
Message 9 of 9
(802 Views)