Best way to do this in FPC.
lazarus / 38-Networking and Web Programming
----------------------------------------------------------------------
MuteClown · 2010-03-30 02:12
Hey,
i am working on building a admin tool for a game server, i am making a
bot to connect to the game servers rcon. The thing is i have no
experience with how networking works, i guess this is a good time for
me to learn
I found someone made something that would connect to the rcon but with
php, so im going to try and replicate it with fpc.
Here is the initial connection class in php:
Code: [Select] class q3rcon {
// Some vars needed for rcon info
private $password;
// Some vars to store what server we will connect to
private $address;
private $port;
private $socket_connection;
// Misc. other vars
private $last_socket_err_num;
private $last_socket_err_str;
// Constructor: takes the IP address (or hostname), port, and
rcon password of
// the server and opens a connection.
public function __construct($serv_address, $serv_port,
$serv_password, $timeout=30) {
$this->address = $serv_address;
$this->port = intval($serv_port);
$this->password = $serv_password;
/*
echo $this->address;
echo $this->port;
echo $this->password;
*/
$this->last_socket_err_num = -1;
$this->last_socket_err_str = "";
// Open up the connection wih the given address and
port
$this->socket_connection = fsockopen("udp://" .
$this->address, $this->port, $this->last_socket_err_num,
$this->last_socket_err_str, $timeout);
if (!$this->socket_connection) {
die("Could not connect with given
ip:port\n
errno: $this->last_socket_err_num\n
errstr:
$this->last_socket_err_str");
}
//turn blocking on so that you can't over-spam the
connection
}
// Precondition: A socket has been opened without error by the
constructor
// Postcondidtion: The given command will be sent and a
response will be
// recoverable from the function get_response()
public function send_command($cmd) {
fwrite($this->socket_connection, str_repeat(chr(255),
4) . "rcon " . $this->password . " " . $cmd . "\n");
}
// Get the server's response to our previous query.
// Precondition: A command should have already been sent with
send_command($cmd).
// Postcondidtion: The server's response string will be
returned.
public function get_response(&$er = false) {
$er = false;
stream_set_timeout($this->socket_connection, 0,
500000);
$buffer = "";
while ($buff = fread($this->socket_connection, 9999))
{
if(!substr($buff, 4, 8) == "print"){
$er = true;
}
$buffer .= substr($buff, 4 + 6);
}
return $buffer;
}
public function close() {
fclose($this->socket_connection);
}
}
?>
what would be the best option to do this in pascal?
----------------------------------------------------------------------
Leledumbo · 2010-03-30 03:54
Use some networking library. The best 2 are LNet and Synapse.
----------------------------------------------------------------------
MuteClown · 2010-03-30 05:57
OK tried getting LNet
but when i tried to make i got this:
Code: [Select]lwebserver.pp(645,53) Error: absolute can only be
associated with a var or const
lwebserver.pp(654,27) Warning: Local variable "lServerSocket" does not
seem to be initialized
lwebserver.pp(705,53) Error: absolute can only be associated with a
var or const
lwebserver.pp(729,7) Warning: Local variable "lServerSocket" does not
seem to be initialized
lwebserver.pp(877,52) Error: absolute can only be associated with a
var or const
lwebserver.pp(1119,4) Warning: User defined: TODO
lwebserver.pp(1255) Fatal: There were 3 errors compiling module,
stopping
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did
not specify a source file to be compiled)
And with Synapse how do i link it in lazarus, also where should i be
storing it?
----------------------------------------------------------------------
theo · 2010-03-30 07:43
Quote from: MuteClown on March 30, 2010, 07:57:30 amAnd with Synapse
how do i link it in lazarus, also where should i be storing it?
Create a package or download the one I've posted here
http://forum.lazarus.freepascal.org/index.php/topic,7089.msg33281.html
#msg33281
You don't have to rebuild the IDE, because Synapse is not visual
components.
----------------------------------------------------------------------
Leledumbo · 2010-03-30 08:23
QuoteOK tried getting LNet
but when i tried to make i got this:Use svn version. There are changes
in the compiler regarding usage of absolute.
----------------------------------------------------------------------
MuteClown · 2010-03-30 19:27
Ok, so i installed lNet and can use it with the GUI, but how do i use
it in non-graphical mode?
So i need to link the units in lazarus, -FU -FI how do i do it in
lazarus?
----------------------------------------------------------------------
Leledumbo · 2010-04-05 09:47
Quotebut how do i use it in non-graphical mode?Don't use the component
palette. Create the connection from code.