Game Development Community

Using TCPObject

by Misha "Scourage" Sole · in Torque Game Engine · 07/23/2003 (10:50 am) · 8 replies

I am trying to post a large amount of data to a web server, and the get method does not allow me to send enough characters. I hear that one can use the post method with tcpobject, but I am unclear how to do this properly. I have also found no documentation on the tcpobject function. If someone could point me in the right direction I would appreciate it.

Thanks.

#1
07/23/2003 (1:51 pm)
I should probably do a more detailed tutorial on the TCPObject at some time... but to answer your question:

%obj = new TCPObject(TCPObj);	
%obj.connect("www.yoursite.com:80");

function TCPObj::onConnected(%this)
{	
	// When connected to the server send large amount of data
	// Read in large amount of text data

	%filename= %largeTextFile;
   %file = new FileObject();
   if(%file.openForRead(%filename))
   {
   	%text = %file.readLine();
      while(!%file.isEOF())
      {
        %data = %data @ "{=||=}" @ %file.readLine(); // {=||=} is used to represent a CR and/or LF 
        															  // and should be parsed on the server side to 
        															  // such for an exact duplicate of the original 
        															  // data to be created
      }
   }
   %file.delete();
   
   // Replace SPC and + with HTML encoded alternative
   %data=strreplace(%data," ","%20");
	%data=strreplace(%data,"+","%2B");
   
	//Create HTML variable to hold data
	%data="data=" @ %data;
	
	//Create HTML 1.0 POST line.  Must do STRLEN of %data for Content-length HTML header
	%htmlpost="POST /path/to/serverscript.php HTTP/1.0\nHost: www.yoursite.com:80\nUser-Agent: Torque/1.0 \nAccept: */*\nContent-length: " @ strlen(%data) @ "\nContent-type: application/x-www-form-urlencoded\n\n" @ %data;
	
	//Send POST line to the webserver
	%this.send(%htmlpost @ " \r\n");
}

Your biggest help would be to look for the HTML 1.0 and 1.1 specs as they describe the protocol for both the client and server side.
#2
04/16/2008 (2:21 pm)
I tried using tcpobject and post protocol to send large data(~56k) and it failed, it didn't get to the server, if I try it on a smaller size(eg. 28k) it succeeded after a while. is there any limitation on how much I can send or timeout parameter in tcpobject that I can tweak?
here's my code:
function httpPage::onConnected(%this)
{
   warn("Connected ...");
   if(!isfile(%this.filename)){
      %query = "GET " @ %this.page @ " HTTP/1.0\nHost: " @ %this.Address @ "\n\n";
      %this.send(%query);
   }
   else{
         %file = new FileObject();
         if(%file.openForRead(%this.filename))
         {
   	      %text = %file.readLine();
            while(!%file.isEOF())
            {
               %text = %text @ "\r\n" @ %file.readLine();
            }
         }
         %file.delete(); 
   
         %text=URLEncode(%text);
         %boundary="---------------------------29772313742745";
	      %data="--"@%boundary@"\r\nContent-Disposition: form-data; name=\"SampleFile\";filename=\"" @ filename(%this.filename) @ "\"; Content-Type:text/plain\r\n\r\n" @ %text @"\r\n\r\n--" @%boundary ;
	      
	     // %data= %data@ "\r\nContent-Disposition: form-data; name="userName"

	      %httpCmd="POST "@%this.page @ " HTTP/1.1\nHost: "@ %this.Address @"\nUser-Agent: Mozilla/5.0\nAccept: */*"@"\nContent-Length:"@ strlen(%data)@"\nContent-Type: multipart/form-data; charset=UTF-8; boundary="@%boundary@"\r\n\r\n"
	       @ %data  @ "\r\n";

	     // echo(%httpCmd);
	      
	      %this.send(%httpCmd);
   
   }
   
}
#3
04/16/2008 (7:51 pm)
You have to be carefull with chunk data. I'm currently using a modifier release of TCPObject to support exchange of binary information and taking care of chunked data.
#4
04/16/2008 (8:32 pm)
Do you have a link to this modifier release of tcpobject? By chunk data do you mean data separated by \r\n?
#5
04/17/2008 (1:52 pm)
I did some debugging and pinpointed the cause of the problem: I think %file.openForRead(%this.filename) took too long for a large file, and if you don't send something down the pipe when you are connected for a while, you get kicked out. how do I solve this problem?
#6
04/17/2008 (1:59 pm)
I did some debugging and pinpointed the cause of the problem: I think %file.openForRead(%this.filename) took too long for a large file, and if you don't send something down the pipe when you are connected for a while, you get kicked out. how do I solve this problem?
#7
04/17/2008 (10:28 pm)
By chunked data, I mean that the server (HTTP server) will not send all your information in one block (look at your HTTP header). This week-end I will look over modifications I did on my code and try to post it.
#8
06/11/2008 (7:52 am)
@Frank: Did you ever post our modifications somewhere for sending binary data over TCPObject?