It depends what you're doing. If you're downloading files then it's necessary to use TCP, if you're using a custom protocol more than likely you'll need to use TCP, but if you're just connecting to a website then your best choice is an HTTP object. Granted, HTTP objects can only send GET requests, but most likely if you'll be sending a POST request you'll have to deal with a bunch of complicated things like cookies and PHP sessions that would go way outside the range of any tutorial for TorqueScript.
To connect to a website with HTTP it's pretty easy.
new httpObject(HTTPTest);
function HTTPTest::onLine(%this, %line)
{
echo(%line);
}
HTTPTest.get("blockland.us","index.html");
This will echo every line of HTML the server sends back. You'll need to write your own custom parsing algorithm, I made an HTML parser but it's at home so I won't have it for a while.
Using TCP objects is a good bit more complicated. This is the TCP equivalent for the same thing:
new TCPObject(TCPTest);
function TCPTest::onLine(%this, %line)
{
echo(%line);
}
function TCPTest::onConnected(%this)
{
%this.send("GET /index.html HTTP/1.0\r\nHost: blockland.us\r\n\r\n");
}
TCPTest.connect("blockland.us:80");
Those requests can get really complicated really fast. For example, if you wanted to send a request to the a server with a payload of a name and IP, you'd have to send a request like this:
%payload = "name=" @ %name @ "&ip=" @ %ip;
%request = "POST /something.php HTTP/1.0\r\nHost: someHost.com\r\nContent-Length=" @ strLen(%payload) @ "\r\n\r\n" @ %payload @ "\r\n";
So anyway, as I said the requests can get really complicated and go way outside the scope of any tutorial any one of us will type. You'll have to research HTTP requests yourself, my only tip is to always use HTTP/1.0 not HTTP/1.1 because 1.1 can send a bunch of way more complicated data that is much more difficult to parse.
Here's a couple of default functions you can overwrite for your needs:
TCPObject::onConnected( %this )
TCPObject::onConnectFailed( %this )
TCPObject::onDisconnected( %this )
TCPObject::onLine( %this, %line )
So that's for visiting webpages. Then there's code for downloading pages. This one actually isn't too much more complicated, but there's a few more steps.
new TCPObject(downloadTCP) { outputFile = "config/image.png"; }
function downloadTCP::onLine(%this, %line)
{
if(%line $= "") //end of headers
%this.setBinary(1);
}
function downloadTCP::onBinChunk(%this, %size)
{
cancel(%this.save);
%this.save = %this.schedule(1000, saveBufferToFile, %this.file);
}
downloadTCP.connect("blockland.us:80");
downloadTCP.send("GET /frontpage/frontpage-shaders-00.jpg HTTP/1.0\r\nHost: blockland.us\r\n");
I find this particular way of doing it a good bit easier than the "more correct" way of parsing the headers, setting the binary size, checking when the size goes over that threshold, and saving, but this way works 99.9% of the time and doesn't require parsing the headers.
Anyway, I'm sure you'll have a lot of questions, so go ahead and ask.