I am working to a project involving a GSM/GPRS module which role it is to connect clients with some devices over the internet using TCP-IP and Modbus protocol. Now I am studying the TCP connecting scheme.
As far as I understood , the GSM network automatically close a TCP connection if there is not any traffic on it for some time.
I want to detect and maintain TCP connection as reliable as possible. Also to detect half-open connections.
The connection procedure is as follows: my GSM module acts as a server, so it opens a socket on port 502 and wait for clients to read data. The client can read data periodically at 3 minutes or longer.
When a client connects with my module , it requests for some data, the module sends the requested data and wait for other requests, and keeps the connection open.
Now if the connection gets broken for other reasons (due to power failures, GSM network etc) the client needs to be able to connect with the module again. The module must detect broken connection and must act so the client can reconnect to it again
In order to detect broken connection the module send periodically some dummy data to the client and check if that data was received by the client. If not, the tcp connection must be re-established.
The question is: how to interpret this "re-establishment" of the connection?
Should I close the current TCP connection, and open a new one, meaning listening to port 502 again?
Should I close the current TCP connection and trying to connect to the port and ip of the client as provided from the last valid TCP connection?
The point is that the module must be available as soon as possible to answer to client's requests; that it is why I need the TCP connection to be functional as soon as possible.
PS. The module supports just AT commands ( Quectel M95 module) so I don't have access to all functionalities of the TCP stack. Thank you. (I am new in TCP stack so I may have used technical terms with improper meaning)
The question is: how to interpret this "re-establishment" of the connection?
Ideally the clients should be concerned about the connectivity to the server. Clients come and go. Server should continue to listen
on the port.
Should I close the current TCP connection, and open a new one, meaning listening to port 502 again?
No. Server shall just determine that the connection is gone and close the socket for that connection alone. No need to try listening again. The listen
done in the beginning should still hold good.
Should I close the current TCP connection and trying to connect to the port and ip of the client as provided from the last valid TCP connection?
The server will be least concerned about the client port. It cannot connect to the client. Rather client should re-connect to server.
So have the intelligence built in client to find the broken connections and to re-establish the connection with server.