how to read from a TCP socket line by line

Luke Gorrie <>
Tue Aug 14 15:33:26 CEST 2001

Daniel Solaz <> writes:

> Hello.
> How does one do when there's the need to read from a TCP socket line by 
> line?  I've been looking through the docs but found nothing.
> Something like this would be nice:
> {ok, Socket} = gen_tcp:connect(...),
> Line = io:get_line(Socket, ...)
> Maybe I should look at how the inets webserver parses HTTP headers 
> (which BTW is exactly what I want to do, except on the client side).

You can use the [{packet, line}] option in gen_tcp:connect/3.

For example:

  1> gen_tcp:connect("mail", 25, [{packet, line}, {active, true}]).
  2> flush().
  Shell got {tcp,#Port<0.8>,
                 "220 ESMTP Sendmail 8.11.3/8.11.3; Tue, 14 Aug 2001 06:30:31 -0700 (PDT)\r\n"}

I think this was added somewhat recently, but it looks like it's there
in R7B.


More information about the erlang-questions mailing list