繁体   English   中英

无法连接到特定的网络接口

[英]Can't connect to specific network interface

我已经阅读了有关'inet''gen_tcp'的文档,但无法理解错误在哪里。

connect_option()= {ip,inet:socket_address()}

socket_address()= ip_address()

ip_address()= ip4_address()| ip6_address()

ip6_address()= {0..65535,0..65535,0..65535,0..65535,0..65535,0..65535,0..65535,0..65535}

因此它必须是{ip,{0..65535,0..65535,0..65535,0..65535,0..65535,0..65535,0..65535,0..65535}}

(aaa@127.0.0.1)8> Ip = {65152,0,0,0,51840,11332,54567,49336}.
{65152,0,0,0,51840,11332,54567,49336}
(aaa@127.0.0.1)9> gen_tcp:connect({127,0,0,1}, 6653, [binary, {packet, raw}, {active, false}, {ip, Ip}]).
{error,eafnosupport}
(aaa@127.0.0.1)10> gen_tcp:connect({0,0,0,0,0,0,0,1}, 6653, [binary, {packet, raw}, {active, false}, {ip, Ip}]).
** exception exit: badarg
     in function  gen_tcp:connect/4 (gen_tcp.erl, line 148).

inet:getifaddrs().
{ok,[{"lo",
      [{flags,[up,loopback,running]},
       {hwaddr,[0,0,0,0,0,0]},
       {addr,{127,0,0,1}},
       {netmask,{255,0,0,0}},
       {addr,{0,0,0,0,0,0,0,1}},
       {netmask,{65535,65535,65535,65535,65535,65535,65535,
                 65535}}]},
     {"eth0",
      [{flags,[up,broadcast,running,multicast]},
       {hwaddr,[82,84,0,229,5,188]},
       {addr,{172,17,0,218}},
       {netmask,{255,255,255,128}},
       {broadaddr,{172,17,0,255}},
       {addr,{65152,0,0,0,20564,255,65253,1468}},
       {netmask,{65535,65535,65535,65535,0,0,0,0}}]},
     {"eth1",
      [{flags,[up,broadcast,running,multicast]},
       {hwaddr,[82,84,0,229,5,189]},
       {addr,{65152,0,0,0,51840,11332,54567,49336}},
       {netmask,{65535,65535,65535,65535,0,0,0,0}}]}]}

要么

(aaa@127.0.0.1)9> Op.
[binary,
 {packet,raw},
 {active,false},
 {reuseaddr,true},
 {ip,{65152,0,0,0,51840,11332,54567,49336}}]
(aaa@127.0.0.1)10> gen_tcp:listen(6653, Op).

{错误,EINVAL}

如果再次检查gen_tcp doc,您会看到在使用ipv6地址时,需要添加Socket Option inet6 ,您的代码中缺少该选项

试试下面的代码,它应该可以工作:

Op = [binary, {packet,line}, {active,false}, {reuseaddr,true}, inet6, {ip, {0,0,0,0,0,0,0,1}}].
gen_tcp:listen(8000, Op).

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM