Search code examples
socketswebsocketerlangmqttgen-tcp

Erlang gen_tcp:recv socket closing


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?


Solution

  • 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.