Problems with TCPObject and iPhone 3g
by Hitesh Patel · in iTorque 2D · 11/13/2009 (5:56 pm) · 13 replies
In my game I download high scores from the web using TCPObject. This works fine when the wifi is enabled on the iphone. But if there is no wifi available and I am using 3G the scores are not downloaded. The data being downloaded is really small. I am not sure if there is a time out period when using TCPObject. Maybe as the internet speed is slow with 3G and thats was casing this problem, not sure. Any help will be appreciated.
#2
11/14/2009 (6:38 pm)
On the 3G you have to keep in mind that you have significantly higher latency and lower bandwidth and especially a lot of packet drops which means that tcp takes its time to get the correct data in the correct order for anything thats larger than a few bytes
#3
We've added functionality to turn the radio on automatically for TCPObject requests, as well as independently if you wish as we also use the TCPobject for our high scores in Flipt, and it works through 3G and GPRS/Edge without and issues, other than a slight bit of additional latency as Marc mentioned. This code will be in the next release of iTGB, which should be very soon.
If it isn't a radio issue, put some breaks on the TCPObject functions that rwceive data and see if anything it all is coming back from your server.
If
11/15/2009 (10:55 am)
It could also be that the radio isn't enabled when you try sending. Does it work if you first connect to a web page in Safari, and them run your app afterwards? If so, then it is a problem with the radio not turned on.We've added functionality to turn the radio on automatically for TCPObject requests, as well as independently if you wish as we also use the TCPobject for our high scores in Flipt, and it works through 3G and GPRS/Edge without and issues, other than a slight bit of additional latency as Marc mentioned. This code will be in the next release of iTGB, which should be very soon.
If it isn't a radio issue, put some breaks on the TCPObject functions that rwceive data and see if anything it all is coming back from your server.
If
#4
11/16/2009 (10:20 am)
Firstly thanks to all for the reply. I have tried HTTP object and it didnt make any differnece. When I access the same page using Safari it works fine and shows the scores in seconds. But in the game it doesnt. How do I turn the radio on automatically for TCPObject requests in iTGB????
#5
Like TCPObject::onConnectionError , onDNSFailed,etc.
Implement ALL of them, and look for errors?
11/17/2009 (1:27 pm)
Have you implemented all the necessary messages to find errors ??Like TCPObject::onConnectionError , onDNSFailed,etc.
Implement ALL of them, and look for errors?
#6
11/18/2009 (5:35 am)
I have implemented different msgs for each to see where its getting stuck but it looks like it goes through the connection and its gets stuck when downloading. As mentioned earlier it all works perfectly fine when I am connected through the wifi. I also ran a speed test to see the diffrence in speed at as expected wifi was 10 times faster then 3G. But even though its slow I am sure it shouldnt take that long to download the high scores as it downloads it in a few seconds when i am using safari with 3G.
#7
11/18/2009 (12:50 pm)
Ok, is the message being returned from a script of yours? One thing i noticed is that the server side was not flushing its output fast enough, leaving torque to wait in despair (as it is not a intense net object). When i added echo("\n") to the end of my php script, it flushed the end of the lines immediately down to my app and i could reduce my timeout from 30 seconds to between 3 and 8 seconds on edge/gprs/3g.
#8
Mod note : changed some information you shouldnt give out
11/19/2009 (6:36 am)
I tried adding the echo ("\n") at the end of my php script but still no joy. I was chekcing to see where the delay is and I found out that it takes it time after the onConnect. When I am connected through Wifi it takes atleast 10 seconds before the TCPObject::onLine code is executed. The thing is though when i am submitting the score it submits it fine with 3G (obviously its a bit slower then wifi but it does submit). Here is my php script, I am not sure if its anything to do with itMod note : changed some information you shouldnt give out
<?php
$service = "****";
$username = "*******";
$password = "*******";
$database = "*******";
mysql_connect($service, $username, $password);
@mysql_select_db($database) or die("Unable to select database");
$nooflevels = 50;
$allscores = "allscore:";
for ($i = 0; $i <= $nooflevels; $i++)
{
// get high score
$query = "***";
$result = mysql_query($query);
$thisobjectsused = mysql_result($result, "***");
$allscores = "$allscores $thisobjectsused";
}
echo ("$allscores");
echo("n");
?>
#9
The code is perfectly fine,
Try putting a breakpoint in the code side :
And waiting for the connection to hit. One thing we did see was a delay, but ALL the "lines" had already come into the buffer. The reason being that the protocol probably sends packets in a bunch(x bytes of data) and the content already comes into the system code side already.
Put a watch/look at the full value of the buffer when it hits the breakpoint, see what it has recieved.
11/19/2009 (8:09 am)
Try not to give away server information, database and table names in a forum -The code is perfectly fine,
Try putting a breakpoint in the code side :
U32 TCPObject::onReceive(U8 *buffer, U32 bufferLen)
And waiting for the connection to hit. One thing we did see was a delay, but ALL the "lines" had already come into the buffer. The reason being that the protocol probably sends packets in a bunch(x bytes of data) and the content already comes into the system code side already.
Put a watch/look at the full value of the buffer when it hits the breakpoint, see what it has recieved.
#10
And many thanks for removing the info that shouldnt have been on the forum.
11/19/2009 (11:33 am)
Ummmm...sorry but i am bit confused as to where to put the code and about the watch/look. I am only 3 months old in TGB age :) (actually never did any programming before this). Below is the code I use to download scores, thanks to people on the forum for this code:function TCPGetAllScore(%this)
{
%obj = new TCPObject(TCPAllScoreReciever);
%obj.connect("mywebsite.com:80");
menumsgline.text = "Contacting server...";
}
function TCPAllScoreReciever::onDNSResolved(%this)
{
menumsgline.text = "Connection established...";
}
function TCPAllScoreReciever::onDNSFailed(%this)
{
menumsgline.text = "Could not download. Please try later.";
}
function TCPAllScoreReciever::onConnected(%this)
{
echo ("connected");
menumsgline.text = "Downloading scores, please wait...";
%httpCmd="POST /myphp.php HTTP/1.1nHost: mywebsite.com:80nUser-Agent: Torque/1.0 nAccept: */*nContent-Length: "@100@"nContent-Type: application/x-www-form-urlencoded; charset=UTF-8nn";
echo(%httpCmd);
%this.send(%httpCmd @ "\r\n");
}
function TCPAllScoreReciever::onConnectFailed(%this)
{
error("Connection failed");
menumsgline.text = "Could not download. Please try later.";
%this.delete();
}
function TCPAllScoreReciever::onDisconnect(%this)
{
}
function TCPAllScoreReciever::onLine(%this, %line)
{
echo(%line);
if(firstWord(%line) $= "allscore:") // returned from php file - indicates success
{
$allwebscore = %line;
echo ($allwebscore);
menumsgline.text = "Scores downloaded succesfully.";
%this.disconnect();
}
}And many thanks for removing the info that shouldnt have been on the forum.
#11
I assume you replaced the domain on your own real project, because mywebsite.com is a dummy adress as /myphp.php is a dummy filename
11/19/2009 (9:59 pm)
your httpcmd, at least if I don't miss something, is missing a significant amount of \ in front of the ns that are meant to be line breaks.I assume you replaced the domain on your own real project, because mywebsite.com is a dummy adress as /myphp.php is a dummy filename
#12
Hitesh :
You should read the XCode documentation on setting a breakpoint in code ;p But basically, you need to click on the gutter in the editor on the left, and a blue arrow appears. As soon as some activity appears inside the tcpObject itself (you run the game through the XCode IDE- in debug configuration) , the code will tell you how much you have received etc.

When it stops there in code with a red arrow - You hover the mouse over "buffer" to see whats inside. Is it all of the data you expect ? or is it only some - this will tell you if its torque side or php side.
11/20/2009 (3:53 am)
Marc, i think its the forums that chop off the \ items , after editing (weird right?). Hitesh :
You should read the XCode documentation on setting a breakpoint in code ;p But basically, you need to click on the gutter in the editor on the left, and a blue arrow appears. As soon as some activity appears inside the tcpObject itself (you run the game through the XCode IDE- in debug configuration) , the code will tell you how much you have received etc.

When it stops there in code with a red arrow - You hover the mouse over "buffer" to see whats inside. Is it all of the data you expect ? or is it only some - this will tell you if its torque side or php side.
#13
11/24/2009 (12:30 pm)
I have managed to resolve the issue. The problem was in the httpcmd. The content-length was cuasing the problem, i changed it to 1000. I dont understand why it works with wifi and not 3g. But after making the change it all works fine. Thanks everyone for your help.
Torque Owner Justin Mosiman
Opsive
Here's the code:
new HTTPObject(HighScoresObject); HighScoresObject.get("www.opsive.com:80", "/iPhoneApps/WarEvolved/highScores.php");I don't have a problem with it on WiFi or 3G, you can see the raw scores at www.opsive.com/iPhoneApps/WarEvolved/highScores.php. See if that fixes your problem.