I am trying to send TCP packets across a network (WiFi and LAN) to a LabVIEW client. I am sending a packet of 1400 bytes containing the following:
Byte 0-3: Packet size
Byte 4-1398: Data
Byte 1399: 10
My data consists of an array of zeros with one byte = 255. The 255 byte starts at the beginning of the data array and increments by one each packet (as shown in the 2D picture). This allows me to make sure each packet is coming across correctly (I can check that the index of the byte equal to 255 is the index of the last+1).
Currently I am reading in the TCP with VISA. It is configured as termination char of 10. The problem arises when LabVIEW is sent a partial packet. When the TCP VISA receives a partial packet, it continues to read in until the termination char. Now the total bytes read in is partial packet + 1400. When this happens, the program steps back 1400 bytes and deletes the rest of that partial packet.
All of this is fine, except it appears that after a partial packet comes in and is corrected, part of my data is missing. ie. My array skips some values as shown in the 2D picture representation.
Is there a better way to receive TCP data in LabVIEW that corrects for partial packets?
I would suggest that you read your data in two reads and don't use the termination character to stop your read. Your data contains the size of the data to follow. Therefore simply read 4 bytes, convert this to an I32 and pass that to the next read for the number of bytes to read. If your data is binary you have a good chance at having your termination character (0x10) be a valid data byte and not the actul termination character.
Thanks for the reply... I agree with your suggestions - thanks. I saw that setup in the examples.
However, that doesn't solve the partial packet problem. If I read the first 4 bytes then send that to the second TCP read all works well until the partial packet comes along. When that happens, the beginning of the partial packet comes in with the size information and the second TCP read reads the rest of the partial packet and the beginning of the next full packet (including the size information). Now there is the rest of the full packet and the beginning of the next packet in the buffer. (See below)
Partial packet problem:
1st packet is partial, second packet is full
SSSSDDDDDDDDDDDDDDDDDDDDDDDSSSSDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD...DDD (1400) bytes
Read size and take in 1400 bytes. This includes the partial packet and part of the next full packet.
Next read cycle starts to read the first four of the data left in the buffer which is four bytes of random data. This is where the program fails!
The next read is 4 bytes looking for size information but instead of reading in the beginning of the packet, it reads in the leftover packet in the buffer. Obviously the size information is not correct now and the whole program goes to hell!
Any suggestions to fix this problem?
Are you saying that your sender is sending incomplete packets. That is, it is saying that it will be sending you 1400 bytes but onlly sends 1000 bytes? If this is the case the problem is on the end sending the data. Getting back in sync after this type of fault is not that easy since you don't know where you are in the data stream. Gnerally the best recovery here is to close the connection and start with a new connection.
Have you tried using a network analyzer (Wireshark) to capture the data? This can help narrow down where you are losing data.