php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43325 feof() hangs on UDP stream
Submitted: 2007-11-18 23:12 UTC Modified: 2007-11-21 07:51 UTC
From: mertcan at dpozitif dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.2.5 OS: Centos 5.0
Private report: No CVE-ID: None
 [2007-11-18 23:12 UTC] mertcan at dpozitif dot com
Description:
------------
Using feof on UDP hangs. It should give true or false, in my example it should give true but it hangs. I've tried many ways but it always hangs.

Reproduce code:
---------------
$msg = "????challenge rcon";

$fp = fsockopen("udp://62.68.207.16",27015, $errno, $errstr);
fputs($fp, $msg, strlen($msg));
$response = fgets($fp, 1024);    			
$challenge = substr($response, 19, strlen($response) - 20);
		
$msg = "????rcon ".$challenge." \"123456\" ".$komut.chr(0);
fputs($fp, $msg, strlen($msg));

echo feof($fp) ? 'true':'false';
		
$response .= fgets($fp, 32);

fclose ($fp);

Expected result:
----------------
true

Actual result:
--------------
hangs

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-11-19 13:29 UTC] jani@php.net
Shouldn't you make the stream non-blocking? Try adding this after fsockopen:

stream_set_blocking($fp, false);
 [2007-11-19 14:56 UTC] mertcan at dpozitif dot com
It still hangs:

$fp = fsockopen("udp://".$ip,$port, $errno, $errstr);
stream_set_blocking($fp, 0);
 [2007-11-19 16:31 UTC] jani@php.net
Well, can you give a script that has some possibility of working properly? In the give script you have at least one variable which is not set anywhere: $komut

 [2007-11-19 17:19 UTC] mertcan at dpozitif dot com
i gave $tanim in function use:

function rcon_komut($komut, $rcon_pass, $ip, $port)
{
	$msg = "????challenge rcon";
    	$fp = fsockopen("udp://".$ip,$port, $errno, $errstr);
		
	stream_set_blocking($fp, 0);
		
    	fputs($fp, $msg, strlen($msg));
	$response = fgets($fp, 1024);    			
	$challenge = substr($response, 19, strlen($response) - 20);
		
	$msg="????rcon".$challenge."\"".$rcon_pass."\"".$komut.chr(0);
	fputs($fp, $msg, strlen($msg));
		
	while (!feof($fp))
		$response .= fgets($fp, 32);

    	fclose ($fp);
	$response = substr($response, 6, strlen($response)-6);
	return $response;
}
 [2007-11-20 08:44 UTC] jani@php.net
That's still just a piece of the script, a proper test script starts with <?php and ends in ?> and can be just copy'pasted and run..
 [2007-11-20 09:10 UTC] mertcan at dpozitif dot com
Here is the full program:

<?php

echo rcon_komut("meta list", "123456", "62.68.207.16", 27015);

function rcon_komut($komut, $rcon_pass, $ip, $port)
{
	$response = "";
	$msg = "????challenge rcon";
    	$fp = fsockopen("udp://".$ip,$port, $errno, $errstr);
		
	stream_set_blocking($fp, 0);
		
    	fputs($fp, $msg, strlen($msg));
	$response = fgets($fp, 1024);    			
	$challenge = substr($response, 19, strlen($response) - 20);
		
	$msg="????rcon".$challenge."\"".$rcon_pass."\"".$komut.chr(0);
	fputs($fp, $msg, strlen($msg));
		
	while (!feof($fp))
		$response .= fgets($fp, 4096);

    	fclose ($fp);

	return $response;
}

?>
 [2007-11-20 09:29 UTC] jani@php.net
Your script doesn't work because you don't get anything in $response in the first place. So you're passing invalid data around long before feof() is used and the server just sits there propably waiting for more data to be input before it gives any back..
 [2007-11-20 15:34 UTC] mertcan at dpozitif dot com
actually if i set stream_set_blocking for $fp to 1 it gives the result (which i posted the code below) if i change stream_set_blocking to 0, it does not give any result. in either ways if i use feof($fp) it hangs..

<?php

echo rcon_komut("meta list", "123456", "62.68.207.16", 27015);

function rcon_komut($komut, $rcon_pass, $ip, $port)
{
        $response = "";
        $msg = "????challenge rcon";
        $fp = fsockopen("udp://".$ip,$port, $errno, $errstr);

        stream_set_blocking($fp, 1);

        fputs($fp, $msg, strlen($msg));

        $response .= fgets($fp, 1024);

        fclose ($fp);

        return $response;
}

?>
 [2007-11-21 07:51 UTC] jani@php.net
Your script was buggy, this works:

<?php

echo rcon_komut("meta list", "123456", "62.68.207.16", 27015);

function rcon_komut($komut, $rcon_pass, $ip, $port)
{
  $fp = fsockopen("udp://".$ip,$port, $errno, $errstr);
  socket_set_timeout($fp, 3);

  $prefix = "\xff\xff\xff\xff";
  $msg = "{$prefix}challenge rcon\n";
  fputs($fp, $msg, strlen($msg));

  $response = fgets($fp, 1024);    
  $challenge = substr($response, 19, strlen($response) - 20);

  $msg = "\xff\xff\xff\xffrcon $challenge \"$rcon_pass\" $komut\n";
  fputs($fp, $msg, strlen($msg));

  $status = socket_get_status($fp);

  while($status['unread_bytes']) {
    $response .= fgets($fp, 2048);
    $status = socket_get_status($fp);
  }
  fclose ($fp);
  return $response;
}
?>

Using feof() makes no sense with udp streams..

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Nov 12 19:01:28 2024 UTC