gpt4 book ai didi

java - 从 SSLSocket 读取最快或最好的方法

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:57 26 4
gpt4 key购买 nike

我正在运行一个多线程的简约 http(s) 服务器(虽然不是 Web 服务器),它接受三个服务器套接字上的连接:本地、互联网和互联网-ssl。

每个套接字都有 1000 毫秒的 so 超时(将来可能会降低)。

工作线程读取这样的请求:

byte[] reqBuffer = new byte[512];
theSocket.getInputStream().read(reqBuffer);

现在的问题是,新实现的 ssl 套接字会出现 1/n-1 记录拆分技术的问题。还有一些客户端在使用 ssl(4/n-4 等)时以其他奇怪的方式拆分,所以我想我可能只是像这样执行多次读取:

byte[] reqBuffer = new byte[512];
InputStream is = theSocket.getInputStream();
int read = is.read(reqBuffer, 0, 128); // inital read - with x/n-x this is very small
int pos = 0;
if (read > 0) {
pos = read;
}
int i = 0;
do {
read = is.read(reqBuffer, pos, 128);
if (read > 0) {
pos += read;
}
i++;
} while(read == 128 && i < 3); // max. 3 more reads (4 total = 512 bytes) or until less than 128 bytes are read (request should be completely read)

它适用于 firefox 或 chrome 等浏览器以及使用该技术的其他客户端。

现在我的问题是新方法要慢得多。对本地套接字的请求非常慢,以至于超时 2 秒的脚本超时请求(我不知道为什么)。也许我的代码中存在一些逻辑问题?

有没有更好的方法来读取 SSL 套接字?因为每秒有数百甚至上千个请求,而新的读取方法甚至会减慢 http 请求。

注意:ssl-socket 目前没有使用,在我解决这个问题之前不会使用。

我还尝试使用缓冲读取器逐行读取,因为我们在这里讨论的是 http,但服务器突然用完了文件描述符(限制为 20 000)。不过,可能是因为我的实现。

我很感谢关于这个问题的每一个建议。如果您需要有关代码的更多信息,请告诉我,我会尽快发布。

编辑:实际上,我对我正在尝试做的事情进行了更多思考,我意识到这归结为读取 HTTP header 。因此,最好的解决方案是逐行读取请求行(或逐字符读取字符),并在 x 行后停止读取或直到到达空行(标记标题的末尾)。我当前的方法是将 BufferedInputStream 放在套接字的 InputStream 周围,并使用由 BufferedReader “读取”的 InputStreamReader 读取它(问题:当我使用 BufferedReader 时使用 BufferedInputStream 是否有意义?)。此 BufferedReader 读取字符的请求字符,检测行尾 (\r\n) 并继续读取,直到达到超过 64 个字符的行,最多读取 8 行或达到空行(标记 HTTP header 的结尾)。明天我将测试我的实现并相应地编辑此编辑。

编辑:我差点忘了在这里写下我的结果:有效。在每个 socket 上,甚至比以前的工作方式更快。感谢大家给我指出正确的方向。我最终是这样实现的:

List<String> requestLines = new ArrayList<String>(6);
InputStream is = this.cSocket.getInputStream();
bis = new BufferedInputStream(is, 1024);
InputStreamReader isr = new InputStreamReader(bis, Config.REQUEST_ENCODING);
BufferedReader br = new BufferedReader(isr);

/* read input character for character
* maximum line size is 768 characters
* maximum number of lines is 6
* lines are defined as char sequences ending with \r\n
* read lines are added to a list
* reading stops at the first empty line => HTTP header end
*/
int readChar; // the last read character
int characterCount = 0; // the character count in the line that is currently being read
int lineCount = 0; // the overall line count
char[] charBuffer = new char[768]; // create a character buffer with space for 768 characters (max line size)

// read as long as the stream is not closed / EOF, the character count in the current line is below 768 and the number of lines read is below 6
while((readChar = br.read()) != -1 && characterCount < 768 && lineCount < 6) {
charBuffer[characterCount] = (char) readChar; // fill the char buffer with the read character
if (readChar == '\n' && characterCount > 0 && charBuffer[characterCount-1] == '\r') { // if end of line is detected (\r\n)
if (characterCount == 1) { // if empty line
break; // stop reading after an empty line (HTTP header ended)
}
requestLines.add(new String(charBuffer, 0, characterCount-1)); // add the read line to the readLines list (and leave out the \r)
// charBuffer = new char[768]; // clear the buffer - not required
characterCount = 0; // reset character count for next line
lineCount++; // increase read line count
} else {
characterCount++; // if not end of line, increase read character count
}
}

最佳答案

这很可能更慢,因为您正在等待另一端发送更多数据,可能是它永远不会发送的数据。

更好的方法是给它一个更大的缓冲区,例如 32KB(128 很小),并且只读取一次可用的数据。如果此数据需要在某种消息中重新组合,则不应使用超时或固定数量的循环,因为 read() 只能保证至少返回一个字节。

关于java - 从 SSLSocket 读取最快或最好的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19715159/

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