[erlang-questions] how to use sockets in erlang

Ivan Uemlianin ivan@REDACTED
Fri Jun 17 13:10:12 CEST 2011


Dear All

I have a Python application I'm porting to Erlang (I'm expecting the 
Erlang version to be a bit faster, but much more robust and easier to 
scale up).  The central function is to jsonify some data, send it over a 
socket to a remote (third party) server, and receive a reply.

Thanks to mochijson2, the jsonifying is sorted, but something in the 
socketing is not going well.

In the excerpts below I abstract the socket connect/send/receive code. 
The python works as expected, the erlang gen_tcp:recv returns {error, 
closed}.  I suspect I am missing something basic about sockets in 
Erlang.  Please can someone point out what it is?

n.b. jsondata (python) and JsonData (erlang) are identical --- I have 
even copied the list of integers in JsonData and pasted them into the 
python and sent them.

Python version --- works:

     jsondata = json.dumps(data) + "\n"
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.connect((appliance_host, appliance_port))
     s.send(jsondata)
     returndata = s.recv(4096)

Erlang version --- doesn't work:

     JsonData = list_to_binary(mochijson2:encode(Data) ++ <<10>>),
     {ok, Socket} = gen_tcp:connect(?APPLIANCE_HOST, ?APPLIANCE_PORT,
				  [inet,
				   binary,
				   {active, false},
				   {packet, 0}]),
     ok = gen_tcp:send(Socket, JsonData),
     Result = gen_tcp:recv(Socket, 4096, 5000),
     % Result = {error, closed}


With thanks and best wishes

Ivan


-- 
============================================================
Ivan A. Uemlianin
Speech Technology Research and Development

                     ivan@REDACTED
                      www.llaisdy.com
                          llaisdy.wordpress.com
                      www.linkedin.com/in/ivanuemlianin

     "Froh, froh! Wie seine Sonnen, seine Sonnen fliegen"
                      (Schiller, Beethoven)
============================================================



More information about the erlang-questions mailing list