一、服务器-server.php <?php error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); $address = '192.168.15.166'; $port = 10000; if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "/n"; } if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "/n"; } if (socket_listen($sock, 5) === false) { echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "/n"; } do { if (($msgsock = socket_accept($sock)) === false) { echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "/n"; break; } if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) { echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "/n"; break; } if ($buf == 'quit') { echo "Close../r/n"; break; } if ($buf == 'shutdown/r/n') { socket_close($msgsock); echo "Close../r/n"; break; } $talkback = "PHP: $buf/r/n"; socket_write($msgsock, $talkback, strlen($talkback)); echo "$buf/r/n"; socket_close($msgsock); } while (true); socket_close($sock); ?> 二、客户端-client.php <?php error_reporting(E_ALL); echo "<h2>TCP/IP Connection</h2><br />"; $service_port = 10000; $address = '192.168.15.166'; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); echo "Attempting to connect to '$address' on port '$service_port'...<br />"; socket_connect($socket, $address, $service_port); $in = $_GET['key']."/r/n"; socket_write($socket, $in, strlen($in)); $out = socket_read($socket, 1024, PHP_NORMAL_READ); echo $out; socket_write($socket, "quit/r/n", strlen("quit/r/n")); socket_close($socket); ?>