无法连接到特定的网络接口
问题描述:
我已阅读文档'inet','gen_tcp',但无法理解错误在哪里。无法连接到特定的网络接口
connect_option()= {IP,INET:socket_address()}
socket_address()= IP-地址()
IP-地址()= 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}}
([email protected])8> Ip = {65152,0,0,0,51840,11332,54567,49336}.
{65152,0,0,0,51840,11332,54567,49336}
([email protected])9> gen_tcp:connect({127,0,0,1}, 6653, [binary, {packet, raw}, {active, false}, {ip, Ip}]).
{error,eafnosupport}
([email protected])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}}]}]}
或
([email protected])9> Op.
[binary,
{packet,raw},
{active,false},
{reuseaddr,true},
{ip,{65152,0,0,0,51840,11332,54567,49336}}]
([email protected])10> gen_tcp:listen(6653, Op).
{错误,EINVAL}
答
如果再次检查gen_tcp文档,你会看到,使用IPv6地址的工作时,你需要添加套接字选项INET6,你在代码上缺少的是
试试这个代码,它应该工作:
Op = [binary, {packet,line}, {active,false}, {reuseaddr,true}, inet6, {ip, {0,0,0,0,0,0,0,1}}].
gen_tcp:listen(8000, Op).
ok,但是仍然给出错误 Op = [binary,{packet,line},{active,false},{reuseaddr,true},inet6,{ip,{65152,0,0,0, 51840,11332,54567,49336}}]。 {ok,L} = gen_tcp:listen(6653,Op)。 **异常错误:右侧值不匹配{error,einval} –
f(Op)。 ?您正在尝试将变量分配给不同的值,如: A = 1。A = 2。这是获得右侧值错误的方法。 – rorra