确定Erlang中传入的TCP / IP连接的IP地址和端口 [英] Determining IP address and port of an incoming TCP/IP connection in Erlang
问题描述
我想获取一个传入的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.
这篇关于确定Erlang中传入的TCP / IP连接的IP地址和端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!