当我添加socket_set_nonblock时,插座接受给出错误

发布于 2025-02-05 03:30:44 字数 1193 浏览 3 评论 0原文

我的代码工作正常,但是当我更改并添加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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光倒影 2025-02-12 03:30:45

...但是当我更改并添加socket_set_nonblock($ socket)socket_accept给我错误11,...

11是eagain / ewoldblock,这意味着如果套接字'''socket' t明确设置为非阻滞。这正是将插座设置为非障碍物时所期望的错误,并且必须通过以后再尝试,理想情况下,请在选择文件描述符已准备好进行此操作。如果您不希望这种错误,则不要将插座设置为非阻滞。

... but when I changed and added socket_set_nonblock($socket) socket_accept gave me error 11, ...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文