Erlang gen_tcp gets nothing

Im trting to receive data on the client side, but received nothing.

The code of the server that sends the message

client(Socket, Server) ->
  gen_tcp:send(Socket,"Please enter your name"),
  io:format("Sent confirmation"),
  {ok, N} = gen_tcp:recv(Socket,0),
  case string:tokens(N,"\r\n") of
    [Name] ->
      Client = #client{socket=Socket, name=Name, pid=self()},
      Server ! {'new client', Client},
      client_loop(Client, Server)
  end.

Client who should receive and print

    client(Port)->
  {ok, Sock} = gen_tcp:connect("localhost",Port,[{active,false},{packet,2}]),
  A = gen_tcp:recv(Sock,0),
  A.
+1
source share
1 answer

I think your client is faulty because it indicates:

{packet, 2}

while the server indicates (not shown in code):

{packet, 0}

In Programming Erlang (2nd)on with. 269 ​​it says:

Note that the package arguments used by the client and server must be consistent. If the server was opened using {packet,2}and the client was opened using {packet,4}, then nothing will work.

The following client can successfully receive text from the server:

%%=== Server:  {active,false}, {packet,0} ====

client(Port) ->
    {ok, Socket} = gen_tcp:connect(
        localhost, 
        Port, 
        [{active,false},{packet,0}]
     ),

    {ok, Chunk} = gen_tcp:recv(Socket, 0),
    io:format("Client received: ~s", [Chunk]),
    timer:sleep(1000),

    Name = "Marko",
    io:format("Client sending: ~s~n", [Name]),
    gen_tcp:send(Socket, Name),

    loop(Socket).

loop(Socket) ->
    {ok, Chunk} = gen_tcp:recv(Socket, 0),
    io:format("Client received: ~s~n", [Chunk]),
    loop(Socket).

, , -, . TCP ( UDP) , , - . {packet,0}, , recv(Socket, 0) , . , . , , , recv():

get_msg(Socket, Chunks) ->
    Chunk = gen_tcp:recv(Socket, 0),
    get_msg(Socket, [Chunk|Chunks]).

: , , ? {packet,0} Erlang , , ? , recv() ? , - :

get_msg(Socket, Chunks) ->
    case gen_tcp:recv(Socket, 0) of
        {ok, Chunk} ->
            get_msg(Socket, [Chunk|Chunks]);
        {error, closed} ->
            lists:reverse(Chunks);
        {error, Other} ->
            Other
    end.

: chatserver recv(), , , , recv(), , , , recv(), , . , - recv() . () , . , , {packet,0}.

{packet, N} {active, true|false} :


():

send(), * . send() , recv(), .

* " Erlang (2nd)" . 176 , send() send() - , , , , send() , recv() .


:

, , :

Defaults = inet:getopts(Socket, [mode, active, packet]),
io:format("Default options: ~w~n", [Defaults]).

--output:--
Default options: {ok,[{mode,list},{active,true},{packet,0}]}

inet: getopts(), , gen_tcp:accept(Socket) , Socket.


                    {active, true}   {active,false}
                   +--------------+----------------+
{packet, 1|2|4}:   |    receive   |     recv()     |
                   |    no loop   |     no loop    |
                   +--------------+----------------+
{packet, 0|raw}:   |    receive   |     recv()     |
  (equivalent)     |    loop      |     loop       |
                   +--------------+----------------+     

{, }

. . "tcp" - . , , recv().

{, 1 | 2 | 4}:

, , . {packet, 2} , , . , , , . TCP-, , . , , . , , , .

{packet, 2}, , , , 100, , 100 , .

, send(), erlang N , {packet, N}, . , recv() erlang N , {packet, N}, , recv() , recv() .

raw} ():

{packet, 0}, recv() , Length. 0, , recv() , . {packet, 0} recv(Socket, 0) , recv() , , :

get_msg(Socket, Chunks) ->
    case gen_tcp:recv(Socket, 0) of
        {ok, Chunk} -> 
            get_msg(Socket, [Chunk|Chunks]);
        {error, closed} ->
            lists:reverse(Chunks);
        {error, Other} ->
            Other
    end.

, gen_tcp:close(Socket) , (. gen_tcp: close/1 ). , , gen_tcp: shutdown/2.

, chatserver , {packet, 0} package {packet, 0} recv(Socket, 0):

client_handler(Sock, Server) ->
    gen_tcp:send(Sock, "Please respond with a sensible name.\r\n"),
    {ok,N} = gen_tcp:recv(Sock,0), %% <**** HERE ****
    case string:tokens(N,"\r\n") of

recv().


{, }

, TCP ( UDP), . - , accept() , connect(). recv() , receive:

get_msg(Socket)
    receive
        {tcp, Socket, Chunk} ->  %Socket is already bound!
        ...
    end

{, 1 | 2 | 4}:

( ) :

get_msg(Socket) ->
    receive
        {tcp, Socket, CompleteMsg} ->
            CompleteMsg,
        {tcp_closed, Socket} ->
            io:format("Server closed socket.~n")
    end.

raw} ():

, , Erlang , Erlang , . Erlang , , . , , , :

get_msg(ClientSocket, Chunks) ->
    receive
        {tcp, ClientSocket, Chunk} ->
            get_msg(ClientSocket, [Chunk|Chunks]);
        {tcp_closed, ClientSocket} ->
            lists:reverse(Chunks)
    end.


recv() - recv() . , Socket raw, Length. : Erlang gen_tcp: recv (Socket, Length). , - : Erlang Inet:

{packet, PacketType}(TCP/IP sockets)
Defines the type of packets to use for a socket. Possible values:

raw | 0
    No packaging is done.

1 | 2 | 4
    Packets consist of a header specifying the number of bytes in the packet, followed by that  
    number of bytes. The header length can be one, two, or four bytes, and containing an  
    unsigned integer in big-endian byte order. Each send operation generates the header, and the  
    header is stripped off on each receive operation.

    The 4-byte header is limited to 2Gb [message length].

Erlang gen_tcp: recv (Socket, Length), {packet,0}, recv() TCP.

+4

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


All Articles