Erlang: gen_tcp: recv () does not receive a packet sent from the client?

I changed this server to use gen_tcp:recv inorder to limit the number of bytes for a packet to 50. I commented out the line inet:setopts(Socket, [{active, once}]), , because gen_tcp:recv must be {active,false} . This is client side erl shell

 2> cp3:client(). exit 3> 

and this is erl server shell

 4> cp3:server(). Started Server: <0.46.0> Accept Server: Pid <0.48.0> Connection accepted Accept Server: Loop Server: 5> 

I also wondered how to find out if a socket with return value {tcp_closed, Socket} is gen_tcp:recv if gen_tcp:recv does not create one?

 -module(cp3). -export([client/0, server/0,start/0,accept/1,enter_loop/1,loop/1]). client() -> {ok, Socket} = gen_tcp:connect("localhost", 4001,[list, {packet, 0}]), ok = gen_tcp:send(Socket, "packet"), receive {tcp,Socket,String} -> io:format("Client received = ~p~n",[String]), io:format("Client result = ~p~n",[String]), gen_tcp:close(Socket) after 1000 -> exit end. server() -> Pid = spawn(fun()-> start() end), Pid. start() -> io:format("Started Server:~n"), {ok, Socket} = gen_tcp:listen(4001, [binary, {packet, 0},{reuseaddr, true},{active, false}]), accept(Socket). accept(ListenSocket) -> io:format("Accept Server:~n"), case gen_tcp:accept(ListenSocket) of {ok, Socket} -> Pid = spawn(fun() -> io:format("Connection accepted ~n", []), enter_loop(Socket) end), io:format("Pid ~p~n",[Pid]), gen_tcp:controlling_process(Socket, Pid), Pid ! ack, accept(ListenSocket); Error -> exit(Error) end. enter_loop(Socket) -> %% make sure to acknowledge owner rights transmission finished receive ack -> ok end, loop(Socket). loop(Socket) -> %% set socket options to receive messages directly into itself %%inet:setopts(Socket, [{active, once}]), io:format("Loop Server:~n"), case gen_tcp:recv(Socket, 50) of {ok, Data} -> case Data of <<"packet">> -> io:format("Server replying = ~p~n",[Data]), gen_tcp:send(Socket, Data), loop(Socket) end; {error, Reason} -> io:format("Error on socket ~p reason: ~p~n", [Socket, Reason]) end. 
+4
source share
1 answer

I do not really understand your question, but the code above does not work. I hope the following answers to your problem. Your tcp receive case gen_tcp:recv(Socket, 50) of has one error. It is waiting for 50 bytes to read. Check the gen_tcp: recv / 2 documentation. Change the length (packer length 6, but preferably to) 0 to get all bytes.

The value does not limit the size of the data, but it will not send data until it receives data of length 50. Instead, you may need to accept and then check.

+4
source

All Articles