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:

PatNowak

PatNowak

Just small note that from Elixir 1.5 you should use String.to_charlist and previous version - to_char_list is deprecated.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement