Koszal
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
Trending in Challenges
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 2- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
Koszal
-Nhelps. Fromman nc:trisolaran
nevermind