0

i've read docs for 'inet', 'gen_tcp', but can't understand where is error.

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}

so it must be {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}}]}]}

or

(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).

{error,einval}

1 Answers1

0

If you check the gen_tcp doc again, you will see that when working with ipv6 address, you need to add the Socket Option inet6, which you are missing on your code

Try this code, it should work:

Op = [binary, {packet,line}, {active,false}, {reuseaddr,true}, inet6, {ip, {0,0,0,0,0,0,0,1}}].
gen_tcp:listen(8000, Op).
rorra
  • 9,593
  • 3
  • 39
  • 61
  • ok, but this still gives an error 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). ** exception error: no match of right hand side value {error,einval} – Alexander Shavelev May 30 '17 at 06:55
  • f(Op). ? You are trying to assign a variable to different values, like: A = 1. A = 2. That is the way to get that right hand side value error. – rorra May 30 '17 at 14:14