Greetings, I'm using PHP v4.3.7 on my Windows 2000 Server machine with IIS 4.0 as web server. When I tried to test a simple client server app (server.php and client.php - attached), I get the warning message below: -
Warning message =============== listenfd heard something, setting up new client Accepted 192.168.0.1:3313 as client[3] >From 192.168.0.1:3313, Client[3]: Hello ---------------------------------------------------------------------------- ---- Warning: socket_read() unable to read from socket [0]: An established connection was aborted by the software in your host machine. in D:\test\socket\server.php on line 150 Closing client[3] (192.168.0.1:3313) Question ======== What caused the warning message and how to I fix it? PS == If possible, please help me to improve these code, thanks. /* * start - server.php */ <html> <body> <?php set_time_limit(0); // defaults... define('MAXLINE', 2048); // how much to read from a socket at a time define('LISTENQ', 10); // listening queue define('PORT', 1149); // the default port to run on define('FD_SETSIZE', 10); // file descriptor set size (max number of concurrent clients)... // for kill the biatch... function killDaemon() { global $listenfd, $client; socket_close($listenfd); $msg = "Daemon going down!\n"; for ($i = 0; $i < FD_SETSIZE; $i++) { if ($client[$i] != null) { socket_write($client[$i], $msg, strlen($msg)); socket_close($client[$i]); } } print "Shutting down the daemon\n"; echo '<br>'; exit; } // whenever a client disconnects... function closeClient($i) { global $client, $remote_host, $remote_port; print "Closing client[$i] ({$remote_host[$i]}:{$remote_port[$i]})\n"; echo '<br>'; socket_close($client[$i]); $client[$i] = null; unset($remote_host[$i]); unset($remote_port[$i]); } // set up the file descriptors and sockets... // $listenfd only listens for a connection, it doesn't handle anything // but initial connections, after which the $client array takes over... $listenfd = socket_create(AF_INET, SOCK_STREAM, 0); if ($listenfd) { print "Listening on port " . PORT . "\n"; echo '<br>'; } else die("AIEE -- socket died!\n"); echo '<br>'; socket_set_option($listenfd, SOL_SOCKET, SO_REUSEADDR, 1); if (!socket_bind($listenfd, "0.0.0.0", PORT)) { socket_close($listenfd); die("AIEE -- Couldn't bind!\n"); echo '<br>'; } socket_listen($listenfd, LISTENQ); // set up our clients. After listenfd receives a connection, // the connection is handed off to a $client[]. $maxi is the // set to the highest client being used, which is somewhat // unnecessary, but it saves us from checking each and every client // if only, say, the first two are being used. $maxi = -1; for ($i = 0; $i < FD_SETSIZE; $i++) $client[$i] = null; // the main loop. while(1) { $rfds[0] = $listenfd; for ($i = 0; $i < FD_SETSIZE; $i++) { if ($client[$i] != null) $rfds[$i + 1] = $client[$i]; } // block indefinitely until we receive a connection... $nready = socket_select($rfds, $null, $null, null); // if we have a new connection, stick it in the $client array if (in_array($listenfd, $rfds)) { print "listenfd heard something, setting up new client\n"; echo '<br>'; for ($i = 0; $i < FD_SETSIZE; $i++) { if ($client[$i] == null) { $client[$i] = socket_accept($listenfd); socket_set_option($client[$i], SOL_SOCKET, SO_REUSEADDR, 1); socket_getpeername($client[$i], $remote_host[$i], $remote_port[$i]); print "Accepted {$remote_host[$i]}:{$remote_port[$i]} as client[$i]\n"; echo '<br>'; break; } if ($i == FD_SETSIZE - 1) { trigger_error("too many clients", E_USER_ERROR); echo '<br>'; exit; } } if ($i > $maxi) $maxi = $i; if (--$nready <= 0) continue; } // check the clients for incoming data. for ($i = 0; $i <= $maxi; $i++) { if ($client[$i] == null) continue; if (in_array($client[$i], $rfds)) { $n = trim(socket_read($client[$i], MAXLINE)); if (!$n) { closeClient($i); } // LINE 150 **************$$$$$$$$$$$$$$$$&&&&&&&&&&&&&&&&& else { // if a client has sent some data, do one of these: if ($n == "/killme") killDaemon(); else if ($n == "/quit") closeClient($i); else { // print something on the server, then echo the incoming // data to all of the clients in the $client array. print "From {$remote_host[$i]}:{$remote_port[$i]}, Client[$i]: $n\n"; echo '<hr>'; for ($j = 0; $j <= $maxi; $j++) { if ($client[$j] != null) { socket_write($client[$j], "From client[$i]: $n\r\n"); } } } } if (--$nready <= 0) break; } // end if() } // end for } ?> </body> </html> /* * end - server.php */ // *********************************************************************** /* * start - client.php */ <html> <body> <form action="<?php echo $PHP_SELF; ?>" method="post"> Enter some text:<br> <textarea name="message" rows=10 cols=60 wrap>hello</textarea> <input type="submit" name="submit" value="Send"> </form> <?php // form submitted $host ="192.168.0.1"; $port = 1149; // open a client connection $fp = fsockopen ($host, $port, $errno, $errstr); if (!$fp) { $result = "Error: could not open socket connection"; } else { // write the user string to the socket fputs($fp, $message); // close the connection // fclose ($fp); } ?> </body> </html> /* * end - client.php */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php