mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jan 2015
Posts: 12
T
Pikka bird
OP Offline
Pikka bird
T
Joined: Jan 2015
Posts: 12
Hi guys, I'm trying to get some information from my server with sockets. However, I only receive the header of the page and not the content.

Page I want to retrive informations from : http://gotme.site-meute.com/get-location-10

Socket code :
Code:
alias locationcheck {
  sockclose location
  sockopen location gotme.site-meute.com 80
}

; When the socket is open, make the request to get the location
on *:sockopen:location: {
  sockwrite -n $sockname GET /get-location-10 HTTP/1.1
  sockwrite -n $sockname Host: gotme.site-meute.com
  sockwrite -n $sockname $crlf
}

; When the socket responds, read it and check for new location
on *:sockread:location: {
  if ($sockerr) { sockclose $sockname | halt }

  var %data
  sockread %data
  tokenize 32 %data
    
  ; I use 123 so it display empty lines
  echo -a 123 %data
    
}


Result:
123 HTTP/1.1 200 OK
123 Date: Mon, 16 Feb 2015 00:10:11 GMT
123 Server: LiteSpeed
123 Connection: close
123 X-Powered-By: PHP/5.4.37
123 Content-Type: text/html
123 Content-Length: 143
123
123

Thanks for your help! smile

Joined: Sep 2014
Posts: 259
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Sep 2014
Posts: 259
404? smile

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
Quote:
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 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 does not end in a $crlf.

Joined: Jan 2015
Posts: 12
T
Pikka bird
OP Offline
Pikka bird
T
Joined: Jan 2015
Posts: 12
I'm pretty sure it's not a 404 since the data on the link I provided is working

Joined: Jul 2006
Posts: 4,149
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,149
To expand on Loki's quote, sockread %var won't read a line if it does not end with a terminated line (a $crlf OR $lf alone!!)


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jan 2015
Posts: 12
T
Pikka bird
OP Offline
Pikka bird
T
Joined: Jan 2015
Posts: 12
From what I understand, I need to add the -f parameter to the sockread so it'll become "sockread -f %data" ? I tried and still got the same result

EDIT: nvm, Thanks guys, I'm idiot! :p

Last edited by Trahanqc; 16/02/15 07:30 PM.
Joined: Jul 2006
Posts: 4,149
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,149
There are various way to go about it but using /sockread -f isn't 'proper'.
-f will force the current content to be read even if it does not end with a terminated line, which is not what you want.
Why? Because if you get a slow connection and you only get half the expected content, -f will read that and read the rest after, where you would typically assume you have read two different lines.
You would then need to make sure you get the correct content yourself, which is a bit more complex..
If you don't use http 1.1 or if the socket is getting closed at the end, triggering on sockclose, a proper and easy way is to keep reading line by line with "sockread %data" (don't forget to check for $sockbr and to stop processing if you didn't read anything), and to read that last line which doesn't end with $crlf/$lf inside the on sockclose event.
Otherwise, on sockclose won't get triggered and there you need to make sure, as I said, that you read all the content correctly before processing it, for that you can read everything with sockread -f but you add what you read to a buffer, and you process that buffer once you have recieved all the data (use the Content-length value to know when you have read everything).


#mircscripting @ irc.swiftirc.net == the best mIRC help channel

Link Copied to Clipboard