确定Erlang中传入的TCP / IP连接的IP地址和端口

确定Erlang中传入的TCP / IP连接的IP地址和端口

问题描述:

我想获取一个传入的TCP / IP连接的IP地址和端口号。不幸的是, gen_tcp 接受 recv 函数只返回一个套接字,而 gen_udp recv 函数也会返回地址信息。有没有一种直接的方法来收集属于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?

您需要 inet / peername 1 。从 Erlang inet文档

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

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

类型:

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

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

返回连接另一端的地址和端口。

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