簡體   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