0

我已经阅读了'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).

{错误,无效}

4

1 回答 1

0

如果您再次检查gen_tcp文档,您将看到在使用 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).
于 2017-05-29T17:08:38.333 回答