I'm trying to write simple MODBUS server on MQX device. Modbus protocol is using TCP protocol on lower level, so basically Modbus consist of sending reqeust and getting responses from other side.
My server is doing main loop where it :
- accepts connection from TCP client
- waits for requests from connected client (recv)
- processes and returns responses to client (send)
- if connection dies then it listens for another connection.
Now my basic problem is how to write robust TCP server that will respond to all requests - right now, I'm losing pace to client - somehow server doesnt reply on first packet and then it replies one packet behind request being sent by client...
This problem is pointing me to another question I'm seeking answer for. How do you write robust server in a sense that it will always retrieve one message and respond to it and that it will do this properly also if more messages are already in buffer. How do you do this - not to loose any incoming message ?
I'd kindly ask for some guidance, advice or pointer to more details about this aspect of TCP protocol & sockets...