Difference between revisions of "Sockread"

From Scriptwiki
Jump to: navigation, search
m
m
Line 28: Line 28:
 
  }
 
  }
  
 +
=== See also ===
 +
* [[on sockopen]]
  
 
[[Category:Socket]]
 
[[Category:Socket]]

Revision as of 11:06, 11 September 2006

/sockread [-fn] [numbytes] <%var|&binvar>

The /sockread command reads bytes from the receive buffer into the specified variable.

If you specify a %var variable, a line of text ending with a Carriage Return/LineFeed is read into %var. The $crlf are stripped off (this may result in %var being $null if the line only consisted of $crlf).

If you specify a &binvar then [numbytes] of info is read into the binary variable. If no [numbytes] is specified it defaults to 4096 bytes.

If you specify the -f switch with a %var variable, this forces mIRC to fill the %var variable with whatever text is in the receive buffer, even if it doesn't end in a $crlf.

The -n switch allows you to read a $crlf terminated line into a &binvar. If the incoming line does not contain a $crlf, no bytes will be read into &binvar, unless you specify the -f switch, which forces the read (same as when reading into %vars).

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. If your script doesn't read the whole buffer, the on sockread event is re-triggered if: a) you were reading into a &binvar. b) you were reading into a %var and there is still a $crlf terminated line in the buffer waiting to be read.

Example: This example shows you how you should process a sockread event. The socket has already been opened and has received information, so the sockread event is triggered. The socket name is testing. There is an explanation of each step below the sample script.

on 1:sockread:testing:{
 if ($sockerr > 0) return
 :nextread
 sockread %temp
 if ($sockbr == 0) return
 if (%temp == $null) %temp = -
 echo 4 %temp
 goto nextread
}

See also