Erlang:查找我的IP地址 [英] Erlang: Finding my IP Address

查看:248
本文介绍了Erlang:查找我的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Redis为某些部分完成负载均衡器/登录服务器/游戏服务器设置。负载均衡是其中之一。在我的Redis负载平衡实例中,我使用有序集。关键是应用程序名称,成员是游戏服务器的IP地址。



这里是我的问题。我想在erlang内使用一种公共方法。我找不到符合我需要的东西我想知道我是否在看东西。

  {ok,L} = inet:getif(),
IP = element(1,hd(L)),

给我看看我在看什么对于。我相信目前是{192,168,0,14}。但是该函数不是public。

  {ok,Socket} = gen_tcp:listen(?PORT_LISTEN_GAME,[{active,一次},{reuseaddr,true}]),
{ok,{IP,_} = inet:sockname(Socket),

给我{0,0,0,0}。我试过 inet:getaddr(owl),它给我{127,0,1,1}。



我只限于通过TCP发送消息,并使用 inet:peername(Socket)?似乎很多东西要这么简单。我的应用程序的所有不同部分都运行在同一台计算机上进行测试。是否会让我回来{127,0,0,1}?那不行。我需要将IP发送给用户(我的手机),以便他们可以与正确的服务器进行链接。环回不会做....



当前代码



喜欢感谢所有的回应。是的,我在新年之后注意到了Lol4t0的评论。所以我改变了我的代码来反映出来。发布这个像慢慢的人像我自己。我必须大肆夸大我的大脑,让这些东西点击。

  hd([Addr || {_, }<  -  Addrs,
{addr,Addr}< - 选项,
{flags,Flags}< - 选项,
列表:member(loopback,Flags)= / = true ])。


解决方案

我们已成功使用此功能获取第一个非本地IPv4地址:

  local_ip_v4() - > 
{ok,Addrs} = inet:getifaddrs(),
hd([
Addr || {_,Opts}< - Addrs,{addr,Addr}
size(Addr)== 4,Addr = / = {127,0,0,1}
])。

当然可以更改返回IPv6,如果这是你想要的。 >

I'm attempting to complete a Load Balancer / Login Server / Game Server setup using Redis for some parts. Load balancing is one of them. In my Redis load balancing instance I'm using ordered sets. The key is the application name, the members are the IP addresses of the game servers.

Herein lies my issue. I would like to use a public method within erlang. I cannot find anything that fits my needs. I'm wondering if I'm over looking something.

{ok, L} = inet:getif(),
IP = element(1, hd(L)),

Gives me what I'm looking for. I believe currently it's {192,168,0,14}. But the function is not "public."

{ok, Socket} = gen_tcp:listen(?PORT_LISTEN_GAME, [{active,once}, {reuseaddr, true}]),
{ok, {IP, _} = inet:sockname(Socket),

Gives me {0,0,0,0}. I've tried inet:getaddr("owl") which gives me {127,0,1,1}.

Am I limited to sending messages via TCP and using inet:peername(Socket)? Seems like a lot to get something so simple. All the different parts of my app are running on the same computer for testing. Is it going to give me back {127,0,0,1}? That wouldn't work. I need to send the IP back to the user (my mobile phone) so they can link up with the proper server. Loopback wouldn't do....

Current Code

I would like to thank all the responses. Yes, I noticed Lol4t0's comment just after the New Year. So I changed my code to reflect that. Posting this for the slow people like myself. I have to wrack my brain for a bit to get these things to click.

hd([Addr || {_, Opts} <- Addrs,
    {addr, Addr} <- Opts,
    {flags, Flags} <- Opts,
    lists:member(loopback,Flags) =/= true]).

解决方案

We've been successfully using this function to get the first non-local IPv4 address:

local_ip_v4() ->
    {ok, Addrs} = inet:getifaddrs(),
    hd([
         Addr || {_, Opts} <- Addrs, {addr, Addr} <- Opts,
         size(Addr) == 4, Addr =/= {127,0,0,1}
    ]).

It can of course be changed to return IPv6 as well if that is what you want.

这篇关于Erlang:查找我的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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