Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining IP address and port of an incoming TCP/IP connection in Erlang

I would like to fetch the IP address and port number of an incoming TCP/IP connection. Unfortunately gen_tcp's accept and recv functions only give back a socket, while gen_udp's recv function also gives back the address information. Is there a straightforward way to collect address information belonging to a socket in Erlang?

like image 544
hji Avatar asked Aug 06 '09 17:08

hji


1 Answers

You need inet/peername 1. From the Erlang inet docs:

peername(Socket) -> {ok, {Address, Port}} | {error, posix()}

Types:

Socket = socket() Address = ip_address() Port = int()

Returns the address and port for the other end of a connection.

like image 135
ire_and_curses Avatar answered Nov 03 '22 15:11

ire_and_curses