以下代码用socket监听localhost:8080端口,当用户输入一串字符后,回显’you say ‘ +那串字符。
<?php
$host = 'localhost';
$port = '8080';
$welcome = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($welcome, $host, $port);
socket_listen($welcome, 3);
$sock = socket_accept($welcome);
$input = socket_read($sock, 1024);
$output = 'you say ' . $input;
socket_write($sock, $output, strlen($output));
socket_close($sock);
socket_close($welcome);
注意用telnet localhost 8080命令然后再输入一串字符查看结果