// set some variables
$host = gethostbyname(gethostname());
$port = 9996;
// don't timeout!
// 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 socket connection on " . $host . ":" . $port . "\n\r");
// accept incoming connections
// spawn another socket to handle communication
$socketAccept = '';
$socketAccept = socket_accept($socket) or die("Could not accept incoming connection\n");
echo("Incomming connection accepted!\n\r");
// Make a file to save incoming socket data to
$randFileName = 'test_' . md5(mt_rand(0, 1000) ) . '.test';
$inFileHandle = fopen($randFileName, 'wb');
while(!$input == '')
// read client input
$input = socket_read($socketAccept, 1024) or die("Could not read input\n\r");
// save data to file
fwrite($inFileHandle, $input, 1024);
// close file handle
echo("Socket file data read and saved to\n\r" . $randFileName . "\n\r");
// shut down sockets
// close sockets
This script works the first time itīs run and my client script on another computer sends data and this server script saves it to a file. And all is well.
Now.. the problem is that when i want to run the server script again and try to send data to it with my client script. The client script gives the error that a connection on the server script is blocking the socket_write() function in my client script.
It would seem that the socket first opened the first run on the server script is still open and wont accept new connections.
I canīt get whats wrong. Im really looking for your advice.
By request i can post the client script also, but i donīt think its where the problem lies.
Sorry for the english :)
- I am running the script from Win7 command line via the php.exe executible.
Thank you in advance.
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.