girng

girng

hi all! i’ve been on elixir’s irc but can’t really get anything working. so i decided to join the forum.
i’m from the crystal lang repo, i’m very new to elixir but i do like it.

i’m having trouble with my tcp server, and read and send messages from the tcp stream. i’m using this code from the clientside:
https://github.com/godotengine/godot/blob/b48cc669e29ba149664b1e1d6003e112f19a9dff/core/io/stream_peer.cpp#L211

it sends a unisgned 32 bit, that is the length of the message (clientside) i need to read this from my elixir tcp server:

defmodule Server do
  def start(port) do
    tcp_options = [:binary, {:packet, 0}, {:active, false}]
    {:ok, socket} = :gen_tcp.listen(port, tcp_options)
    IO.puts "Started Master Server on port: #{port}"
    listen(socket)
  end

  def listen(socket) do
    {:ok, conn} = :gen_tcp.accept(socket)
    spawn(fn -> recv(conn) end)
    listen(socket)
  end

  def recv(conn) do
    IO.puts "New connection: #{inspect(conn)}"
    test_message = "hello clarice"
    #:gen_tcp.send(conn, Integer.to_string( byte_size(test_message)) )
    :gen_tcp.send(conn, test_message)
    :gen_tcp.send(conn, test_message)
    case :gen_tcp.recv(conn, 0) do
      {:ok, data} ->
       :gen_tcp.send(conn, data)
       IO.puts "Received: #{data}"
       recv(conn)
      {:error, :closed} -> :ok
        IO.puts "#{inspect(conn)} has disconnected"
    end
  end
end

Server.start(1234)

the problem I have is, my tcp option :packet, only goes up to 4. i need it to be 32, but it won’t let me. i also tried to send the the byte_size befoe the message (line 18 commented out) but that doesn’t work either. i’m not sure what i’m doing wrong, or how i can read a unsigned 32 bit flag from a tcp stream.

thank you in advance

Showing Posts 1 to 7

NobbZ

NobbZ

As far as I remember 32 bits are equivalent to 4 bytes, so just do packet: 4. From my understanding that should work. I can’t test right now, as I’m not near to a computer.

girng

girng OP

cool NobbZ, i tried 4, but when reading the data from the stream in godot, it freezes. i’m using the godot/core/io/stream_peer.cpp at b48cc669e29ba149664b1e1d6003e112f19a9dff · godotengine/godot · GitHub method

does elixir send the 32 unsigned bit before the string internally, or do i need to send it before calling :gen_tcp.send?
for example, something like:

:gen_tcp.send(conn, byte_size("Welcome to the server!"))
:gen_tcp.send(conn, "Welcome to the server!")

i just tried this, but am getting 04:12:43.630 [error] Bad value on output port 'tcp_inet'hmmm

NobbZ

NobbZ

I haven’t used TCP in Erlang or elixir yet, but usually the package length info is prepended without the users ado. You need to make sure though to always send complete data.

girng

girng OP

yeah, unfortunately {packet, 4} freezes Godot. when i use get_u32() from here.

not sure how to go about making godot work with a simple tcp echo server in elixir. i am sadly stuck and not sure what to do, i spent all day yesterday in the elixir-lang irc trying to get this to work but with no luck. i’m lost sorry.

looks like if i change {packet, 4} to {packet, 2} and use godot’s get_u8() it works, but the messages are not correct. it’s all being delivered with distorted strings, etc. something’s not right , i need to have a 32 bit receiving and sending i think.

i don’t know how to send/receive a 32 bit signed tcp message with elixir :frowning:

NobbZ

NobbZ

The header length can be one, two, or four bytes, and containing an unsigned integer in big-endian byte order.

From Erlang’s documentation. Does your other application use big endian as well?

If it sends little endian, you’ll need to selfimplement package chunking over package: :raw.

Please try to send the same package from both, elixir and your other program and compare the actual sent package via tcpdump

girng

girng OP

@NobbZ it fixed it sir!

On Godot, in my StreamPeer class (TCP Server) i set big_endian = true

and used {packet, 4} for tcp options in gen_tcp. (4 bytes = 32 bits)

thank you sir for staying with me… love the help! <3

NobbZ

NobbZ

Endianess is number two on the list to check when handling with integers on different systems…

The first thing is the width (which you already checked) and the third thing is signedness, but to be honest, I never had to look into it, its been always one of the the first both :wink:

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews