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:
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.
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.
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.
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
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