Blockland Forums > Modification Help
TCPObject GET requests
<< < (9/10) > >>
Nexus:

--- Quote from: Greek2me on March 16, 2012, 11:58:57 PM ---Post your code.

--- End quote ---


--- Code: ---function Downloader::onDNSResolved(%this)
{
      echo("DNS Resolved");
}

function Downloader::onDNSFailed(%this)
{
      echo("DNS Failed");
}

function Downloader::onConnected(%this)
{
      echo("Connected");
      %req = "GET "@$file@" HTTP/1.0\r\n";
      echo(%req);
      %this.send(%req);
}

function Downloader::onConnectFailed(%this)
{
      echo("Connection Failed");
}

function Downloader::onDisconnect(%this)
{
      echo("Disconnected");
}

function Downloader::onLine(%this, %line)
{
      echo("TCP: " @ %line);

if(strPos(%line,"Content-Length:") >= 0)
%this.length = getWord(%line,1);

//ONLY NEEDED IF SAVING THE FILE
if(%line $= "")
%this.setBinarySize(%this.length);
}

//ONLY NEEDED IF SAVING THE FILE
function downloader::onBinChunk(%this,%chunk)
{
echo("chunk'd");
if(%chunk < %this.length)
return;

%path = "config/server/TCP example.txt";

%this.saveBufferToFile(%path);
%this.disconnect();
}

function defaultconnect()
{
   if(!isobject(Downloader))
      return;

   $ip = "98.227.200.17";
   $file = "/shares/USB_Storage/testdoc.txt";
   echo("Sending connect request to "@$ip@" for "@$file);

   Downloader.connect($ip@":80");
}


if(isobject(Downloader))
      Downloader.delete();
new tcpobject(Downloader);


--- End code ---


--- Code: ---==>defaultconnect();
Sending connect request to 98.227.200.17 for /shares/USB_Storage/testdoc.txt
Connected
GET /shares/USB_Storage/testdoc.txt HTTP/1.0


Disconnected
--- End code ---
I just copied and pasted the onbinchuck function out of some other coding help topic somewhere, I'm thinking it was the one ipq linked.
I also verified to make sure it was working by sending a request to forum.blockland.us and it downloaded some broken html something that ends like halfway through a line of code, but it still was called.  I'll attach it in case someone cares.

There is about a five second delay between when the GET request is sent and Disconnected appears

Also I have tried like a million variations on the GET request, but this is the one that I am pretty sure Ephialtes indicates is best.
Port:
Why are you using binary content retrieval for a plain text file?
Nexus:

--- Quote from: Port on March 18, 2012, 04:02:09 AM ---Why are you using binary content retrieval for a plain text file?

--- End quote ---

No idea, what would you suggest?
Port:

--- Quote from: Nexus on March 18, 2012, 04:03:18 AM ---No idea, what would you suggest?

--- End quote ---

If you need to save the file, create a file object and open a file for writing upon connecting, write lines when recieved (make sure you don't write the header to the file) and close/delete the file object when disconnected or done receiving data. You may want to add a line "timeout" to automatically close/delete the file object in case the web server doesn't send Content-Length and doesn't disconnect you either (most web servers respect the Connection: close header though).
Nexus:

--- Quote from: Port on March 18, 2012, 04:05:51 AM ---If you need to save the file, create a file object and open a file for writing upon connecting, write lines when recieved (make sure you don't write the header to the file) and close/delete the file object when disconnected or done receiving data. You may want to add a line "timeout" to automatically close/delete the file object in case the web server doesn't send Content-Length and doesn't disconnect you either (most web servers respect the Connection: close header though).

--- End quote ---

Sounds good, but how do I detect when lines are recieved?
Navigation
Message Index
Next page
Previous page

Go to full version