closing a listening socket by port number

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

closing a listening socket by port number

Willem Broekema
I want to close a listening socket by its port number.

I have an application that should start a listening socket on a
predefined port. Instead of return "{badmatch,{error,eaddrinuse}}",
it should exit, or at least close the socket of, that process and
take its place.

Is it possible?

Thanks,

- Willem



Reply | Threaded
Open this post in threaded view
|

closing a listening socket by port number

Samuel Elliott
On  2/11, Willem Broekema wrote:

| I want to close a listening socket by its port number.

This does not work this way.

| I have an application that should start a listening socket on a
| predefined port. Instead of return "{badmatch,{error,eaddrinuse}}",
| it should exit, or at least close the socket of, that process and
| take its place.
|
| Is it possible?

The TCP/IP stack does not allow that (you have to know the file descriptor
to close it; the FD is the only thing that you can manipulate at all).

You should use a supervisor (see OTP principles) or at least a linked
process: that process would close every socket object opened by the faulty
process.



Reply | Threaded
Open this post in threaded view
|

closing a listening socket by port number

Rodolphe Duge de Bernonville
In reply to this post by Willem Broekema
Willem Broekema a ?crit :

>
> I want to close a listening socket by its port number.
>
> I have an application that should start a listening socket on a
> predefined port. Instead of return "{badmatch,{error,eaddrinuse}}",
> it should exit, or at least close the socket of, that process and
> take its place.
>
> Is it possible?
>

you should also do that in the module that open the socket :

setsockopt with SO_REUSEADDR

example from different erl files :

   gen_tcp:listen(Port, [binary, {packet,cdr}, {reuseaddr,true}])
 
doc :
   http://www.erlang.org/doc/r8b/lib/kernel-2.7/doc/html/inet.html