I am implementing a small subset of a UDS client with SocketCAN on embedded linux. The communication between the client and server works on a request / response pattern. I would like to read() the response of the server after having used write() to send the UDS request. The problem is when there exists older data in the receive buffer (which was received before the write()-call) the read() returns this old data. How can I either a) clear the input buffer or b) determine the timestamp of the received response data to compare with the timestamp of the request? What is the cleanest solution for this? Because read() is blocking when there is no data to return it is not so easy to just dummy-read until the buffer is empty. I feel there must be a simple solution for this but I am new to socket() and its friends...
Thanks for your help!