[erlang-questions] Peername of a closed TCP socket

Dowse, Malcolm <>
Wed Jun 3 13:19:52 CEST 2009


Thanks for the tip, but unfortunately it didn't help. After some more
investigation I managed to reproduce the issue. The client was closing
its connection immediately after opening with the SO_LINGER option
enabled, and a timeout of 0 seconds. This resulted in a RST with no FIN.
So, even if I called peername immediately after accept, with option
{exit_on_close, false} set, it always returned enotconn.

However, when I wrote a server in C I had no problem getting the remote
address. The 'accept' C call returns a socket address along with the new
file descriptor. This is also the interface given by the 'accept' call
in python's socket module.

Would it be possible to get a similar accept interface in Erlang, in the

best regards,


-----Original Message-----
From: Tony Rogvall [mailto:] 
Sent: 26 May 2009 22:06
To: Dowse, Malcolm
Cc: Erlang Questions
Subject: Re: [erlang-questions] Peername of a closed TCP socket

You can try the inet option:
	{exit_on_close, false}
This will keep the port and the socket active so you can read  
continue write data to a half open socket and possibly get the  
peername, if that is available
by the underlying operating system.

You must then call gen_tcp:close to terminate the port/socket


On 26 maj 2009, at 17.39, Malcolm Dowse wrote:

> Hello,
> When a TCP client disconnects very soon after connecting to an  
> Erlang server, is there any reliable way of getting the client's  
> remote address?
> The inet:peername/1 function returns an error if the socket in  
> question has closed. As a result, I can't find any better solution  
> than to refactor the code so that inet:peername/1 is called as soon  
> as possible after the gen_tcp:accept/1.
> Thanks in advance,
> malcolm
> _______________________________________________
> erlang-questions mailing list
> http://www.erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list