How to detect tcp-client disconnect with gen_tcp?

I am trying to use the gen_tcp module. There is a server side code example that I'm having problems with.

%% First, I bind server port and wait for peer connection
{ok, Sock} = gen_tcp:listen(7890, [{active, false}]),
{ok, Peer} = gen_tcp:accept(Sock),

%% Here client calls `gen_tcp:close/1` on socket and goes away.
%% After that I am tryin' send some message to client
SendResult = gen_server:send(Peer, <<"HELLO">>),

%% Now I guess that send is failed with {error, closed}, but...
ok = SendResult.

When I call again gen_tcp:send/2, the second call to wil returns {error, closed}as expected. But I want to understand why the first call succeeded? Am I missing some details about tcp? This is a strange (for me) behavior only for a {active, false} connection.

+3
source share
1 answer

, , , , . send, , , , , , , . , , send , .

recv , , . , Erlang active, , active .

, active , Erlang. , C API , .

+5

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


All Articles