Forum Moderators: coopster

Message Too Old, No Replies

fsockopen problem

         

Ironfist

8:51 am on Oct 28, 2003 (gmt 0)

10+ Year Member



I have written this code that connects to a server, and downloads a file called GAMELST. However, although it works, it takes a very long time to complete and often timeouts. The file when it does download OK is less than 20kb.

Can anyone spot what the problem is?


// don't timeout!
set_time_limit(0);

$fp = fsockopen($server, $port, &$errno, &$errstr) or die("$errno: $errstr");
if(!$fp)
{
echo "Unable to open MK server connection";
}
else
{
fputs($fp, "GAMELST\n");
socket_set_timeout($fp, 2);

$res='';
while (!feof($fp))
{
$res=$res.fread($fp, 1024);
}

//save game info to file
$gamefile = fopen("$date/games.txt", "w");
fwrite($gamefile,$res);
fclose($gamefile);
}

panic

6:03 pm on Oct 28, 2003 (gmt 0)

10+ Year Member



Are you sure it's not the proxy that limits this?

-P

Ironfist

1:12 pm on Nov 1, 2003 (gmt 0)

10+ Year Member



What do you mean?
Which proxy?

Glacai

2:25 pm on Nov 1, 2003 (gmt 0)

10+ Year Member



Are you closing $fp?

Ironfist

5:08 pm on Nov 1, 2003 (gmt 0)

10+ Year Member



Yes, I close $fp later on in the script after downloading data based on the contents of this first file.

This is the complete script:
(not sure why, but these code tags did not retain my indents)


<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>

<BODY>
<h1>MK Games Results (MK server)</h1><BR><BR>

<?php
$server = "metalknights.com";
$port = 3333;

//set data directory, all game info files written to directory named by date.
$date = date ('jmYHi');
$date = "MK_$date";
mkdir ("$date", 0777);

// don't timeout!
set_time_limit(0);

$fp = fsockopen($server, $port, &$errno, &$errstr) or die("$errno: $errstr");
if(!$fp)
{
echo "Unable to open MK server connection";
}
else
{
fputs($fp, "GAMELST\n");
# $start = time();
socket_set_timeout($fp, 2);
// $res = fread($fp, 30000);

$res='';
while (!feof($fp)) {
$res=$res.fread($fp, 1024);
}

//save game info to file
$gamefile = fopen("$date/games.txt", "w");
fwrite($gamefile,$res);
fclose($gamefile);

// read game list file
$games= file("$date/games.txt", 'r');
$num_games = count($games);
echo "total number of games : $num_games<br>";
for ($i=0; $i<$num_games; $i++)
{
$line = explode (",", $games[$i]);
//display won games
if ($line[3]=="Game WON!")
{
# echo "<table><tr><td width=200>$line[0]</td><tr></table>";
//get game details
fputs($fp, "GAMEDET $line[0]\n");
#$start = time();//maybe don't need this line
socket_set_timeout($fp, 2);//maybe don't need this line
$detres = fread($fp, 700);
//save game details to file
$gamedetsfile = fopen("$date/game_".$line[0].".txt", "w");
fwrite($gamedetsfile,$detres);
fclose($gamedetsfile);
}
}

}
//close socket connection to server
fclose($fp);

// read game list file
$games= file("$date/games.txt", 'r');//maybe not needed as already read file
$num_games = count($games);//ditto

for ($i=0; $i<$num_games; $i++)
{
$line = explode (",", $games[$i]);
//display won games
if ($line[3]=="Game WON!")
{
$game_details= file ("$date/game_".$line[0].".txt", "r");
echo "<b><font color=blue size=4><br>$game_details[1] : </b></font>";
echo "A $game_details[2] game played on the map \"$game_details[3]\". This game was a $game_details[5] and finished $game_details[4] .<br>";
For ($p=9; $p<19; $p++)
{
$deadguys= explode (",", $game_details[$p]);
for ($d=0; $d<2; $d++){
if ($deadguys[1]){
echo "$deadguys[$d] <br>";
}
}
}
}
}
?>
</BODY>
</HTML>