我已阅读' ,' gen_tcp' 的文档,但无法理解哪里是错误。
connect_option()= {ip,inet:socket_address()}
socket_address()= IP-地址()
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}
答案 0 :(得分:0)
如果您再次查看gen_tcp文档,您会看到在使用ipv6地址时,您需要添加套接字选项 inet6 ,这是您在代码中缺少的 p>
试试这段代码吧:
Op = [binary, {packet,line}, {active,false}, {reuseaddr,true}, inet6, {ip, {0,0,0,0,0,0,0,1}}].
gen_tcp:listen(8000, Op).