From WikiChip
mirc/commands/sockread
< mirc‎ | commands
Revision as of 21:34, 24 July 2014 by Ouims (talk | contribs) (Reading)

The /sockread command reads bytes from the receive buffer of a socket connection into a specified variable. This command must be and is to be used inside socket events.

mIRC in general only understand $crlf terminated line. With socket, mIRC only understands $lf terminated line (any $cr before a $lf is removed).

By default, /sockread %var reads a terminated line into the variable, /sockread &var reads 4096 into the variable.

A single /sockread may not be enough to read the entire buffer (if you are reading line by line for example). You might want to keep reading until $sockbr (the number of bytes read) is set to zero. This is far faster than letting mIRC re-trigger the event. If your script does not 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 terminated line in the buffer waiting to be read. 

This means you will miss the last line if it does not end with a terminated line and you are reading line by line only.

Note: It is possible to read such a last line inside the on sockclose event.

Synopsis

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

Switches

Switch Description
f Forces mIRC to fill the variable with whatever text is in the receive buffer, even if it does not end in a terminated line.
n Reads a terminated line into a &binvar. If the incoming line does not contain a terminated line, no bytes will be read into the &binvar, unless you specify the -f switch.

Parameters

Parameter Description
[numbytes] The number of bytes to read from the receive buffer.
[%var/&binvar] The name of the variable to read the socket buffer into.

Reading

It is important to note that the packets size will vary, for example, it would theorically be possible to get the event to trigger, to try to read a line with /sockread %a, and to get an empty value because mIRC received data but not enough to get the first terminated line, or if the only data sent is a non terminated line. Practically speaking, it is unlikely to happen because you receive several KB at a second, so reading line by line will be slower than having the received buffer filled.

A common situation when dealing with HTML is to find a specific line and then to read the next line in the received buffer, avoiding setting a variable and letting mIRC retriggers the event, illustrated below:

ON *:SOCKREAD:mySocket:{
  var %a
  sockread -f %a
  if (<div id="myid" isin %a) {
   sockread -f %a
   echo -a my data!!! : %a
 }
}

In this case, there is actually no way to tell if mIRC has a terminated line in his buffer before doing the second /sockread, so there is no way to make sure this will work For the same reasons as above, this will likely work, but could theorically fail, so keep that in mind.

Example

Here is an example which will read and echo to the status window the whole source of a page line by line.

Note: leading/consecutives and trailing spaces won't be shown correctly due to mIRC tokenizing parameters on spaces for command.

Note2: too long line will produce an error.

ON *:SOCKREAD:mySocket:{
  var %a
  if ($sockerr > 0) { return }
  sockread -f %a
  echo -s > %a
  while ($sockbr) {
    sockread -f %a
    echo -s > %a
  }
}

Compatibility

Added: mIRC v5.3
Added on: 13 Dec 1997
Note: Unless otherwise stated, this was the date of original functionality.
Further enhancements may have been made in later versions.


See Also

v · d · e mIRC commands list

A /abook, /action, /add, /ajinvite, /alias, /aline, /ame, /amsg, /anick, /aop, /auser, /auto, /autojoin, /avoice, /away

B /background, /ban, /bcopy, /beep, /bindip, /bread, /break, /breplace, /bset, /btrunc, /bunset, /bwrite

C /channel, /clear, /clearall, /clearial, /cline, /clipboard, /close, /closechats, /closedccs, /closefserves, /closemsg, /cnick, /color, /colour, /comclose, /comlist, /commands, /comopen, /comreg, /continue, /copy, /creq, /ctcp, /ctcpreply, /ctcps

D /dcc, /dccserver, /dde, /ddeserver, /debug, /dec, /describe, /dialog, /did, /didtok, /disable, /disconnect, /dlevel, /dline, /dll, Template:mIRC/donotdisturb, /dns, /dqwindow, /drawcopy, /drawdot, /drawfill, /drawline, /drawpic, /drawrect, /drawreplace, /drawrot, /drawsave, /drawscroll, /drawsize /drawtext

E /ebeeps, /echo, /editbox, /else, /elseif, /emailaddr, /enable, /events, /exit

F /fclose, /filter, /findtext, /finger, /firewall, /flash, /flist, /flood, /flush, /flushini, /fnord, /font, /fopen, /fseek, /fsend, /fserve, /fullname, /fupdate, /fwrite

G /ghide, /gload, /gmove, /gopts, /goto, /gplay, /gpoint, /gqreq, /groups, /gshow, /gsize, /gstop, /gtalk, /gunload, /guser

H /hadd, /halt, /haltdef, /hdec, /hdel, /help, /hfree, /hinc, /hload, /hmake, /hotlink, /hop, /hsave

I /ial, /ialclear, /ialmark, /identd, /if, /ignore, /iline, /inc, /iuser

J /join

L /leave, /linesep, /links, /list, /load, /loadbuf, /localinfo, /log, /logview

M /maxdepth, /mdi, /me, /menubar, /mkdir, /mnick, /mode, /msg

N /noop, /notice, /notify

O /onotice, /omsg

P /pareline, /part, /partall, /pdcc, /perform, /play, /playctrl, /pop, /protect, /proxy, /pvoice

Q /qme, /qmsg, /query, /queryrn, /quit, /quote

R /raw, /registration, /reload, /remini, /remote, /remove, /rename, /renwin, /reseterror, /resetidle, /return, /returnex, /rlevel, /rline, /rmdir, /run, /ruser

S /save, /savebuf, /saveini, /say, /scid, /scon, /server, /set, /setlayer, /showmirc, /signal, /sline, /sockaccept, /sockclose, /socklist, /socklisten, /sockmark, /sockopen, /sockpause, /sockread, /sockrename, /sockudp, /sockwrite, /sound, /speak, /splay, /sreq, /strip, /switchbar

T /timer, /timestamp, /tip, /tips, /titlebar, /tnick, /tokenize, /toolbar, /topic /tray, /treebar

U /ulist, /unload, /unset, /unsetall, /updatenl, /url, /username, /uwho

V /var, /vcadd, /vcmd, /vcrem, /vol

W

X /xyzzy