Guest User

Untitled

a guest
Jan 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. $host = "127.0.0.1";
  2. $port = 25003;
  3. // don't timeout!
  4. set_time_limit(0);
  5. // create socket
  6. $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socketn");
  7. // bind socket to port
  8. $result = socket_bind($socket, $host, $port) or die("Could not bind to socketn");
  9. // start listening for connections
  10. $result = socket_listen($socket, 3) or die("Could not set up socket listenern");
  11.  
  12. // accept incoming connections
  13. // spawn another socket to handle communication
  14. $spawn = socket_accept($socket) or die("Could not accept incoming connectionn");
  15. // read client input
  16. $input = socket_read($spawn, 1024) or die("Could not read inputn");
  17. // clean up input string
  18. $input = trim($input);
  19. echo "Client Message : ".$input;
  20. // reverse client input and send back
  21. $output = strrev($input) . "n";
  22. socket_write($spawn, $output, strlen ($output)) or die("Could not write outputn");
  23. // close sockets
  24. socket_close($spawn);
  25. socket_close($socket);
  26.  
  27. $ php -q test.php &
  28. [1] 2855
  29.  
  30. # Now we try again a SECOND copy...
  31. $ php -q test.php
  32.  
  33. PHP Warning: socket_bind(): unable to bind address [98]: Address already in use in test.php on line 9
  34. Could not bind to socket
  35.  
  36. $ telnet 127.0.0.1 25003
  37. Trying 127.0.0.1...
  38. Connected to 127.0.0.1.
  39. Escape character is '^]'.
  40. HELLO WORLD.
  41. Client Message : HELLO WORLD..DLROW OLLEH
  42. Connection closed by foreign host.
Add Comment
Please, Sign In to add comment