TCP/IP communication

Hi,

Recently we develped a data communication program using TCP/IP with QNX
4.25 and Watcom ‘C’ 10.6. The program periodically sends a query packet and
receives data from a TCP/IP enabled device. Before sending a query, I would
like to discard pending data if any. With a a serial communication program,
I would use tcflush function for such a purpose. With TCP/IP function for
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 every
error (like checksum error, no data received, valid header not received)
condition, I am closing the TCP/IP connection. In next iteration of query
before sending query, I have to reestablish a connection with the server.

Regards,
Krupa

“Krupa” <seto@vsnl.com> wrote in message news:avjv91$dcd$1@inn.qnx.com

Hi,

Recently we develped a data communication program using TCP/IP with
QNX
4.25 and Watcom ‘C’ 10.6. The program periodically sends a query packet
and
receives data from a TCP/IP enabled device. Before sending a query, I
would
like to discard pending data if any. With a a serial communication
program,
I would use tcflush function for such a purpose. With TCP/IP function for
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
every
error (like checksum error, no data received, valid header not received)
condition, I am closing the TCP/IP connection. In next iteration of query
before sending query, I have to reestablish a connection with the server.

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:

  1. the data is delivered correctly, or
  2. 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.

Cheers,
Andrew

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…

Thanks,
Krupa
Andrew Thomas <andrew@at.cogent.dot.ca> wrote in message
news:avjvra$ec3$1@inn.qnx.com

“Krupa” <> seto@vsnl.com> > wrote in message news:avjv91$dcd$> 1@inn.qnx.com> …
Hi,

Recently we develped a data communication program using TCP/IP with
QNX
4.25 and Watcom ‘C’ 10.6. The program periodically sends a query packet
and
receives data from a TCP/IP enabled device. Before sending a query, I
would
like to discard pending data if any. With a a serial communication
program,
I would use tcflush function for such a purpose. With TCP/IP function
for
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
every
error (like checksum error, no data received, valid header not received)
condition, I am closing the TCP/IP connection. In next iteration of
query
before sending query, I have to reestablish a connection with the
server.

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:

  1. the data is delivered correctly, or
  2. 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.

Cheers,
Andrew