girng

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:

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

Marked As Solved

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

Also Liked

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:

girng

girng

@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

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.

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.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement