簡體   English   中英

使用InputStream read()后流不接收數據

[英]Stream not receiving data after using InputStream read()

在開發簡單應用程序時遇到了與I / O相關的問題,我不確定為什么我的代碼無法正常工作。 我只處理了部分無效的代碼,並舉了一個簡單的示例。

服務器等級

public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket serversocket = new ServerSocket(56700);
        Socket socket = serversocket.accept();
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        while (true) {
            String line;
            if ((line = br.readLine()) != null) {

                System.out.println(line);

                switch (line) {
                    case "line":
                        System.out.println(br.readLine());
                        break;

                    case "file": {

                        try (FileOutputStream fos = new FileOutputStream(new File("file.txt"))) {

                            byte[] bytes = new byte[4096];

                            int count;
                            while ((count = is.read(bytes)) > 0) {
                                fos.write(bytes, 0, count);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
}

客戶類別

public class Client {

    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("localhost", 56700);
        PrintWriter pw = new PrintWriter(socket.getOutputStream());

        pw.write("line\n");
        pw.write("line test\n");
        pw.flush();

        try (FileInputStream fis = new FileInputStream(new File("test.txt")); OutputStream fos = socket.getOutputStream()) {

            pw.write("file\n");
            pw.flush();

            byte[] bytes = new byte[4096];
            int count;
            while ((count = fis.read(bytes)) > 0) {
                fos.write(bytes, 0, count);
            }

        } catch (IOException ex) {
            System.out.println("Error sending the file.");
        }

        pw.write("\n");
        pw.write("line\n");
        pw.write("line test 2\n");
        pw.flush();

    }
}

基本上,我有一個向服務器發送數據的客戶端。 客戶端使用PrintWriter write(String str)在感興趣的數據之前寫一行,以標識所發送信息的類型:一行文本或一個文件。 如您所見,服務器使用BufferedReader readLine()讀取文本行,並使用InputStream read(byte[] bytes)從文件InputStream read(byte[] bytes) 客戶端首先發送一行文本,然后發送文件中的數據以字節為單位,最后再次發送一行文本。

問題是服務器成功發送和接收文件后,它不會接收到文本行。 我檢查並意識到BufferedReader永遠不會收到這一行文本。 如您所見,在寫完文件數據之后,我寫了一個\\n ,以防萬一這是問題所在,但沒有用。 我可能缺少與這些流的功能相關的內容。

任何想法? 提前致謝。

此代碼中的問題:

try (FileInputStream fis = new FileInputStream(new File("test.txt")); OutputStream fos = socket.getOutputStream()) {

    pw.write("file\n");
    pw.flush();

    byte[] bytes = new byte[4096];
    int count;
    while ((count = fis.read(bytes)) > 0) {
        fos.write(bytes, 0, count);
    }

} catch (IOException ex) {
    System.out.println("Error sending the file.");
}

將try-finally與OutputStream fos = socket.getOutputStream() ,從try-finally代碼塊退出后,您可以對socket.getOutputStream()編程,因為try-finally始終在隱式finally塊中調用close方法。 您的PrintWriter在此處使用相同的SocketOutputStream

pw.write("\n");
pw.write("line\n");
pw.write("line test 2\n");
pw.flush();

但是此流已關閉。

嘗試這個:

OutputStream fos = socket.getOutputStream()
try (FileInputStream fis = new FileInputStream(new File("test.txt"))) {

    pw.write("file\n");
    pw.flush();

    byte[] bytes = new byte[4096];
    int count;
    while ((count = fis.read(bytes)) > 0) {
        fos.write(bytes, 0, count);
    }

} catch (IOException ex) {
    System.out.println("Error sending the file.");
}

pw.write("\n");
pw.write("line\n");
pw.write("line test 2\n");
pw.flush();
pw.close(); //closing PrintWriter and internal SocketOutputStream

如果只使用以下一種,那就更好了:PrintWriter或OutputStream

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM