从套接字读取数据并将其附加到stringbuilder的问题



我遵循了一些教程来创建一个可以同时处理多个客户机的基本服务器。它正确地接收数据,但它会消耗越来越多的内存,直到耗尽堆空间。我相信这是因为我的循环不能识别我的结束字符并离开for循环。

下面是接收数据的线程的代码:
 while (true){
            try {
                is = new BufferedInputStream(s.getInputStream());
                InputStreamReader isr = new InputStreamReader(is);
                int character;
                StringBuilder process = new StringBuilder();
                try {
                    while((character = isr.read()) != 13) {
                        process.append((char)character); //here is the issue - it just continues to append until it runs out of memory
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                parent.parent.writeToLog("Client " + Integer.toString(num) + " sent a message: " + process.toString());
                System.out.println(process);
                is = null;
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            BufferedOutputStream os;
            try {
                os = new BufferedOutputStream(s.getOutputStream());
                OutputStreamWriter osw = new OutputStreamWriter(os);
                osw.write("Response from server at client socket " + Integer.toString(num) + " at " + (new java.util.Date()).toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

我在代码中添加了一个注释,它在堆栈跟踪中显示了错误。下面是客户端,它只是Java教程中的EchoClient:

  import java.io.*;
import java.net.*;
import java.util.Random;
public class EchoClient {
    public static void main(String[] args) throws IOException {
        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            echoSocket = new Socket("localhost", 2405);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                    + "the connection to: taranis.");
            System.exit(1);
        }
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        out.println("Message to host: hello" + (char) 13);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.println(Integer.toString((new Random()).nextInt()) + (char) 13);
        out.close();
        in.close();
        stdIn.close();
        echoSocket.close();
    }
}

您可以看到我将字符13附加到输出的地方,这意味着结束循环,但从我的理解来看,这不起作用。有人能帮我一下吗?

我认为问题出在下面的代码中:

                while((character = isr.read()) != 13) {
                    process.append((char)character); //here is the issue - it just continues to append until it runs out of memory
                }

即使到达流结束,isr.read()也会返回-1而不是13。有一种可能是,您永远不会收到为13的输入字符以退出循环。

这可能是问题所在:while((character = isr.read()) != 13)

你为什么要检查13?如果到达流的末尾,InputStreamReader.read()将返回-1。

我的猜测是你到达了流的末尾,用垃圾填充了StringBuilder。注意,(char)-1实际上代表unicode字符65535。

最新更新