code

code

Why am I getting an error in gen_tcp.listen/2?

Here is my code:

defmodule Server do
    def listen_for_con(ip_addr) do
        case :gen_tcp.listen(8000, [:binary, active: false, reuseaddr: true, ip: ip_addr]) do
            {:ok, server} -> IO.puts "Server is listening"
            {:error, reason} -> IO.puts "Error in :gen_tcp.listen: #{reason}"
        end
    end
end

Server.listen_for_con("example_ip_addr")

Here is the error that I get when I try to run the code:


(exit) :badarg
(kernel 7.2) inet_tcp.erl:160: :inet_tcp.listen/2
server2.exs:3: Server.listen_and_accept/1
(elixir 1.11.2) lib/code.ex:931: Code.require_file/2

I do not know what is wrong with the arguments I am giving to the function.

It is a script and I run it as follows: elixir server.exs

Thanks in advance guys.

Marked As Solved

voltone

voltone

You need to specify the IP address as a 4- or 6-tuple, e.g. {127, 0, 0, 1}.

You can use inet:parse_address/1 to parse a human-readable address, just note that this is an Erlang API so it expects a charlist, e.g. :inet.parse_address('127.0.0.1').

Also Liked

voltone

voltone

The official documentation is in the inet reference manual you found, and a small section of the kernel user guide. There’s also a chapter on sockets in the LYSE book, which may help you get started.

Both of these assume familiarity with Erlang syntax, which is often the case when you get to such lower-level APIs. There may be some Elixir-oriented blog posts out there, I don’t know…

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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