在以下代码中,服务器正在侦听端口2345.在接受来自客户端的连接后,它返回{ok, Socket}
start() ->
{ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 4},
{reuseaddr, true},
{active, true}]),
{ok, Socket} = gen_tcp:accept(Listen).
我想获取客户端的IP和端口,如何通过分析套接字来获取它们?
答案 0 :(得分:5)
使用inet:peername/1
。文档中的功能描述:
peername(Socket) -> {ok, {Address, Port}} | {error, posix()}
Types:
Socket = socket()
Address = ip_address()
Port = integer() >= 0
Returns the address and port for the other end of a connection.