code

code

Why does the program exit when I try to take input?

def handle_client(client) do
        msg = IO.gets("> ") |> String.trim
        :gen_tcp.send(client, buffering(msg))
        :gen_tcp.send(client, msg)
end

When I replace the function that takes input(I tried IO.gets/1 and IO.read/2) with a normal string, the message is sent normally.

Here is the function that listens and accepts connections(irrelevant, I think ?)

def listen_and_accept(ip_addr) do
        case :gen_tcp.listen(8000, [:binary, active: false, reuseaddr: true, ip: ip_addr]) do
            {:ok, server} ->
                IO.puts "Listening..."
                case :gen_tcp.accept(server) do
                    {:ok, client} ->
                        IO.puts "Client accepted"
                        spawn fn -> handle_client(client) end
                    {:error, reason} ->
                        IO.puts "Error in :gen_server.accept: #{reason}"
                end
            {:error, reason} -> IO.puts "Error in :gen_tcp.listen: #{reason}"
        end
end

Thanks in advance guys.

Marked As Solved

aseigo

aseigo

IO.gets/2 takes a device and a prompt, default to stdio, which is a shortcut to Process.group_leader/0 according to the docs … the spawn changes the process group leader and so it probably just sits there forever waiting for I/O from a process that never comes.

If you wish to send user input from the console to the connection, you’re going to probably want to send msgs to the connection handlers from a process accepting input from a console.

Also Liked

aseigo

aseigo

That’s about right, yes. You’ll want to take input from console, and then use message passing to get it into your client handler process.

However, that is a bit of an odd pattern in your code. Essentially, what it is doing is waiting for a client connection, then spawning an asynchronous process to handle that connection while the next connections may come in. So you can easily have N of these handlers running concurrently. Which one gets which user input?

If you really want to handle one connection at a time with user interaction, then don’t spawn the client connection handler (avoiding creating a separate process) and just handle it as you have now all in the main process (which presumably you are running from the REPL?).

But yes, that’s why what you have isn’t working as you expected, though the solution may involve changing how your program works a bit to actually fit your goals :slight_smile:

Welcome to Elixir, btw… it’s certain to be a fun journey!

Last Post!

code

code

Thanks for your help man, and yeah elixir is fun indeed.

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