【发布时间】:2019-07-25 21:03:27
【问题描述】:
我的情况:
- 我有一个程序在端口 (4448) 上发送来自某些机器的大量数据。
- 我可以通过 telnet 连接检索这些数据。
远程登录步骤:
- 以 cmd 作为 ADMIN 启动
- 写入 telnet 并发送
- 写“open localhost 4448”并发送
- 写“GET xx,yy”
- 在此之后,我只是在现场阅读屏幕信息
xx 和 yy 是标识机器的数字
现在让我们继续 php 方面,我有我的 index.php,但是当我执行套接字绑定时,它给了我一个错误(在代码下报告)和关于代码,这来自:http://php.net/manual/en/sockets.examples.php 及其第一个示例,我也阅读并尝试了这个解决方案https://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP,它使用了 2 个 php 文件,在这两种情况下我都会遇到同样的错误......
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
$address = 'localhost';
$port = 4448;
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;
}
/* Send instructions. */
$msg = "GET xx,yy";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
这是我得到的错误:
警告:socket_bind():无法绑定地址 [10013]:尝试被 以访问权限禁止的方式访问套接字
我已经找过管理员权限了,我觉得没问题。
额外信息:我正在使用 PHP 7,我的 Web 服务器是 Xampp 包中的 Apache。
【问题讨论】: