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…

Last Post!

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

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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