how to read from a TCP socket line by line

Luke Gorrie luke@REDACTED
Tue Aug 14 15:33:26 CEST 2001


Daniel Solaz <dsolaz@REDACTED> 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}]).
  {ok,#Port<0.8>}
  2> flush().
  Shell got {tcp,#Port<0.8>,
                 "220 sharks.alteon.com ESMTP Sendmail 8.11.3/8.11.3; Tue, 14 Aug 2001 06:30:31 -0700 (PDT)\r\n"}
  ok

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

Cheers,
Luke




More information about the erlang-questions mailing list