Aetherus
I wrote a GenServer that wraps a gen_tcp socket and does some long running tasks involving reading and writing on the socket.
defmodule MyClient do
use GenServer
@impl true
def init(_) do
{:ok, nil, {:continue, :connect}}
end
@impl true
def handle_continue(:connect, _) do
{:ok, socket} = :gen_tcp.connect(@ip, @port, [
mode: :binary, # <-- NOTICE THIS CONFIG
packet: 0,
keepalive: true,
active: false,
reuseaddr: true,
send_timeout: 5000,
send_timeout_close: true
])
send(self(), :work)
{:noreply, socket}
end
@impl true
def handle_info(:work, socket) do
{:ok, data} = :gen_tcp.recv(socket, 4) # <-- The problematic line
...
{:noreply, socket}
end
end
When I set the mode to :list, it works fine.
When I set the mode to :binary, the line :gen_tcp.recv always returns {:error, :closed}. I wonder why. Did I messed up the configuration?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
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
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ityonemo
“Argument Length is only meaningful when the socket is in raw mode and denotes the number of bytes to read. If Length is 0, all available bytes are returned. If Length > 0…”
your socket isn’t in raw mode! You may want to consider just passing length 0, otherwise whatever is incoming is going to cause a problem.
Aetherus
I’ve tried
packet: :raw, but still no luck.ityonemo
are you trying to read out 4 bytes that is going to tell you how many more bytes are coming in the packet? There’s a special mode for that. let me see if I can find that. (I think you generally don’t want to be using raw).
Aetherus
Yes and no. The actual packet size is in the 3rd and 4th byte, in little endian (I just have no right to change the protocol
)
By the way, the first 2 bytes is a fixed magic number
<<0x68, 0x16>>.ityonemo
I would say pass 0 and use pattern matching to trim the binary down to the size you care about. I don’t know what the BEAM does about malicious packets that are too long…
Aetherus
Sadly, there’s no EOF. The sizes of the packets vary. All multi-byte numbers are in little endian. And there’s a damn footer that contains a checksum!! I would definitely trash this stupid protocol and implement a standard one, only if I could. At the moment, my only hope is the raw mode.
ityonemo
that’s okay, the beam will reconstitute the packets with appropriate boundaries as sent over TCP, so unless the protocol is doing something really wild and smushing packets together in strange ways, your recv result in should be matchable to
{:ok, <<0x66, 0x16, length::16, result::binary-size(length)>>}(with total length length + 4)ityonemo
oh you might need to do an endianness check on the length match.
Aetherus
That’s not an option, either. The socket should be always open (unless something unexpected happens), and over time, there’ll be indefinitely many reads and writes on it.
Aetherus
Yeah, I did it. I just didn’t write it in this question.