mIRC Homepage
Posted By: Cyrex on SOCKREAD - Proper Usage - 24/11/04 01:22 AM
When using the on SOCKREAD event, is it necessary to use a loop to keep checking the receive buffer? I notice that many socket scripts don't bother even using a loop. I don't notice any difference using a loop or not. What can happen if I don't use a loop?

Take these two on SOCKREAD codes for example:

Example 1

Code:
 
on *:sockread:test:{
  if ($sockerr > 0) { return }
  sockread %temp
  if ($sockbr == 0) { return }
  echo -a %temp
}
 

Example 2

Code:
 
on *:sockread:test:{
  if ($sockerr > 0) { return }
  :nextread
  sockread %temp
  if ($sockbr == 0) { return }
  echo -a %temp
  goto nextread
}
 


Both do the same thing. Why use a loop?
Posted By: Coolkill Re: on SOCKREAD - Proper Usage - 24/11/04 01:46 AM
I would imagine its just to ensure the socket keeps reading until it closes, and not just reads once.

Though, mIRC nowadays seems to do that automatically..

Eamonn.
Posted By: Danthemandoo Re: on SOCKREAD - Proper Usage - 24/11/04 02:08 AM
i never use any loops in my sockread events and i've never had any problems.
Posted By: feud Re: on SOCKREAD - Proper Usage - 24/11/04 04:30 PM
that's probably because you aren't trying to read more text than /sockread can on one pass (4096 bytes i believe). if there is more data to be read, your single pass /sockread will just read what it can and then end.
Posted By: tontito Re: on SOCKREAD - Proper Usage - 25/11/04 08:38 PM
I only wait for the event to trigger, saves lots of cpu and you don't lose a single byte.

And since the binary vars can handle "almost unlimited" amounts off data i force it to read always 8k since the socket can only buffer max 16k.

From tests i have made, when the event is triggered it is very unusual to have more than 8kb waiting... unless your machine is heavily loaded by another reason...

In conclusion, reading 8k seems a very good compromise to have good performance.
Posted By: Online Re: on SOCKREAD - Proper Usage - 28/11/04 02:36 AM
Quote:
When using the on SOCKREAD event, is it necessary to use a loop to keep checking the receive buffer?


Not necessary but advisable.


Quote:
I don't notice any difference using a loop or not.


There is performance difference which I can notice here with my good old 300MHz processor. Let me quote the help file on this:

Note: A single /sockread may not be enough to read the entire buffer. You should keep reading until $sockbr (bytes read) is set to zero. This is far faster than letting mIRC re-trigger the event.

Now as far as I'm aware, a loop would speed up things only in either of these cases:
  1. You're reading into a %variable.
  2. You're using the -n switch to read one line at a time into a &variable.


Here's how I make my loops:

Code:
On *:sockread:sockname:{
  var %var
  sockread %var
  while $sockbr {
    ; process %var
    ...
    sockread %var
  }
}
© mIRC Discussion Forums