[erlang-questions] tcp_closed message

Boshan Sun sunboshan@REDACTED
Thu Feb 21 20:32:16 CET 2019


If you have an Erlang process acting as a server, then you have 1 listening
socket listening on a port(say 8080), it will be stored as an Erlang Port.
When someone connect to the server, then you will have 1 accept socket. If
you use gen_tcp:connect as client *in another shell*, then the client also
has 1 socket.


shell1 - server

listening socket #Port<0.6>
accept socket    #Port<0.7>

-----------------------

shell2 - client
socket #Port<0.6>


When you do gen_tcp:close(S), which socket did you close?

If you close #Port<0.7> in shell1 server side, then no {tcp_closed,S} will
be sent.

If you close #Port<0.6> in shell2 client side, then there will be
{tcp_closed,#Port<0.7>} sent to server process.

A process is linked with the port, you can use `erlang:port_info(S)` to
check. When either side close/disconnect the connection, both side's ports
are closed and the linked processes are dead.

Boshan


Justin Wood <justin.k.wood@REDACTED> 于2019年2月21日周四 上午11:03写道:

> Forgive me, I must not have explained my situation very well.
>
> I have an Erlang process acting as the server. It accepts tcp and udp
> connections. I have various clients that connect to my server. When one of
> those clients disconnect from my server, I get the {tcp_closed, S} message.
> However, from within my Erlang server, if I call gen_tcp:close(S), the
> controlling process does not receive the {tcp_closed, S} message.
>
> I am looking for a way to fully emulate the disconnect / close from the
> client within my server. As I said previously, I can just send the
> {tcp_closed, S} message from within my own application. I'm just not sure
> what else, if anything, the VM does when a socket is closed from the other
> end.
>
> For reference, I am writing a testing tool that will randomly kill
> processes and sockets in order to test that your BEAM application(s) can
> properly handle the non-happy paths that the real world may bring to your
> application.
>
> Justin
>
> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> On Thursday, 21 February 2019 13:43, Boshan Sun <sunboshan@REDACTED>
> wrote:
>
> Here's a really simple repo to demonstrate the usage of gen_tcp/gen_udp.
> https://github.com/sunboshan/socket
>
> You can follow the tcp.erl to use gen_tcp to set up a server, then use
> gen_tcp to connect to the server as a client. When one end disconnect, the
> other end will simply get a {tcp_closed, Socket} message. Nothing more than
> that.
>
> Boshan
>
> Justin Wood <justin.k.wood@REDACTED> 于2019年2月21日周四 上午10:24写道:
>
>> Hello,
>>
>> I have a situation where I am closing a gen_tcp/gen_udp socket within my
>> Erlang application. However, I would really like it to act as if it were
>> disconnected or closed from the other end. Basically I want the
>> {tcp_closed, S} message to be sent to the controlling process.
>>
>> A simple solution would be to just send the message myself. I'm just not
>> sure if that would have the exact same behaviour (there is probably more to
>> it than just the closed messed) as if the socket were truly disconnected or
>> closed from the other end.
>>
>> Justin
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190221/b979f1c9/attachment.htm>


More information about the erlang-questions mailing list