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 3 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
This topic is about the Advent of Code 2021 - Day 4.
Thanks to @bjorng , we now have a new Private Leaderboard.
The entry code is:
370...
New
Phew, this one took a while to get right. My naive attempts was way to slow so I reached for Dijkstras shortest path algorithm… and that ...
New
This topic is about Day 18 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
New
Ok, that was a rough one today.
I haven’t found a way to improve the algorithm further. Part 1 runs in .5 seconds, Part 2 in ~ 5 minutes...
New
This topic is about Day 4 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
Anyone else think the prompt for this challenge is contradictory?
The rules for comparing packets include
If both values are lists, c...
New
Fairly straightforward Dijkstra’s algorithm
import AOC
aoc 2023, 17 do
def compute(input, candidates) do
{{max_row, max_col}, ite...
New
Took a while, but another use case for “move vectors” today and pattern matching. :slight_smile:
The trick was to first generate a list...
New
Hello all, hopefully I post this before someone else does and I don’t dupe.
IMO Day 4 was much easier than Day 3 (yay, I can sleep befor...
New
Other popular topics
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
I would like to know what is the best IDE for elixir development?
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
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
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
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
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







