Page 1 of 1

fsock & UDP

Posted: Thu Apr 15, 2004 11:19 am
by EricS
I'm working on an application that is pulling info from a game server via UDP.

The problem is that when the server is full of players, I can't get all the info from the server. It seems a portion of the data at the end is cut off.

Here's the code

Code: Select all

// This is the server's IP (Forward Observer server for example)
	$IP = "69.25.16.150";
	//Default query port is 1717
	$sock = fsockopen( "udp://" . $IP, 1717);
	if($sock != false)
	{
		//Set time out for socket
		socket_set_timeout($sock, 2);
		//Write query packet to socket/send query to server
		fwrite($sock, "\\info&quote;);
		//Read response from server - set to 8192 bytes (could probably be less)
		$Info = @fread($sock, 8192);
		$Info2 = @fread($sock, 8192);
		//Close the socket
		fclose($sock);
/*
!!!!  Comment out the next line to hide the query data, it's for debug only
*/
		//print($Info . "<BR><BR>");				
	&#125;	
	

	// Parse \\info\\ query packet and load the contents into an array variable called $Infoarray
	$Infoarray = explode("&quote;, $Info);
	$Infoarray2 = explode("&quote;, $Info2);


 
	//meta tag for page to refresh every 30 seconds - THIS IS OPTIONAL
	//$result .= "<meta http-equiv=Refresh content=30>";


// Post server info
	
	//Server name from data
	// * you could also add in HTML tags to set background color, font size, type-face and color here
		$result .= "<BODY><LEFT>" . $Infoarray&#1111;2];
	//server IP and host port
		$result .= "<BR>IP: " . $IP . ":" . $Infoarray&#1111;8];
	//now it's just a matter of determining where in the array the data is that you want to display
		$result .= "<BR>Map: " . $Infoarray&#1111;10];
		$result .= "<BR>Players: ". $Infoarray&#1111;14]. " / " . $Infoarray&#1111;18];
		$result .= "<BR>Round: " . $Infoarray&#1111;38];
		$result .= "<BR>Time: " . $Infoarray&#1111;40].'<br><br>'."\n";
		
		foreach($Infoarray as $key => $value) &#123;
			$result .= $key.' = '.$value."<br>\n";
		&#125;
		
		foreach($Infoarray2 as $key => $value) &#123;
			$result .= $key.' = '.$value."<br>\n";
		&#125;
		
		$result .= "</BODY>";
	
	// Now display the concatenated string onto the page
		print($result);
Here are things I've tried.
1. I tried increasing the socket time out.
2. I tried increasing the read size.
3. I tried making a second fread call.(which is in the code above)

None of this has fixed the problem. I know that UDP has a maximum datagram size, but I also know that someone else is successfully doing what I want to do, but they aren't forth coming with their solution.

Any ideas would be greatly appreciated.
Eric Stewart

Posted: Thu Apr 15, 2004 6:56 pm
by JAM
I had no issues as you explain, but my code is unavailable (on a server I have no access to atm), so I can't check and verify.
Have you read:
http://forwardobserver.us/modules.php?n ... opic&t=157

You might have, but as we get different results.

Was a starting point for my original script.

Posted: Thu Apr 15, 2004 8:13 pm
by EricS
Thanks for the response.

That was actually the starting point for the script I'm working on. The author of that script also complains about the same problem. Apparently it's turning out to be a bug in the server code that is serving up the UDP datagram.

Still not totally sure yet though.
Thanks.