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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews