1

I am working in project with websockets and MQTT. Websocket server receives 4 messages from mqtt and I made it loop to receive any other messages. However, if there are no messages, Encoded_fixed_header = gen_tcp:recv(Socket, 0) gives an error because there are no messages to receive. I did some research on gen_tcp:recv and it says that it waits infinitely for a message, but it seems not and it closes the socket.

recieve(Socket,WsPID) ->
    Encoded_fixed_header = gen_tcp:recv(Socket, 0),
    Length = gen_tcp:recv(Socket, 0)
    Variable_Header = gen_tcp:recv(Socket, 0),
    Playload=gen_tcp:recv(Socket, 0),
    recieve(Socket,WsPID).

What could be the problem?

Rad1
  • 185
  • 1
  • 4
  • 17

1 Answers1

0

check the value of the active parameter either in the gen-tcp:listen/2, when you start the server, or in the inet_default_connect_options, if you use the default value.

Pascal
  • 13,977
  • 2
  • 24
  • 32
  • I am using False as a value for active : `{ok, Socket} = gen_tcp:connect("localhost", 1885,[binary, {packet, 0},{active,false},{exit_on_close, false}])` I think that is the value I should set . – Rad1 Jul 03 '14 at 01:30
  • As your code is explicitly calling `gen_tcp/recv/2` to get new messages, you should set the active option to false. Active true is used to receive directly the incoming message in a receive bloc of the process which called `gen_tcp:connect/2`.Then beware that your code will produce nothing as you drop the returned values at each recall of the receive function. – Pascal Jul 03 '14 at 04:19
  • You can find a good tutorial there [http://learnyousomeerlang.com/buckets-of-sockets](LYSE) – Pascal Jul 03 '14 at 06:32