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

不想你离开。 提交于 2019-12-22 08:46:50

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/30389804/in-erlang-how-to-get-clients-ip-and-port

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