01-15-2009, 10:56 PM
I'm not good at working with sockets, but I search some websites and try to create php script, which will send commands to hltv(half life 1) via php. But something seems to be wrong because hltv on my pc doesn't do anything when I send some command.
So here is my code:
I only tryed to re-programm code to control hlds server, but when I run this php script, it will show no error but also hltv will not responding for my messages. (sorry for my bad english)
Thanks for help.
So here is my code:
PHP Code:
//get challenge from HLTV
$cmd = "\xFF\xFF\xFF\xFF"."challenge rcon"."\n";
$getchallenge = getServerData($cmd, '89.173.135.189', '27020');
$getchallenge = substr($getchallenge, 5);
$getchallenge = trim($getchallenge);
HalfLife_Rcon('myHLTVIPadress', 'myHLTVport', 'myHLTVrcon', 'record test.dem', $getchallenge);
function GetServerData($command, $ip, $port)
{
$data = '';
if (!$server_connection = fsockopen('udp://'.$ip, $port))
return false;
socket_set_blocking($server_connection, 1);
# Time out after 5 seconds
socket_set_timeout($server_connection, 5);
fwrite($server_connection, $command, strlen($command));
# This must time out first to make sure the right arguement is returned
# The above timeout is just to make sure it doesn't go over 4 seconds
$timeout = microtime();
$timeout += 4;
$i = 0;
$packets = 0;
do
{
do
{
$data .= fread($server_connection, 8192);
if (microtime() > $timeout)
{
# Connection timed out
fclose($server_connection);
return false;
}
$data_status = socket_get_status($server_connection);
}
while ($data_status['unread_bytes']);
if (GetInt8($data, $tmp = 0) == 254)
{
# We have a split packet!
$tmp = 0;
# Get rid of the crap at the start
GetInt32($data, $tmp);
GetInt32($data, $tmp);
$split_packet = str_pad(DecBin(GetInt8($data, $tmp)), 8, 0, STR_PAD_LEFT);
$packet_num = bindec(substr($split_packet, 0, 4));
$packets = bindec(substr($split_packet, 4));
$data = substr($data, $tmp);
$packet_data[$packet_num] = $data;
$data = '';
}
elseif ($packets > 0)
{
# Silly rcon problems
$tmp = 5;
# Get rid of the crap at the start
$data = substr($data, $tmp);
$packet_data[] = $data;
}
$i++;
}
while ($i < $packets);
fclose($server_connection);
if ($packets > 1)
{
ksort($packet_data);
$data = implode('', $packet_data);;
}
if (strlen($data) == 0)
{
# We got no data?! Something must have gone wrong
return false;
}
return $data;
};
function HalfLife_Rcon ($ip, $port, $rcon, $command, $challenge)
{
$cmd = "\xFF\xFF\xFF\xFFrcon ".$challenge." ".$rcon." ".$command;
if (!$serverdata = getServerData($cmd, $ip, $port))
return false;
$serverdata = substr($serverdata, 5);
$serverdata = trim($serverdata);
return $serverdata;
};
function getInt8($pos,$string) {
if (strlen($string) > $pos) {
$packarray = unpack('Ci',substr($string,$pos,1));
return $packarray["i"];
} else {
return 0;
}
};
I only tryed to re-programm code to control hlds server, but when I run this php script, it will show no error but also hltv will not responding for my messages. (sorry for my bad english)
Thanks for help.