how to read from a TCP socket line by line

Daniel Solaz <>
Tue Aug 14 23:55:54 CEST 2001


On Tuesday 14 August 2001 15:33 Luke Gorrie wrote:
> You can use the [{packet, line}] option in gen_tcp:connect/3.
> ...
> I think this was added somewhat recently, but it looks like it's
> there in R7B.

Ah yes, now I see it in the docs, but they say:
"line mode, a packet is a line terminated with newline, lines longer 
than the receive buffer are truncated"

What does the last part mean?  If "truncated" means what I think it 
means, how do I ensure my buffer is large enough when reading binary 
data?

Note that after reading the HTTP response header line by line I should 
switch to buffer mode (using gen_tcp:recv/3), since the response body 
may be binary data.

In Modula-3 I've solved this using "read one line" and "read one 
buffer" methods on the same reader.  But in Erlang it seems I either 
open the socket in active mode and get lines as messages, or open the 
socket in passive mode and get buffers.

-- 
d s o l a z @ L E P I D O P T E R O . C O M




More information about the erlang-questions mailing list