Forum

> > CS2D > Servers > Reading server response from RCon in PHP
Forums overviewCS2D overview Servers overviewLog in to reply

English Reading server response from RCon in PHP

2 replies
To the start Previous 1 Next To the start

old Reading server response from RCon in PHP

MikuAuahDark
User Off Offline

Quote
I need this for my Pixel Art server, where I need to fetch the canvas data via RCon and then encode the canvas data to PNG later on. This is my attempt so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
function ExecRCon($socket, string $rcon_pw, string $cmd): string
{
	$strbuild = ["\x01\x00\xF2"];
	$strbuild[] = chr(strlen($rcon_pw));
	$strbuild[] = $rcon_pw;
	$strbuild[] = pack('v', strlen($cmd));
	$strbuild[] = $cmd;
	
	$str = implode('', $strbuild);
	$str_length = strlen($str);
	
	socket_write($socket, $str);
	
	if(strcmp(socket_read($socket, 3), "\x01\x00\xF0") == 0)
	{
		socket_read($socket, 2);
		$command_result_length = unpack('v', socket_read($socket, 2))[1];
		
		return socket_read($socket, $command_result_length);
	}
	else
		throw new Exception('Failed to execute remote command');
}

I can send RCon command to the server (I can see Parse RCon ext. message), but reading the server message throws warning (and FALSE is returned in socket_read).

1
PHP Warning:  socket_read(): unable to read from socket [10040]: A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself.

Attempt to read the socket again simply hangs it.

This is how I call it. I can see ERROR: Unknown command 'Mon' (Mon) in the server.

1
2
3
4
5
6
7
<?php
require('rcon.php');
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($sock, '0.0.0.0', 3662);
socket_connect($sock, '192.168.43.54', 25000);

echo ExecRCon($sock, 'Password', 'Mon');

Can someone help? I'm relatively new on this thing.

old Re: Reading server response from RCon in PHP

Hajt
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
function send_commands($addr, $rcon, $cmds)
{
	$pieces = explode(":", $addr);
	$ip = $pieces[0];
	$port = $pieces[1];

	$fp = fsockopen("udp://$ip", $port, $errno, $errstr);

	$string = chr(1).chr(0).chr(242).chr(strlen($rcon)).$rcon.pack("S", strlen($cmds)).$cmds;
	fwrite($fp, $string);
	stream_set_timeout($fp, 1);
	$buf = fread($fp, 1024);
	fclose($fp);

	if (bin2hex($buf) == "0100f201340000") {
		return "RCon: Wrong rcon password.";
	}

	if (bin2hex($buf) == "0100f201330000") {
		return "RCon: Too many remote control attempts with wrong password!";
	}

	$buf = preg_split("/\x{F0}\x{00}\x{03}/", $buf);

	unset($buf[0]);

	return implode("\n", $buf);
}

echo send_commands("127.0.0.1:36963", "cosmo", "listplayers");
?>
To the start Previous 1 Next To the start
Log in to reply Servers overviewCS2D overviewForums overview