In Erlang, how do I get the client ip and port?

In the following code, the server listens on port 2345. After accepting a connection from the 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 the client IP and port, how can I get them by parsing the socket?

+3


source to share


1 answer


Use inet:peername/1

. Function description 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.

      

+4


source







All Articles