[英]Retrieve client IP in :ranch
I'm using :ranch to receive TCP packges in my Phoenix application. 我正在使用:ranch在我的Phoenix应用程序中接收TCP打包。
First I created a listener in the server using: 首先,我使用以下命令在服务器中创建了一个侦听器:
:ranch.start_listener(tcp_echo, ranch_tcp, [{port, 5555}], echo_protocol, [] )
How could I print the client IP in the echo_protocol
? 如何在
echo_protocol
打印客户端IP?
Ranch includes a sample echo application . 牧场包括一个示例回声应用程序 。
In echo_protocol.erl
, there's an init function that accepts the connection from the client: 在
echo_protocol.erl
,有一个初始化函数可以接受来自客户端的连接:
init(Ref, Socket, Transport, _Opts = []) ->
ok = ranch:accept_ack(Ref),
loop(Socket, Transport).
You can display the client ip address there: 您可以在此处显示客户端IP地址:
init(Ref, Socket, Transport, _Opts = []) ->
ok = ranch:accept_ack(Ref),
{ok, {IpAddress, _}} = inet:peername(Socket),
io:format("Client ~p~n", [IpAddress]),
loop(Socket, Transport).
It will appear in a format like: 它将以如下格式显示:
Client {127,0,0,1}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.