服务端
<?php $socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr); if (!$socket) { echo "$errstr ($errno)<br /> "; } else { while ($conn = stream_socket_accept($socket)) { fwrite($conn, "Content-Type: text/plain "); fwrite($conn, "hello socket "); fclose($conn); } fclose($socket); } ?>
客户端
<?php $fp = stream_socket_client("tcp://127.0.0.1:8000", $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br /> "; } else { while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp); } ?>