[erlang-questions] reading zero-terminated strings from a socket

15 views
Skip to first unread message

Joel Reymont

unread,
Sep 15, 2012, 3:56:06 PM9/15/12
to erlang-q...@erlang.org
I need to read zero-terminated strings from a socket.

What is the most efficient way of doing so?

I wish there was a {packet, ...} option for this but, alas, {packet,
line} only looks for newlines.

Thanks, Joel

--------------------------------------------------------------------------
- for hire: mac osx device driver ninja, kernel extensions and usb drivers
---------------------+------------+---------------------------------------
http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
---------------------+------------+---------------------------------------
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Loïc Hoguin

unread,
Sep 15, 2012, 4:01:04 PM9/15/12
to erlang-q...@erlang.org
* recv(Socket, _, Timeout)
* binary:split(Bin, << 0 >>)
* bufferize and try next: << Acc/binary, Bin/binary >>

You'll most likely want to check your buffer before receiving again in
case you have receiving many strings at once.
--
Loïc Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu
Reply all
Reply to author
Forward
0 new messages