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

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
nseaSeb
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
kpanic
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
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
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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews