I’m writing an Interface under Linux which gets Data from a TCP socket. The user provides a Buffer in which the received Data is stored. If the provided Buffer is to small I just want to return an Error. First problem is to determine if the Buffer was to small. The recv() function just returns me the amount of bytes actually written into the Buffer. If I use the MSG_TRUNC flag stated on the recv() manpage it still returns me the same. Second problem is to discard the data still queued in the socket. So if I would determine that my provided Buffer was to small I just want to erase everything which is left on the socket. Is there any other ways to do so except Closing and opening the socket again or just receive until nothing is left? Best Regards <
One suggestion was to just recv until nothing is left ( I get returned 0 ) – but wouldnt that end in having to wait for the preset timeout ( in this case it’s 5 sec? ) because everytime I call the recv it waits for data or timeout?
There is no knowledge at TCP level about what constitutes an application protocol message. There are, however, two most common ways to delimit messages in a TCP stream:
In this light, a generic TCP reader should provide two reading functions to be universally useful:
A design similar to Tornado IOStream reading functions would do.