mIRC Homepage
Posted By: doglem Saving a Picture with Sockets - 18/04/05 10:50 PM
Hidy, I am trying to use sockets to retrieve a picture off of a website. This picture changes on each session. So if I refresh I will get a diff picture each time. So really what im wanting to do is once I read what i get back from the socket save it, then open the pic in a dialog. I thought I could be able to do this by /bwrite 'ing each of the code lines for the picture to a file .jpg or something then opening it. But each time I do it it doesn't load the image :\ so I was just wondering if there was a specific way to get an image and then save it to a file so as I could load it into a dialog. Thanks before hand laugh
Posted By: DaveC Re: Saving a Picture with Sockets - 18/04/05 11:16 PM
Have you gtaken a close look at wheat your saving into the .jpg file, there might be some extra data being sent before the image same way info is sent before a webpage contents, thus making it an invalid jpg file, try a hexeditor on the file, and see whats at the front of it, its liekly text, that you can see and thus isolate before starting the bwrites.
Posted By: FiberOPtics Re: Saving a Picture with Sockets - 18/04/05 11:44 PM
Here's an example:

I've uploaded a picture on the url: http://www.student.kuleuven.ac.be/~m0120111/Dieter/test1.jpg

This is the code to download it.

Code:
alias gpic {
  if $sock(gpic) { echo -ac info * Still downloading, hold on | return }
  write -c pic.jpg
  sockopen gpic student.kuleuven.ac.be 80
}

on *:sockopen:gpic:{
  if $sockerr { echo -ac info * Error opening socket | return }
  var %s = sockwrite -n gpic
  %s GET /~m0120111/Dieter/test1.jpg HTTP/1.0
  %s Host: student.kuleuven.ac.be $str($crlf,2)
}

on *:sockread:gpic:{
  if $sockerr { return }
  if !$sock(gpic).mark {
    var %a
    sockread %a
    if %a == $null { sockmark gpic 1 }
  }
  else {
    sockread &a
    while $sockbr {
      bwrite pic.jpg -1 -1 &a
      sockread &a
    }
  }
}

on *:sockclose:gpic:{
  echo -ac info * Finished downloading pic
  run pic.jpg
}
Posted By: tisyn Re: Saving a Picture with Sockets - 19/04/05 06:33 AM
In your script where you have "sockread %a" then "if %a == $null" you should use sockread -n. I am assuming this blank line and sockmark mark the end of the http headers.

And when you are writing the file in the else {} loop you should use sockread -f
Posted By: DaveC Re: Saving a Picture with Sockets - 19/04/05 07:46 AM
You really should read the help before you write stuff.

From the help file...
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).

So adding a -n to the "sockread %a" is insane since its not a binary var
And adding a -f to either of the "sockread &a" is pointless since it has no -n and shouldnt have one anyway, since the data isnt textual but binary, so you just want to take what there is/what you can take and write it to file.

The code is even written to save processing time by doing repeating sockreads untill the buffer is empty instead of relying on repeated event calls.
Posted By: FiberOPtics Re: Saving a Picture with Sockets - 19/04/05 11:45 AM
I shouldn't do anything.

What you should do is tone it down, and go back to reading the help file.

First of all, you mixed up the flags. -n is to use with binvars, not with regular variables.

Second of all, there's no point in specifying the f switch when reading into a binvar, if it's not accompanied by the n switch. When I'm sockreading in the binvar, I don't want to read just $crlf delimited lines, I want to read anything in the buffer, now at a default of 4096 bytes, so I don't use any flags. If I would use the n switch without the f switch, then lines not delimited by a $crlf would not be read in. There would be data loss for sure.

What you should have done is think before writing.
© mIRC Discussion Forums