当我添加socket_set_nonblock时,插座接受给出错误
我的代码工作正常,但是当我更改并添加socket_set_nonblock($ socket)
socket_accept给我错误11时,这是我的
<?php
session_start();
require "checklogin.php";
error_reporting(0);
$user=$_SESSION["usr"];
set_time_limit(0);
ob_implicit_flush();
$host="127.0.0.1";
$port=11287;
$greeting="\nWelcome to my PHP server(listener)\nto quit type 'quit\nto shutdown the server type 'shutdown'.\n";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Err: " . socket_last_error());
socket_bind($socket, $host, $port) or die("Err: " . socket_last_error());
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {}
$result = socket_listen($socket) or die("can't set up listener");
socket_set_nonblock($socket) or die("rip multiple: " . socket_last_error());
while (true) {
if ($spawn = socket_accept($socket) or die("err: can't accept - " . socket_last_error())) {
socket_write($spawn, $greeting, strlen($greeting));
}
do {
$input = socket_read($spawn, 1024, PHP_NORMAL_READ) or die("Could not read input\n");
socket_write($spawn, $user, strlen("needreload"));
echo $input . "<br>";
}
while (true);
socket_close($spawn);
}
socket_close($socket);
?>
代码
My code worked fine, but when I changed and added socket_set_nonblock($socket)
socket_accept gave me error 11, here is my code
<?php
session_start();
require "checklogin.php";
error_reporting(0);
$user=$_SESSION["usr"];
set_time_limit(0);
ob_implicit_flush();
$host="127.0.0.1";
$port=11287;
$greeting="\nWelcome to my PHP server(listener)\nto quit type 'quit\nto shutdown the server type 'shutdown'.\n";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Err: " . socket_last_error());
socket_bind($socket, $host, $port) or die("Err: " . socket_last_error());
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {}
$result = socket_listen($socket) or die("can't set up listener");
socket_set_nonblock($socket) or die("rip multiple: " . socket_last_error());
while (true) {
if ($spawn = socket_accept($socket) or die("err: can't accept - " . socket_last_error())) {
socket_write($spawn, $greeting, strlen($greeting));
}
do {
$input = socket_read($spawn, 1024, PHP_NORMAL_READ) or die("Could not read input\n");
socket_write($spawn, $user, strlen("needreload"));
echo $input . "<br>";
}
while (true);
socket_close($spawn);
}
socket_close($socket);
?>
Any help appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
11是eagain / ewoldblock,这意味着如果套接字'''socket' t明确设置为非阻滞。这正是将插座设置为非障碍物时所期望的错误,并且必须通过以后再尝试,理想情况下,请在选择文件描述符已准备好进行此操作。如果您不希望这种错误,则不要将插座设置为非阻滞。
11 is EAGAIN / EWOULDBLOCK, meaning that the operation (
accept
) would block if the socket wasn't explicitly set to non-blocking. This is exactly the error which is expected when setting a socket to non-blocking and one has to deal with it by trying later again, ideally after checking with select that the file descriptor is ready for doing this operation. If you don't want this kind of error then don't set the socket to non-blocking.