Forum > Networking and Web Programming

Best way to do this in FPC.

(1/2) > >>

MuteClown:
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: --- 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<br>errno: $this->last_socket_err_num\n<br>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);
        }
}
?>
--- End code ---

what would be the best option to do this in pascal?

Leledumbo:
Use some networking library. The best 2 are LNet and Synapse.

MuteClown:
OK tried getting LNet
but when i tried to make i got this:


--- Code: ---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)
--- End code ---

And with Synapse how do i link it in lazarus, also where should i be storing it?

theo:

--- Quote from: MuteClown on March 30, 2010, 07:57:30 am ---And with Synapse how do i link it in lazarus, also where should i be storing it?

--- End quote ---

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:

--- Quote ---OK tried getting LNet
but when i tried to make i got this:
--- End quote ---
Use svn version. There are changes in the compiler regarding usage of absolute.

Navigation

[0] Message Index

[#] Next page

Go to full version