gpt4 book ai didi

PHP 警告 : socket_read(): unable to read from socket [104]: Connection reset by peer

转载 作者:行者123 更新时间:2023-12-03 12:05:08 40 4
gpt4 key购买 nike

我用 socket_create()创建套接字资源,然后绑定(bind) IP通过 socket_bind() 向它发送地址,它的工作正常;

但过了一会儿(超过 30 分钟)在行 socket_read($sock, 2048)抛出此错误:
"PHP Warning: socket_read(): unable to read from socket [104]: Connection reset by peer in test.php on line 198" .

这是我的简化代码:

$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// check if tcp socket ceated or not
if ($this->sock === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg");
}

// Bind the source address
socket_bind($this->sock, $this->ip);
// Connect to destination address
socket_connect($this->sock, $this->mxHost, $this->port);
$buf = socket_read($this->sock, 2048);

这段代码与另一端的 MX 主机建立了 SMTP(端口 25)连接。
也许这是您连接另一端的故障,但是我如何检测到另一端现在还没有准备好连接。换句话说,我怎样才能发现“对等连接重置”发生了?

最佳答案

您应该检查 socket_connect()在读取它之前是成功的。

所以你可以像这样重写你的代码:

-- 更新 --

$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Bind the source address
socket_bind($this->sock, $this->ip);
// Connect to destination address
if (socket_connect($this->sock, $this->mxHost, $this->port)) {
// suppress the warning for now since we have error checking below
$buf = @socket_read($this->sock, 2048);

// socket_read() returns a zero length string ("") when there is no more data to read.
// This indicates that the socket is closed on the other side.
if ($buf === '')
{
throw new \Exception('Connection reset by peer');
}
} else {
// Connection was not successful. Get the last error and throw an exception
$errorMessage = socket_strerror(socket_last_error());
throw new \Exception($errorMessage);
}

关于PHP 警告 : socket_read(): unable to read from socket [104]: Connection reset by peer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26613096/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com