确定Erlang中传入的TCP / IP连接的IP地址和端口 [英] Determining IP address and port of an incoming TCP/IP connection in Erlang

查看:618
本文介绍了确定Erlang中传入的TCP / IP连接的IP地址和端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个传入的TCP / IP连接的IP地址和端口号。不幸的是, gen_tcp 接受 recv 函数只返回一个套接字,而 gen_udp recv 函数也会返回地址信息。有没有一种直接的方法来收集属于Erlang的套接字的地址信息?

I would like to fetch the IP address and port number of an incoming TCP/IP connection. Unfortunately gen_tcp's accept and recv functions only give back a socket, while gen_udp's recv function also gives back the address information. Is there a straightforward way to collect address information belonging to a socket in Erlang?

推荐答案

您需要 inet / peername 1 。从 Erlang inet文档

peername(Socket) - > {ok,{Address,Port}} | {error,posix()}

peername(Socket) -> {ok, {Address, Port}} | {error, posix()}

类型:

Socket = socket()
Address = ip_address()
Port = int()

Socket = socket() Address = ip_address() Port = int()

返回连接另一端的地址和端口。

Returns the address and port for the other end of a connection.

这篇关于确定Erlang中传入的TCP / IP连接的IP地址和端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆