mIRC Homepage
Posted By: Shindogo Trying to learn sockets - 18/11/07 11:37 PM
I'm trying to learn how to script sockets, and it isnt going well. I dont know how close I am, but heres my code:

Code:
on *:TEXT:!bash:#:{
  if ($me != Prometheus) { halt }
  else { echo triggered | sockopen bsocket bash.org 80 | set %nick $nick }
}

on *:SOCKOPEN:BSOCKET:{
  sockwrite -nt $sockname GET http://bash.org/?random
  sockwrite -nt $sockname Host: bash.org
  sockwrite -nt $sockname $crlf
  echo opened
}

on *:SOCKREAD:BSOCKET:{
  echo reading
  var %bash
  sockread %bash
  notice %nick %bash
  sockclose $sockname
  unset %nick
  echo closed
}


It returns a line of HTML: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

I dont know what that means. Can anyone help?
Posted By: Loki12583 Re: Trying to learn sockets - 19/11/07 02:45 AM
Use a while loop to read the entire document.

Code:
sockread %bash
while ($sockbr) {
sockread %bash
}


Fool around with this.
Posted By: Shindogo Re: Trying to learn sockets - 19/11/07 03:27 AM
I still can't make it work. Example?
Posted By: Bekar Re: Trying to learn sockets - 19/11/07 04:02 AM
If you read the help file a bit more thoroughly, you'll read two things:

1) ON SOCKREAD will trigger every time something new comes into that socket, or something remains in the socket's queue, and will re-trigger if there is still things to read.

2) A /sockread command will read into a non-binary variable up until the next carriage-return/line-feed/crlf. You can also see if there's more to read by checking the $sockbr identifier's state.

Take these two together, and you can make some assumptions:

- Let ON SOCKREAD trigger many times, reading from the socket one line at a time.

- Set up a loop of some kind (while/goto) and read until $sockbr is no longer 0.

Now, the code as it stands reads one line, then closes the socket, forcing mIRC to discard the remaining data in the socket connection.

Don't close the socket in your ON SOCKREAD, leave it until an ON SOCKCLOSE event is triggered, or set up a timer and make your own connection timeout.

What does this all translate to?

Code:
ON *:SOCKREAD:bsocket: {
  var %bash
  while ($sockbr != 0) {
    sockread $sockname %bash
    <manipulate the data here>
  }
}
ON *:SOCKCLOSE:bsocket: {
  sockclose $sockname
}


.. Roughly ..
Posted By: Bekar Re: Trying to learn sockets - 19/11/07 04:14 AM
In mind of making it a bit more resilient to multiple users, you may want to look at the /sockmark command, and it's associated identifers.

Example code to look at can be found here.
Posted By: jaytea Re: Trying to learn sockets - 19/11/07 10:55 AM
in the sockread event, $sockbr is equal to 0 initially (since /sockread hasn't been used yet) so that while loop is never entered :P that's why Loki included the extra /sockread above the loop

an alternative to that would've been:

Code:
while ($sock($sockname).rq) {
  sockread -f %var
  ; commands with %var
}


i think you made a typo! /sockread $sockname %bash? :S
Posted By: Bekar Re: Trying to learn sockets - 19/11/07 11:01 AM
Yeah.. was at work, no help file to refer to smile
© mIRC Discussion Forums