Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $host = "127.0.0.1";
- $port = 25003;
- // don't timeout!
- set_time_limit(0);
- // create socket
- $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socketn");
- // bind socket to port
- $result = socket_bind($socket, $host, $port) or die("Could not bind to socketn");
- // start listening for connections
- $result = socket_listen($socket, 3) or die("Could not set up socket listenern");
- // accept incoming connections
- // spawn another socket to handle communication
- $spawn = socket_accept($socket) or die("Could not accept incoming connectionn");
- // read client input
- $input = socket_read($spawn, 1024) or die("Could not read inputn");
- // clean up input string
- $input = trim($input);
- echo "Client Message : ".$input;
- // reverse client input and send back
- $output = strrev($input) . "n";
- socket_write($spawn, $output, strlen ($output)) or die("Could not write outputn");
- // close sockets
- socket_close($spawn);
- socket_close($socket);
- $ php -q test.php &
- [1] 2855
- # Now we try again a SECOND copy...
- $ php -q test.php
- PHP Warning: socket_bind(): unable to bind address [98]: Address already in use in test.php on line 9
- Could not bind to socket
- $ telnet 127.0.0.1 25003
- Trying 127.0.0.1...
- Connected to 127.0.0.1.
- Escape character is '^]'.
- HELLO WORLD.
- Client Message : HELLO WORLD..DLROW OLLEH
- Connection closed by foreign host.
Add Comment
Please, Sign In to add comment