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

∥☆過路亽.° 提交于 2020-01-12 07:21:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/1240312/determining-ip-address-and-port-of-an-incoming-tcp-ip-connection-in-erlang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!