speak, it ain’t illegal yet

October 31, 2006

Serializing Erlang Tuples For Network Transmission

Filed under: erlang, network, serializing, ssl — diginux @ 7:44 am

I have been playing around a bit with setting up an SSL client/server connection between nodes using Erlang. As you have guessed, the SSL module in Erlang expects you to send and receive binary data. You may also know, that the typical way of sending data between nodes in Erlang is by using the ‘!’ operator. For example:

Pid ! {data, SomeList}

Now since the SSL uses sockets to send data, we need to convert from the tuple to a binary form. Luckily, Erlang provides a facility to do this!

On the encoding side all you need to do is use the Erlang term_to_binary Built In Function(BIF):

Data = term_to_binary({data, SomeList})

Then we send it away.

ssl:send(CSock, Data)

Now we receive the data:

{ok, DataRecv} = ssl:recv(SSock, 0)

Then we do the decoding. ssl:send automaticlly converts the binary stream to a list when sending, so we need to convert it back, then go from binary_to_term to reverse our initial encoding.

{ok,OrigData}=binary_to_term(list_to_binary(DataList))

That’s all there is to it!

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.