Guest User

Untitled

a guest
Jan 5th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. <?php
  2. // don't timeout
  3. set_time_limit (0);
  4. // set some variables
  5. $host = "127.0.0.1";
  6. $port = 1234;
  7. // create socket
  8. $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
  9. socket\n");
  10. // bind socket to port
  11. $result = socket_bind($socket, $host, $port) or die("Could not bind to
  12. socket\n");
  13. // start listening for connections
  14. $result = socket_listen($socket, 3) or die("Could not set up socket
  15. listener\n");
  16. echo "Waiting for connections...\n";
  17. // accept incoming connections
  18. // spawn another socket to handle communication
  19. $spawn = socket_accept($socket) or die("Could not accept incoming
  20. connection\n");
  21. echo "Received connection request\n";
  22. // write a welcome message to the client
  23. $welcome = "Welcome to Thanatos RPG\n\rPlease input your username:";
  24. socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send
  25. connect string\n");
  26. //function for writing to a socket; use send_telnet("information to send here");
  27. function send_telnet($output){
  28. socket_write($output, strlen ($output)) or die("Could not write output\n");
  29. echo "Sent output: " . trim($output) . "\n";
  30. }
  31. // keep looping and looking for client input
  32. do
  33. {
  34. // read client input
  35. $input = socket_read($spawn, 1024, 1) or die("Could not read input\n");
  36. if (trim($input) != "")
  37. {
  38. echo "Received input: $input\n";
  39. // if client requests session end
  40. if (trim($input) == "END")
  41. {
  42. // close the child socket
  43. // break out of loop
  44. socket_close($spawn);
  45. break;
  46. }
  47. // otherwise...
  48. else
  49. {
  50. // get username
  51. if (trim($input) == "jack")
  52. {
  53. $output = "Welcome, Jack, to Thanatos RPG. Please input your password:";
  54. send_telnet($output) or die("Could
  55. not write output\n");
  56. echo "Sent output: " . trim($output) . "\n";
  57. }
  58. }
  59. }
  60. } while (true);
  61. // close primary socket
  62. socket_close($socket);
  63. echo "Socket terminated\n";
  64. ?>
Add Comment
Please, Sign In to add comment