gen_tcp:recv - last segement when closed port

jmT2 johan@REDACTED
Sat Mar 11 12:18:32 CET 2006

This is how I open the socket:

     gen_tcp:connect(IP, Port, [list, {packet,0}, {active, false}]).

And this is how I access the socket:

     gen_tcp:recv(Socket, 0, 20000)  %(high timeout since I'm running over  
mobile links)

I'm reading from a web server that replies with HTTP/1.0 and thus closes  
the TCP connection once the page is delivered. The page is delivered in  
five TCP packets:

638 bytes  header info
1400 bytes body
1400 bytes body
1270 bytes body
1199 bytes body

My read-parse loop reads the stream by calling gen_tcp:recv/3 and gets the  

head--- 638

body--- 1024
body--- 376   (aah the first 1400)

body--- 1024
body--- 376  (aah the second 1400)

body--- 1024
body--- 246 (aah the third 1270)

body--- 1024  - ok this is why I say the usual 1024
body closed  - ops where did the remaining 175 bytes go?

My guess is that the port is closed while I'm parsing the last 1024 bytes  
and that the 175 bytes are left in buffer. Is there any way to retreive  

I Klackes example he requested a specific length but I'm only asking for  
any length.


Den 2006-03-11 11:47:57 skrev Per Hedeland <hedeland@REDACTED>:

> jmT2 <johan@REDACTED> wrote:
>> I have a problem using gen_tcp:recv(Socket, 0). If (when as it turns out
>> in my application) the server port closes  the last segment is never
>> returned. Using Ethereal I can see the last TCP packet containing 1199
>> byts, but the last segement returned by recv/2 is as usual 1024 bytes.  
>> The
>> remaining 175 bytes is never found. The next call to recv/2 will only
>> return {error, closed} :-(
> Hm, what's "usual" about 1024 bytes? Are you sure you aren't actually
> doing gen_tcp:recv(Socket, 1024)? In that case what you see is the
> documented behaviour. See also the thread starting at:
> --Per Hedeland

Johan Montelius jmT2

More information about the erlang-questions mailing list