I'm developing a socket server and a client with .NET Framework 4.6.2 and C#.
Client is SynchronousSocketClient
.
Server class is more or less like this one.
On client I send a data to update on database.
On server I do a operation in a database (update a table using a stored procedure).
The process is client sends data to server, server process it and send back a message with the result.
Sometimes that database operation gets more time to do it. When this time is greater than Socket.ReceiveTimeout
I get a SocketException
due to a TimeOut and I don't process the data that comes from the server.
Is there any way to process this data sent by the server?
After the TimeOut I don't wait any more for the data sent by the server and I start the process again, sending new data to the server, but when I try to read the server result from this new data, I read the server response for the previous one.
The process on client is critical and I can't wait to get an answer from the server. I need to continue processing data.
I would suggest to use an ID when sending a message to the server. (You can take the first 2-bytes of your sending buffer) The server would use this ID when it sends a response. Now on the client side you can check whether your response has the same ID. If it has you can use the data, otherwise you just discard it and try to read the next incoming response.
Schematically it would look like this: