girng
How to read a 32 bit unsigned integer on tcp stream?
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
Trending in Questions
Other Trending Topics
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
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First 7 of 7 Posts!
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
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:
i just tried this, but am getting
04:12:43.630 [error] Bad value on output port 'tcp_inet'hmmmNobbZ
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
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
NobbZ
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
tcpdumpgirng
@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
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