一、server.PHP服务端:
if (($ret = socket_bind($sock,$address,$port)) < 0) {
echo "创建套接字失败原因 " . socket_strerror($ret) . "\n";
}
//监听
if (($ret = socket_listen($sock,5)) < 0) {
echo "监听失败原因 " . socket_strerror($ret) . "\n";
}
do {
//接收命令
if (($msgsock = @socket_accept($sock)) < 0) {
echo "命令接收失败原因: " . socket_strerror($msgsock) . "\n";
break;
}
$msg = "\nPHP Test Server. \n" ."用quit,shutdown,sun...等命令测试.\n";
@socket_write($msgsock,$msg,strlen($msg));
do {
if (false === ($buf = @socket_read($msgsock,2048,PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror($ret) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
if ($buf == 'sun') {
echo'what are you doing?';
}
$talkback = "Backinformation : '$buf'.\n";
socket_write($msgsock,$talkback,strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
二、client.PHP客户端:
具体操作,在DOS下用PHP命令打开SERVER,处于监听状态..
再打开一个DOS窗口,用PHP命令打开CLIENT,会得到服务端的回应....
原文链接:https://www.f2er.com/php/18050.html