Erlang connector timeout never occurs

I am implementing a TCP server in Erlang that speaks with mobile phone clients. Mobile phones often go offline, so the server should be able to detect it. Therefore, I want the server to send messages to clients with a timeout, so when the timeout is closed, the connection is closed and the client is disconnected.

I used this listening function on the server:

[{certfile, "cert.pem"}, {keyfile, "key.pem"}, {reuseaddr, true}, {active, false}, {send_timeout, 10000}] 

After I established the connection between the server and the mobile phone, I switch the phone to airplane mode (which disables all wireless signals) and performs ssl: send on the server. The happliy send function returned normally, as if the packet was successfully transmitted.

What did I do wrong?

+2
source share
1 answer

Have you set the {send_timeout_close, true} parameter to the socket from inet ? If not, the socket will not be closed, just return a timeout error. There is also the risk of ssl swallowing your error and doing something with it.

Some other points:

  • Remember to check the return value of any ssl:send and ssl:receive parameters for the error. It is important to know that the transmission went well.

     ok = ssl:send(Sock, Data), 
  • The underlying TCP / IP stack can actually receive data even with the send_timeout set, but it cannot send it because the other endpoint is not working. Closed port knowledge first appears later when the stack realizes that it never received an ACK.

  • There is a raw type for sockets defined in inet . It allows you to set socket options for the OS. Perhaps you can increase the pressure on the operating system to detect loss of connection.

  • Another option is to pass everything to the ssl:recv/3 call, where a timeout means losing the device, regardless of its socket state. This has the advantage of detecting application problems at the other end, since they are not moving in a certain way. You will need to do this to further process your requests.

  • The mobile phone client can also act. If he sends a message via SSL and the receipt never arrives (due to airplane mode), then he knows that something is wrong. However, the server may not know this.

  • TCP / IP provides a reliable, connection-oriented streaming protocol. It does not protect against sudden outages. This is important because your protocol must solve the disconnect problem itself. This is especially important if your protocol has some kind of confirmation or confirmation.

+2
source

Source: https://habr.com/ru/post/901502/


All Articles