jononomo

jononomo

Do I have to provide an actual numerical IP address to :gen_tcp.connect?

I have the following line in an elixir module:

{:ok, socket} = :gen_tcp.connect("my-appname-1842614232.us-east-1.elb.amazonaws.com", 4000, opts)

The error I am getting is: “evaluator process exited with reason: :badarg”

It worked fine when I had the line like this:

{:ok, socket} = :gen_tcp.connect({10, 247, 4, 104}, 4000, opts)

So my question is – do I have to use an actual IP address?

Marked As Solved

NobbZ

NobbZ

According to the documentation it has to be inet:hostname() which is string(), which in elixir means char_list, so your hostname has to be 'example.com'. Alternatively you can also use the c-sigil (~c[example.com])

Also Liked

entone

entone

notice the single quotes ' instead of double "

Generally when interacting directly with Erlang libs, they expect char lists

NobbZ

NobbZ

Yes, they are.

The spec of :gen_tcp.connect/3:

connect(Address, Port, Options) → {ok, Socket} | {error, Reason}

Types

  • Address = inet:socket_address() | inet:hostname()
  • [other types omitted since they are unimportant here]

inet:hostname() (which is :inet.hostname in elixir syntax) is defined as follows:

hostname() = atom() | string()

Erlangs string() type is equal to elixirs charlist type.

charlists can be created using the charlist literal, which is an single-quoted “string”. Also one can use the c and C sigils to create them, when one wants to be a little bit more explicit.

And as @PatNowak correctly tells, you can convert anything implementing List.Chars by passing it to Kernel.to_charlist/1

Nicd

Nicd

You should mark this thread as solved so others can see the answer right away and see it as solved in the thread list. :slight_smile:

Last Post!

jononomo

jononomo

thanks - seems so obvious now.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement