Showing results for 
Search instead for 
Did you mean: 

TCP/IP read first communication



I have a problem with the TCP/IP communication.


-Current status: I'm wornking in one PC (winXP - LV 7.1). In the future I will work with one PC and one compact FieldPoint.


-LabView code: I use two VI, 1st as a client and 2nd as a server.


-Problem: The VI work fine except in the first action (when I push the button to send the message to server (from client to server), the client doesn't recive the message reply from server). But when I push the button again, I recive the message reply correctly!


I attached the code.


Thank you




winXP / LV 7.1 & 2010

0 Kudos
Message 1 of 3

First off I would use modify your code to use state machines. Your communication model is more of a command response model and is overly complex as written. You don't need the all of the polling loops, especially in your client. The server would need to look for messages but there is no reason the client shuold be constantly reading. It should only look for a response after it has sent something to the server. I would also use an event structure in your client to trigger when to send the data to the server. This is more efficient than the polling loop you currently use.


You should modify your message protocol. Rather than sending the message only and reading 512 bytes (what happens if you data is larger than 512 bytes) you should use a simple protocol that would send the length of the data followed by the data itself. That way your initial read at the server would be to read 4 bytes (message length) and then read the exact number of bytes as for the message. If you receive two messages at the same time from the client you will read then both and process it as a single message.


I believe that the basic communication is working now but you are not observing it since the indicators displaying your messages are written every iteration. Therefore when your polling read fails you are overwriting the value. If you watch very closely you can see the message appear and disappear in the "data out" indicators. In some cases it happens so quickly you can barely see the update.


Your server is not closing the client connections when it exits nor is it detecting when a client closes a connection and leaves. For that matter your client is not closing the connection when it exits. It should do this. Don't rely on LabVIEW to do it automatically for you.


You may want to initialize your stop button when the program starts.


I included a client/server example that is more event driven. It also supports communications in both directions. In your case it looks like only the client initiates the message.

Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 2 of 3

(one week later... working hard...)


thank you for your response, Mark Yedinak. I've create a Server Agent based in your example and with some modifications it works fine.
I had always worked with lv7.1, but I think it's time to use lv2010.

The User Events are new for me, but with hard work and effort all can be possible.


File attached in case anyone has the same problem as me.

(Note:The vi is unfinished, it is necessary to manage the program stop.)

Win XP / LV 7.1 & 2010

0 Kudos
Message 3 of 3