gpt4 book ai didi

java - 由对等方重置连接

转载 作者:行者123 更新时间:2023-12-01 12:23:30 25 4
gpt4 key购买 nike

它是关于客户端 (PHP) 服务器 (Java) 环境。当 php 请求大约 40k 字符长的网页时,我在 Java 端(通过 out.flush())收到以下异常。我已经多次使用这种架构,没有出现任何异常。结果,显示了一个页面,但不完整。

java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at sitodove.core.net.Connection.writeMessage(Connection.java:79)
at sitodove.core.net.FrontendConnection.run(FrontendConnection.java:149)

下面是发送特定字符串作为对每个 PHP 请求的响应的 Java 代码

    public void writeMessage(String message) throws IOException
{
Logger.log("writeMessage, Valore in entrata: "+message);
int messageLength=message.length();
String startingNumber=""+messageLength;
StringBuffer prefix=new StringBuffer(""+messageLength);
for(int i=0;i<PROTOCOL_PREFIX_LENGTH-startingNumber.length();i++)
prefix.insert(0, "0");
prefix.append(message);
Logger.log("writeMessage, Valore in uscita: "+prefix);
out.write(prefix.toString());
out.flush();
Logger.log("DATI INVIATI");
}

这是发出请求并从 Java 服务器读取响应的 PHP 代码

$SEPARATOR='!!@!!';
function communicate($remoteServer, $remotePort, $outText)
{
$outTextLength = strlen($outText);
$lengthPaddedPrefix = str_pad($outTextLength, 8, "0", STR_PAD_LEFT);
$message = $lengthPaddedPrefix.$outText;

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket");
socket_connect($socket, $remoteServer, $remotePort) or header( 'Location: /maintenance/' );

socket_write($socket, $message, strlen($message)) or header( 'Location: /maintenance/' );
//echo 'COMUNICATO';
$response = socket_read($socket, 20000000) or header( 'Location: /maintenance/' );

socket_close($socket);

if (strpos($response, "JAVA is not responding") !== false) {
die("Site is down for maintenance");
}
return substr($response, 8);
}

$coreanswer=$coreanswer=communicate('localhost', 6527, "showreport".$SEPARATOR.$_GET['uid'].$SEPARATOR.$_GET['wid'].$SEPARATOR.$_GET['getime'].$SEPARATOR.$_GET['rct']);
echo $coreanswer;

最佳答案

“连接被对等方重置”意味着您尝试写入已关闭的套接字。 socket_read 函数不会一次性从套接字读取所有数据,并且您最终会在服务器完成所有写入操作之前关闭套接字。您需要一个循环来读取所有内容,并准备好处理由于客户端行为不正确而导致的服务器异常。

例如:

$data = "";
do {
$data = socket_read($socket, 20000000);
if ($data === FALSE) {
// socket read error
header( 'Location: /maintenance/' );
break;
}
$response .= $data;
} while ($data);

关于java - 由对等方重置连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26505854/

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