[erlang-questions] Open TCP ports

Erik Reitsma (RY/ETM) erik.reitsma@REDACTED
Fri Jan 12 13:01:41 CET 2007


I use the following code, because I not only need to have the ports
printed in the shell, but I need to do something with it.
I use the fact that all sockets are ports with name "tcp_inet". It
returns a list of tuples {Port, Address, PeerAddress, PeerPort, Owner,
Input, Output}, where Owner is the process that listens to local port
Port. If there is no peer address and peer port, it is only listening,
not yet connected. Key functions to use are erlang:ports/0,
erlang:port_info/1, inet:sockname/1 and inet:peername/1.

get_sockets() ->
    SockPorts =
	lists:filter(fun({Sock,Info}) ->
	
lists:keysearch(name,1,Info)=={value,{name,"tcp_inet"}}
		     end,
	
[{SockPort,erlang:port_info(SockPort)}||SockPort<-erlang:ports()]
		    ),
    SockPortsInfo =
	lists:map(
	  fun({Sock,Info}) ->
		  {socket_info,Name,Links,ID,Owner,Input,Output} =
get_socket_info(Info),
		  {Address,Port} =
		      case catch inet:sockname(Sock) of
			  {ok,{IP,PortNo}} ->
			      {print_ip(IP),PortNo};
			  NoSock ->
			      {"-",-1}
		      end,
		  {PeerAddress,PeerPort} =
		      case catch inet:peername(Sock) of
			  {ok,{PIP,PPortNo}} ->
			      {print_ip(PIP),PPortNo};
			  PNoSock ->
			      {"-",-1}
		      end,
		  {Port,Address,PeerAddress,PeerPort,Owner,Input,Output}
	  end,
	  SockPorts),
    GoodSocks =
	lists:filter(
	  fun({-1,_,_,-1,_,_,_}) ->
		  false;
	     (_) ->
		  true
	  end,
	  SockPortsInfo).

get_socket_info(Info) ->
 
get_socket_info(Info,{socket_info,"undefined",[],-1,undefined,-1,-1}).

get_socket_info([],Res) ->
    Res;
get_socket_info([{name,Name}|More],{socket_info,_Name,Links,ID,Connected
,Input,Output}) ->
 
get_socket_info(More,{socket_info,_Name,Links,ID,Connected,Input,Output}
);
get_socket_info([{links,Links}|More],{socket_info,Name,_Links,ID,Connect
ed,Input,Output}) ->
 
get_socket_info(More,{socket_info,Name,Links,ID,Connected,Input,Output})
;
get_socket_info([{id,ID}|More],{socket_info,Name,Links,_ID,Connected,Inp
ut,Output}) ->
 
get_socket_info(More,{socket_info,Name,Links,ID,Connected,Input,Output})
;
get_socket_info([{connected,Connected}|More],{socket_info,Name,Links,ID,
_Connected,Input,Output}) ->
 
get_socket_info(More,{socket_info,Name,Links,ID,Connected,Input,Output})
;
get_socket_info([{input,Input}|More],{socket_info,Name,Links,ID,Connecte
d,_Input,Output}) ->
 
get_socket_info(More,{socket_info,Name,Links,ID,Connected,Input,Output})
;
get_socket_info([{output,Output}|More],{socket_info,Name,Links,ID,Connec
ted,Input,_Output}) ->
 
get_socket_info(More,{socket_info,Name,Links,ID,Connected,Input,Output})
;
get_socket_info([H|More],Res) ->
    get_socket_info(More,Res).

print_ip({B1,B2,B3,B4}) ->
    integer_to_list(B1) ++ "." ++
    integer_to_list(B2) ++ "." ++
    integer_to_list(B3) ++ "." ++
    integer_to_list(B4);
print_ip(Other) ->
    lists:flatten(io_lib:format("~p",[Other])).

-----Original Message-----
From: erlang-questions-bounces@REDACTED
[mailto:erlang-questions-bounces@REDACTED] On Behalf Of Mats Cronqvist
Sent: Friday, January 12, 2007 12:05 PM
To: erlang-questions@REDACTED
Subject: Re: [erlang-questions] Open TCP ports

attila.rajmund.nohl@REDACTED wrote:
> Hello!
> 
> Is there a way to determine that which erlang processes are listening 
> on a given TCP port? The operating system tells me that the erlang VM 
> is listening on that port, but I need to know that which erlang 
> process is using that port.

   inet:i().
_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://www.erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list