Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // don't timeout
- set_time_limit (0);
- // set some variables
- $host = "127.0.0.1";
- $port = 1234;
- // create socket
- $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
- socket\n");
- // bind socket to port
- $result = socket_bind($socket, $host, $port) or die("Could not bind to
- socket\n");
- // start listening for connections
- $result = socket_listen($socket, 3) or die("Could not set up socket
- listener\n");
- echo "Waiting for connections...\n";
- // accept incoming connections
- // spawn another socket to handle communication
- $spawn = socket_accept($socket) or die("Could not accept incoming
- connection\n");
- echo "Received connection request\n";
- // write a welcome message to the client
- $welcome = "Welcome to Thanatos RPG\n\rPlease input your username:";
- socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send
- connect string\n");
- //function for writing to a socket; use send_telnet("information to send here");
- function send_telnet($output){
- socket_write($output, strlen ($output)) or die("Could not write output\n");
- echo "Sent output: " . trim($output) . "\n";
- }
- // keep looping and looking for client input
- do
- {
- // read client input
- $input = socket_read($spawn, 1024, 1) or die("Could not read input\n");
- if (trim($input) != "")
- {
- echo "Received input: $input\n";
- // if client requests session end
- if (trim($input) == "END")
- {
- // close the child socket
- // break out of loop
- socket_close($spawn);
- break;
- }
- // otherwise...
- else
- {
- // get username
- if (trim($input) == "jack")
- {
- $output = "Welcome, Jack, to Thanatos RPG. Please input your password:";
- send_telnet($output) or die("Could
- not write output\n");
- echo "Sent output: " . trim($output) . "\n";
- }
- }
- }
- } while (true);
- // close primary socket
- socket_close($socket);
- echo "Socket terminated\n";
- ?>
Add Comment
Please, Sign In to add comment