Thanks, I will implement non-blocking socket and read pending input…
My program is client responsible for sending query packets. The device that
makes data available is TCP/IP server that waits for connection request.
For some conditions, server may allow a connection but may not respond with
data packet. In such a case my program will receive no reposnse packet.
The protocol of data packets being exchanged is documented . It includes
application specific header and checksum for each packet. My program
validates header parameters received in response packet and decides whether
data should be stored or discarded. By invalid header and checksum error, I
do not mean TCP header…The program at the device end may not fill in
information as per protocol finalised, hence the validation is implemented.
If validation fails, it is an error condition…
Andrew Thomas <email@example.com> wrote in message
“Krupa” <> firstname.lastname@example.org> > wrote in message news:avjv91$dcd$> email@example.com> …
Recently we develped a data communication program using TCP/IP with
4.25 and Watcom ‘C’ 10.6. The program periodically sends a query packet
receives data from a TCP/IP enabled device. Before sending a query, I
like to discard pending data if any. With a a serial communication
I would use tcflush function for such a purpose. With TCP/IP function
Watcom ‘C’ is there any way of discarding pending input/output data?
Since I was unable to find a way to achieve this, at present after
error (like checksum error, no data received, valid header not received)
condition, I am closing the TCP/IP connection. In next iteration of
before sending query, I have to reestablish a connection with the
You can discard pending input data on the socket by setting the
socket to non-blocking, then read() until you get 0 bytes back.
I don’t know of a good way to discard pending output data.
I’m not sure what you mean by “every error” above. TCP
guarantees that either:
- the data is delivered correctly, or
- you get an (unrecoverable) error on the socket.
In the case of 1, you do not need to perform checksum, the data
must be received, and the header must be valid.
In the case of 2, you must close and re-establish the socket.
If you are seeing data errors on incoming data, then the problem is
with your code. The sender is transmitting incorrect information, or
you have a problem reading the input stream. The most common
error with TCP communication is that the reader does not collect the
entire data set due to a partial read(), and the program is not designed
to recover correctly. In TCP there is no guarantee that a read() call
will return all of the transmitted data, even if you know how much you
are expecting. You need to perform multiple read() calls until you have
collected all of the incoming data.