在Erlang中如何获取客户端的ip和端口?

jia*_*wei 4 erlang tcp

在以下代码中,服务器正在侦听端口2345.在接受来自客户端的连接后,它将返回 {ok, Socket}

start() ->  
{ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 4},  
                                  {reuseaddr, true},  
                                  {active, true}]),  
{ok, Socket} = gen_tcp:accept(Listen).
Run Code Online (Sandbox Code Playgroud)

我想获得客户端的IP和端口,如何通过分析套接字获取它们?

Dan*_*nko 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.
Run Code Online (Sandbox Code Playgroud)