Koszal
Protohackers in Elixir - day 0
I followed a tutorial and created a gen_tcp server. The code is shown below. The server accepts connections, receives data but the function recv_until_closed (at the very bottom) never matches on {:error, :closed}. Consequently, the connections are never closed. Instead I eventually get timeout errors. I’m testing it by echoing a string to nc localhost 5001 as shown in the video (around 17:30)
This is the first video in the Photohackers in Elixir series
Is there some obvious error in my code that I’m failing to see?
defmodule EventListener.Server do
use GenServer
require Logger
def start_link([] = _opts) do
GenServer.start_link(__MODULE__, :no_state)
end
defstruct [:listen_socket]
@impl true
def init(:no_state) do
listen_options = [
ifaddr: {0, 0, 0, 0},
mode: :binary,
active: false,
reuseaddr: true,
exit_on_close: false
]
case :gen_tcp.listen(5001, listen_options) do
{:ok, listen_socket} ->
state = %__MODULE__{listen_socket: listen_socket}
{:ok, state, {:continue, :accept}}
{:error, reason} ->
{:stop, reason}
end
end
@impl true
def handle_continue(:accept, %__MODULE__{} = state) do
case :gen_tcp.accept(state.listen_socket) do
{:ok, socket} ->
handle_connection(socket)
{:noreply, state, {:continue, :accept}}
{:error, reason} ->
{:stop, reason}
end
end
defp handle_connection(socket) do
case recv_until_closed(socket, _buffer = "") do
{:ok, data} ->
:gen_tcp.send(socket, data)
{:error, reason} ->
Logger.error("Failed to receive data: #{inspect(reason)}");
end
:gen_tcp.close(socket)
end
defp recv_until_closed(socket, buffer) do
case :gen_tcp.recv(socket, 0, 10_000) do
{:ok, data} ->
recv_until_closed(socket, [buffer, data])
{:error, :closed} ->
{:ok, buffer}
{:error, reason} -> {:error, reason}
end
end
end
Most Liked
Koszal
I’m testing it by echoing a string to
nc localhost 5001as shown in the video
-N helps. From man nc:
-N
shutdown(2) the network socket after EOF on the input. Some servers require this to finish their work.
1
Popular in Challenges
This topic is about Day 9 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
Nobody’s doing Advent of Code this year? :smile:
I might do the first week or so.
For Day 1, first I solved it using regular expression...
New
Note by the Moderators: This topic is to talk about the first day of the Advent of Code.
For general discussion about the Advent of Code...
New
This topic is about Day 15 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code.
For general discussion about the Advent of Code 2018 an...
New
Note: This topic is to talk about Day 25 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can joi...
New
Trying to get more facility with dynamic programming concepts on Leetcode and having an issue I can’t find a way around. It’s a chutes an...
New
Note by the Moderators: This topic is for general discussion about the Advent of Code 2018.
To prevent people from being spoiled about s...
New
This topic is about Day 6 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums ):
https://adve...
New
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby.
Anyway, here is my solution:
New
Other popular topics
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
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
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
I would like to know what is the best IDE for elixir development?
New
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
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New








