The reason is much simpler than that. The last line of the page is not terminated by a newline, and /sockread without -f only reads newline-terminated lines. Using /sockread -f you can read data that isn't terminated by a newline, but "on sockread" only triggers when there's a newline-terminated line available [1] which is why just adding -f to your original code doesn't work, and why adding a loop does work (as it forces mIRC to read out the rest, newline or not).

However, it's dangerous to use /sockread -f like that, because it could happen that for example one-and-a-half line arrives first, and the second half of the second line later (on the TCP level!) in which case you will read the whole first line, then the first half of the second line, and later the second half of the second line, and see them as three lines instead of two! In this case it's not likely to happen, but better practice is to instead use a trick: only use /sockread -f to read the last line from the on sockclose event. This way you will only read half a line if there really is half a line and nothing more, because on sockclose obviously only triggers when the connection is closed.

A slight complication in this case is that your use of HTTP/1.1 will automatically keep the connection open, so "on sockclose" will not trigger anytime soon. Changing the request to use "HTTP/1.0" is a simple fix here, although you could also add the "Connection: close" header to the HTTP/1.1 request. All in all this gives the following code (note you had the socket name wrong in the "on sockclose" event originally):

Code:
alias getstats set %getstats $1 | sockclose stats1 | sockopen stats1 xwis.net 80
on *:sockopen:stats1:sockwrite stats1 $+(GET /xcl/?pname=,%getstats HTTP/1.0,$crlf,Host: xwis.net,$crlf,$crlf)
on *:sockread:stats1:{
  var %var 
  sockread %var
  echo -a $sockname >> %var
}
on *:sockclose:stats1:{
  var %var 
  sockread -f %var
  echo -a $sockname >> %var
  echo -a CLOSED!
}

Note that you should also add normal error checking on $sockerr and $sockbr etc. But this should work already.

However, if you have any control over the webpage you're pulling the data from, it's easier to just add a newline after the last line...

[1] a somewhat simplified version of the story here


Saturn, QuakeNet staff