mircscripting.info Forum Index mircscripting.info
#mIRCscripting Forum
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Socket Download tutorial

Post new topic   Reply to topic    mircscripting.info Forum Index -> Tutorials
View previous topic :: View next topic  
Author Message
Quite Active

Joined: 24 Jan 2008
Posts: 30
Location: Neverlands

PostPosted: Sat May 23, 2009 8:34 pm    Post subject: Socket Download tutorial Reply with quote

This is a short and simple "download-via-mIRC-sockets" tutorial, and it downloads the mIRC gif logo on mIRC's homepage.

This is NOT a socket tutorial, the tutorial assumes you already know how to use sockets, and it just shows you how to download using them. If you need to learn more about mIRC sockets, take a peek at any of these links:
- http://mirc.kicks-ass.org/tutorials/contents.php?file=sockets.txt
- http://mircscripting.info/forum/viewtopic.php?t=83&sid=8bca8f0c879dd35d0eb3bda8adbc3f00
- http://www.mircscripts.org/comments.php?cid=2076
- http://www.mircscripts.org/showdoc.php?type=tutorial&id=1217
- http://www.mircscripts.org/showdoc.php?type=tutorial&id=37
- http://www.mircscripts.org/showdoc.php?type=tutorial&id=1128
( I would suggest you take a look at all of them actually, the more the better Smile )

The main alias is called d.image and will open a socket connection called image to www.mirc.com
Full path to the image is: http://www.mirc.com/images/indexlogo.gif

Step 1: Creating a connection to the host.
alias d.image { sockopen image www.mirc.com 80 }

Step 2: Requesting the file.
on 1:sockopen:image:{
  sockwrite -nt $sockname GET images/indexlogo.gif HTTP/1.1
  sockwrite -nt $sockname Host: www.mirc.com
  sockwrite -nt $sockname $str($crlf,2)

Step 3 (a): Knowing what exactly is the host replying with in the sockread (it is not the download part yet, just an event that will echo anything the socket brings you)
on 1:sockread:image:{ var %read | sockread %read | echo -a * [Sockread] %read }

In this example, the previous sockread event should echo this:
* [Sockread] HTTP/1.1 200 OK
* [Sockread] Date: Sun, 24 May 2009 00:12:24 GMT
* [Sockread] Server: Apache
* [Sockread] Last-Modified: Fri, 03 Apr 2009 10:29:21 GMT
* [Sockread] ETag: "3349df-24dd-49d5e501"
* [Sockread] Accept-Ranges: bytes
* [Sockread] Content-Length: 9437
* [Sockread] Content-Type: image/gif

The above is called The Header, the end of the header the host sends you, and the start of the actual file's contents are separated with a blank line, so basically you're gonna see loads of crap coming through the sockread right after the "Content-Type" line, it's a bit too long to paste in here, however, it will look something like this:


* [Sockread] GIF89a
* [Sockread] <d˗XB&M8gdoySqC!F Ĉ6dl%4l``A@M[h]ƴͶpݾ)wЁy_I<E.~
* [Sockread] ,H5Ln7 d)/Ip.@xڲ

* [Sockread] =Clvs
* [Sockread] vMҷeؠyx{iq^eK0݀8` L0xgS<V]z1PB<y|
GeA 0ן
* [Sockread] `H6 ȡx%ƒA N- \rM_v)&CJ Lv5B 0 p)&-X6(f\ So% r5eVR1(#2Ђzj.%f + fRp#{n 0e^rC][h蕌WuM(mò0g6lc[&
dz\ۮMPYe2dݺڵk ,@p,Iin0x0bjcoۭڝV) (K玐.jFKVqiJqRAr ױK6ߌ)Ν&-=0 |hPWaS1T1[?o1i\K8a,L`O~rI-\`I6|۽f4S#xm=NB
* [Sockread] \@ԗ#k>"k7bw66f; 8suwqz<'ylH\cB.`VTN. ֏??'l_l0ЖSNꫯ

PS.: The "* [Sockread] Content-Length: 9437" line might be useful to know if the socket downloaded the full file contents, or screwed up at some point, so let's keep in mind the file's contents = 9437
another PS.: Downloading the file is "writing it's binary value", if you don't know about /bwrite or /bread or &bvars you might wanna check the help file.

Ok, now we got the header and the content length, and we know the actual file content's are preceeded by a blank line (separating it from the header), so basically what you need to tell mIRC to do is:
1- /sockread the socket.
2- once you hit a blank line, download whatever comes next.


Step 3 (b): Downloading!
replace the previous sockread event with this one
on 1:sockread:image:{
  ;- if the socket isn't marked, procedd with the sockread normally
  if (!$sock($sockname).mark) {
    var %read | sockread %read

    ;- If sockread is blank (null), mark the socket so that it starts downloading what follows (on the next read)
    if (%read == $null) { echo -a *** Downloading... | sockmark $sockname 1 }

  ;- Socket is marked, we just passed the blank line, start downloading from here
  ;- Save the file in $mircdir with the same name it had on the website
  else { sockread &image | bwrite $qt($mircdirindexlogo.gif) -1 -1 &image }

Now the download is initiated, once it's done the socket will be closed by itself. Remember the content length? No? Ok, it was 9437
We'll use that in a sockclose event, so that once the socket closes (download is complete) we would be able to know if the file is fully downloaded, using $file( ).size identifier.

Step 4: Verifying downloaded file.
And it's really self explanatory
on 1:sockclose:image:{
  echo -a *** Download complete.
  echo -a *** File Size: $file($mircdirindexlogo.gif).size

Now this will echo:
*** Download complete.
*** File Size: 9437

YAY! we've just downloaded an image via mIRC Cool--~
boo.. and stuff.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    mircscripting.info Forum Index -> Tutorials All times are GMT - 5 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group