gpt4 book ai didi

java - 用于服务器/客户端通信的自定义 readLine() 方法

转载 作者:行者123 更新时间:2023-12-01 10:38:28 24 4
gpt4 key购买 nike

我正在编写一个服务器/客户端应用程序,其中服务器将与许多不同的客户端进行通信。与每个客户端的通信是在运行服务器的计算机上的单独线程中进行的。到目前为止,我一直在使用 BufferedReader 类,以便使用 readLine() 方法从客户端套接字读取数据。 readLine() 遇到的问题是,当它找到新行字符时,它会停止读取。由于我的程序的性质,我想用 $^% 等字符序列替换新行限制,因此通过 readLine() 调用,BufferedReader 将继续读取它找到的 &^%。例如,如果客户端尝试发送一个 url 或文件路径,其中\n 可以作为自然路径的一部分找到,则 readLine() 方法将读取\n 并停止进一步读取。

我创建了以下类来尝试解决这个问题。但我现在创建了一个更大的。当我使用 BufferedReader 类和 readLine() 方法时,我的服务器可以为很多客户端提供服务,但是当我使用 CustomBufferedReader 和 readCustomLine() 时,服务器在 4 或 5 个线程开始运行后崩溃。我很确定与 readLine() 相比,我的类消耗了大量资源,但我不知道为什么或如何消耗。

如果您对此事有任何见解,我将不胜感激。

public class CustomBufferedReader extends BufferedReader {

public CustomBufferedReader(Reader reader) {
super(reader);
}


/**
* Keeps reading data from a socket and stores them into a String buffer until
* the combination of $^% is red.
*
* @return A String containing the buffer red without the $^% ending.
* @throws IOException
*/
public String readCustomLine() throws IOException {


//$^%
String buffer="";
try
{
if(super.ready())
{
//First I'm reading 3 bytes in order to have at least 3 bytes
//in the buffer to compare them later on.
try
{
buffer = buffer + (char)super.read();
buffer = buffer + (char)super.read();
buffer = buffer + (char)super.read();
}
catch (IOException e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
int i=0;
//This while well keep reading bytes and adding the to the buffer until it reads
//$^% as the terminating sequence of bytes.
while (!(buffer.charAt(i)=='$' && buffer.charAt(i+1)=='^' && buffer.charAt(i+2)=='%')){
try
{
buffer = buffer + (char)super.read();
i++;
}
catch (IOException e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
}

// Returns the saved buffer after subtracting the $^% ending.
return buffer.substring(0, buffer.length() - 3);
}
}
catch (IOException e)
{
//e.printStackTrace();
}
return buffer;
}


}

最佳答案

我认为这是实现您正在寻找的目标的更简单的方法:

String line = new Scanner(reader).useDelimiter("$^%").next();

关于为什么你的readCustomLine的实现不起作用,你可能遇到了并发问题。如果您查看 BufferedReader 的 readLine 实现,您可能会注意到它的所有代码都在 synchronized block 中运行。所以你可以在你的代码中尝试一下。

此外,如果 super.read() 抛出异常,您只需捕获它并继续执行,即使生成的缓冲区有错误,您也可以尝试删除内部的 try/catch block 。

最后,正如 EJP 所指出的,您应该删除 read() 调用并检查每个 super.read() 是否为 -1(表示 EOF)。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

public class CustomBufferedReader extends BufferedReader {

public CustomBufferedReader(final Reader reader) {
super(reader);
}

/**
* Keeps reading data from a socket and stores them into a String buffer
* until the combination of $^% is read.
*
* @return A String containing the buffer read without the $^% ending.
* @throws IOException
*/
public synchronized String readCustomLine() throws IOException {

// $^%
String buffer = "";

try {
// First I'm reading 3 bytes in order to have at least 3 bytes
// in the buffer to compare them later on.
buffer = buffer + safeRead();
buffer = buffer + safeRead();
buffer = buffer + safeRead();

int i = 0;
// This while will keep reading bytes and adding them to the
// buffer until it reads $^% as the terminating sequence of bytes.
while (!(buffer.charAt(i) == '$' && buffer.charAt(i + 1) == '^'
&& buffer.charAt(i + 2) == '%')) {
buffer = buffer + safeRead();
i++;
}

// Returns the saved buffer after subtracting the $^% ending.
return buffer.substring(0, buffer.length() - 3);
} catch (IOException e) {
/*
* Personally, I would remove this try/catch block and let the
* exception reach the caller
*/
e.printStackTrace();
System.out.println(e.getMessage());
}

return buffer;
}

private char safeRead() throws IOException {
int value = super.read();

if (value == -1) {
throw new EOFException();
}

return (char) value;
}
}

关于java - 用于服务器/客户端通信的自定义 readLine() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34552137/

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