本文介绍了在Erlang中如何获取客户端的ip和端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

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

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

推荐答案

使用 inet:peername / 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.

这篇关于在Erlang中如何获取客户端的ip和端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:13