TCP listener on Elixir using the Erlang gen_tcp module

I use the following code to create a TCP listener on elixir:

defmodule KVServer do
use Application

@doc false
def start(_type, _args) do
  import Supervisor.Spec

  children = [
    supervisor(Task.Supervisor, [[name: KVServer.TaskSupervisor]]),
    worker(Task, [KVServer, :accept, [4040]])
  ]

  opts = [strategy: :one_for_one, name: KVServer.Supervisor]
  Supervisor.start_link(children, opts)
end

@doc """
  Starts accepting connections on the given `port`.
"""
def accept(port) do
  {:ok, socket} = :gen_tcp.listen(port,
                  [:binary, packet: :line, active: false, reuseaddr:     true])
  IO.puts "Accepting connections on port #{port}"
  loop_acceptor(socket)
end

defp loop_acceptor(socket) do
  {:ok, client} = :gen_tcp.accept(socket)
  {:ok, pid} = Task.Supervisor.start_child(KVServer.TaskSupervisor, fn -> serve(client) end)
  :ok = :gen_tcp.controlling_process(client, pid)
  loop_acceptor(socket)
end

defp serve(socket) do
  socket
  |> read_line()
  |> write_line(socket)

  serve(socket)
end

defp read_line(socket) do
  {:ok, data} = :gen_tcp.recv(socket, 0)
  data
end

defp write_line(line, socket) do
  :gen_tcp.send(socket, line)
end
end

This is taken from the following link: http://elixir-lang.org/getting-started/mix-otp/task-and-gen-tcp.html

When I try to get data from my gps development (for which I am writing this piece of code) using: gen_tcp.recv (socket, 0), I get an error message: {:error, reason} = :gen_tcp.recv(socket, 0)and the reason it was shown is simply "closed" .

However, the device sends the data that I confirmed using the tcp packet sniffer (tcpflow).

Also, when I try to send data using telnet, as described in the tutorial above, it works fine.

Any help would be greatly appreciated.

+4
1

- . , . "packet:: line" : gen_tcp.listen ":: raw".

telnet, telnet ( ).

+6

Source: https://habr.com/ru/post/1613736/


All Articles