In Erlang how to get Client's ip and port?

2019-05-07 08:45发布

In the following code , server is listening to port 2345. After accepting connection from client it returns {ok, Socket}

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

I want to get client's IP and port, how can I get them by analyzing socket?

标签: tcp erlang
1条回答
贼婆χ
2楼-- · 2019-05-07 09:11

Use inet:peername/1. The description of the function from documentation:

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.
查看更多
登录 后发表回答