繁体   English   中英

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

[英]Issue with reading data from a socket and appending it to a 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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM