LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

tcp

If you don't use the timeout method I described your client must either send the number of bytes it will be sending or you must define some type of pattern to indicate the end of the data. If you don't have either of these then you have no option but to use the method I described. Is this some type of command/response protocol?

 

The way to use the timeout to indicate the end of data is to have your server wait for data to arrive. While waiting you use a longer timeout. Once some data arrives you continue reading the data but use a smaller timeout to indicate the end of the end. Basically you set your timeout to some short but reasonable value. Let's say you set it to 250 ms for example. The assumption here is that the client will send all of the data set at one time and that there is some minimum time between data sets. And yes, this method does work and is fairly reliable. I have been using it for years. It will not work if your sending device is sending unsolicited data sets and the data sets are sent with no time between them. This works very reliably in a command/response protocol or if there is some amount of time between data sets.

 

As mentioned above the only other alternatives you have is if your protocol either sends the number of bytes for the data set or some other id to specify the what type of data set is being sent. In this case your protocol would have to have defined formats and fixed sizes for the data sets.

 

The code would basically look like this:

TCP Read.png



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
0 Kudos
Message 11 of 18
(679 Views)

As u said i cant use timeout but for temination i am reading in CRLF mode & one more thing,If sends me( to Server )number of byte than i have to use 2 consecutive TCP read & this is what i am affraid to use.

                                                                                                                          And if i dont use the number of byte so i cant use the according timeout to check whether data is received or not. In my project worst time will be 2hrs. So can you please tell me the solution now.

 

Thanks,

--------------------------------------------------------------------------------------------------------
Kudos are always welcome if you got solution to some extent.

I need my difficulties because they are necessary to enjoy my success.
--Ranjeet
0 Kudos
Message 12 of 18
(661 Views)

@Ranjeet_Singh wrote:

As u said i cant use timeout but for temination i am reading in CRLF mode & one more thing,If sends me( to Server )number of byte than i have to use 2 consecutive TCP read & this is what i am affraid to use.


I think I'm repeating what others have already said here, but WHY are you afraid to use two reads?  The sender only has to send ONE packet that contains both the length and the actual data.  The server reads the first 4 bytes of that packet in the first read to determine the length, then reads that number of bytes with a short timeout in the second read.  Since the data arrived in the same packet as the length, it must have already been received prior to reading the length so there is no problem using a short timeout.  There is no possibility for an infinite loop here.

0 Kudos
Message 13 of 18
(656 Views)

Ok i will try to implement this but than tell me what i should give timeout for Listen.vi 1st read VI & 2nd read.vi

 

                                                                                                                              If in 2nd read if i use short time out than also within this timeout time data should be read successfully & TCP close connection should be executed. This timeout is for entire session not for before this timeout time read should be started.

 

But by this i think i got 1 direction thanks nathand for that

--------------------------------------------------------------------------------------------------------
Kudos are always welcome if you got solution to some extent.

I need my difficulties because they are necessary to enjoy my success.
--Ranjeet
0 Kudos
Message 14 of 18
(646 Views)

@Ranjeet_Singh wrote:

Ok i will try to implement this but than tell me what i should give timeout for Listen.vi 1st read VI & 2nd read.vi


The timeout for the first read should be as long as necessary; it might be forever (-1) if that's appropriate for your application.  You can force a TCP Read to terminate even if it's waiting forever by closing the TCP connection, which is often useful in a server: let the read wait forever, and if the user exits the application, close the connection to cause the Read to finish (it will return an error but no data, of course).

 

The timeout for the second read can be short, I'd suggest in the range of 10-100ms.  If your packets are relatively small and infrequent, even a 0ms timeout will likely work because the length and data should arrive together.  If you're sending large amounts of data over a slow network and are seeing timeout errors, increase the time.


@Ranjeet_Singh wrote:
If in 2nd read if i use short time out than also within this timeout time data should be read successfully & TCP close connection should be executed. This timeout is for entire session not for before this timeout time read should be started.

I do not understand what you are trying to say here.  Is this a question?

Message 15 of 18
(633 Views)

For 1st read i will use -1 as that is only option for me.

 

How can i close the TCP connection manually(apart from stop button).

 

Timeout for second read, i may need to read 2100000 bytes at the worst case tell me how much i need to keep ?

 

Well my question is.

 

In the second read operation if i used timeout as one of the termination than within this timeout i should ber able to read the data successfully(i.e. i should be able to read the expected number of bytes.) & after this TCP CLOSE.vi should be executed.  Well in this timeout which is given to the TCP read is for entire operaion (i.e. for read & close the operation or only for the operation start i.e. before this timeout the data should be arrived so that read operation can begin.

--------------------------------------------------------------------------------------------------------
Kudos are always welcome if you got solution to some extent.

I need my difficulties because they are necessary to enjoy my success.
--Ranjeet
0 Kudos
Message 16 of 18
(612 Views)

I already gave you the code above to do what you want.. I generally do not like to use an infinite timeout (-1). I would rather use a reasonable timeout value and ignore the timeout on the initial read. Infinite timeouts if not handled properly can cause your program to hang. All you need to determine to use the code I posted is what is a reasonable time between your messages that you can use. You could read gigabytes using my approach. You simply keep reading while there is data. You never answered if you are streaming the data or if this is a command and response type of situation. Streaming can be a bit more complicated to determine a reasonable timeout between data sets. A command response scenario is pretty easy.



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
0 Kudos
Message 17 of 18
(598 Views)

Ranjeet_Singh wrote: 

How can i close the TCP connection manually(apart from stop button).


See the attached snippet.  Note that when you click the STOP control, the lower while loop exits, and it calls TCP Close on the Listening end of the connection, which causes the TCP Read to exit even though it has a -1 timeout.  (Yes, the wiring is a little bit weird in order to show that it's the listening end that causes it to stop, because closing the sending end would also cause an error.)


Ranjeet_Singh wrote:

Timeout for second read, i may need to read 2100000 bytes at the worst case tell me how much i need to keep ?


The timeout value doesn't matter so long as it's sufficiently long for you to read the data.  Is there some reason your application needs a short timeout value?  Otherwise, just make plenty long (30 seconds?) and stop worrying about it.

 


Ranjeet_Singh wrote: 

Well my question is.

 

In the second read operation if i used timeout as one of the termination than within this timeout i should ber able to read the data successfully(i.e. i should be able to read the expected number of bytes.) & after this TCP CLOSE.vi should be executed.  Well in this timeout which is given to the TCP read is for entire operaion (i.e. for read & close the operation or only for the operation start i.e. before this timeout the data should be arrived so that read operation can begin.


I have no idea what you are trying to say here.  TCP Read will look for new data continuously during the timeout period, and will stop when either the timeout period elapses or it the requested amount of data arrives.  The data could have already arrived prior to calling TCP Read, in which case it will return almost instantly, but if the data is not already available it will wait the timeout length for that amount of data to arrive.

 

TCP example.png

0 Kudos
Message 18 of 18
(589 Views)