T
T
TriKrista2019-10-23 21:21:40
Erlang
TriKrista, 2019-10-23 21:21:40

How to send tcp request from Qt client to erlang server?

there is a server:

-module(server).

-export([start/0]).

start() ->
    {ok, ListenSocket} = gen_tcp:listen(1234, [binary, {active, false}, {packet, 4}]),
    wait_connect(ListenSocket, 0).

wait_connect(ListenSocket, Count) ->
    {ok, Socket} = gen_tcp:accept(ListenSocket),
    spawn(?MODULE, wait_connect, [ListenSocket, Count+1]),
    get_request(Socket).

get_request(Socket) ->
    case gen_tcp:recv(Socket, 0) of
       {ok, Binary} ->
            Term = binary_to_term(Binary), %% завершается с ошибкой
            Size = tuple_size(Term),
            io:format("Term: ~p Size: ~p Binary: ~p~n", [Term, Size, Binary]),
            gen_tcp:close(Socket);
       {error, closed} ->
            error_logger:info_msg("Error", [])
    end.

a request is sent to it like this
//...
m_pTcpSocket = new QTcpSocket(this);
//...
QByteArray arrBlock;
QDataStream out(&arrBlock, QIODevice::WriteOnly);
ei_x_buff result;
ei_x_new_with_version(&result);
ei_x_encode_tuple_header(&result, 1);
ei_x_encode_longlong(&result, 1555);
out << result.buff;
m_pTcpSocket->write(arrBlock);

resulting in this error:
** exception error: bad argument
     in function  binary_to_term/1
        called as binary_to_term(<<131,104,1,98,0>>)
     in call from server:get_request/1 (server.erl, line 18)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2019-10-24
@begemot_sun

You have a problem on the side of your Qt code. Namely, incorrect serialization into Erlang structures.
See link: erlang.org/doc/apps/erts/erl_ext_dist.html
for a description of this format.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question