亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復 Broken Pipe Socket 異常 (Java)?連接在哪里被關閉?

如何修復 Broken Pipe Socket 異常 (Java)?連接在哪里被關閉?

胡子哥哥 2024-01-05 16:27:09
我正在為一個學校項目開發一個多線程網絡服務器。我應該能夠在瀏覽器上進入本地主機并請求 3 個不同的文件(.htm、.jpeg、.pdf)。但是,當我對其中包含圖片的 .htm 文件執行此操作(2 個請求)時,.htm 文件會出現在瀏覽器中,但對于我嘗試在圖片上執行的每次寫入,都會出現許多損壞的管道套接字異常(分配需要一次寫入 1024 個字節)。我實現此方法的方式明顯有問題,但當我嘗試寫入第二個文件時,我不知道連接在哪里關閉?我嘗試了一些不同的方法來嘗試解決此問題,包括嘗試讀取套接字輸入流時的循環,但我認為這違背了多線程服務器的目的。服務器:    while(true){        try {            sock = servSock.accept(); // Handles the connection            // Connection received log            System.out.println("Connection received: " + new Date().toString() + " at " + sock.getInetAddress() + sock.getPort());            HTTP pro = new HTTP(sock); // Client handler            pro.run();            ServerThread serverThread = new ServerThread(pro);             // Starts ServerThread            serverThread.start();        } catch (Exception e){            System.out.println(e);        }    }HTTP:    public void run(){        // Try to open reader        try{            readSock = new BufferedReader(new InputStreamReader(sock.getInputStream()));        } catch (Exception e){            System.out.println(e);        }        // Open output stream        try{            this.out = new DataOutputStream(sock.getOutputStream());             this.printOut = new PrintWriter(sock.getOutputStream());         } catch (Exception e){            System.out.println(e);        }        // Try to read incoming line        try {            this.reqMes = readSock.readLine();        } catch (IOException e) {            e.printStackTrace();        }        StringTokenizer st = new StringTokenizer(reqMes);        // Parse the request message        int count = 0;        while(st.hasMoreTokens()){            String str = st.nextToken();            if (count == 1){                this.fileName = "." + str;            }            count += 1;        }對于瀏覽器中的一個 .htm 文件,該文件和 html 看起來都很好。但看起來它在 html 文件中對 .jpeg 文件發出了第二個請求,并且瀏覽器在每次寫入數據時都會卡住加載 java.net.SocketException: Broken pipeline (Write failed)this.out.write(fileData,0,1024);謝謝,如有任何幫助,我們將不勝感激。
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

問題在于響應標頭的格式不正確,導致連接過早結束。必須在標頭之后發送另一個空行(“\r\n”)。

以下代碼現在可以運行(this.CRLF 等于“\r\n”):

? ?public void run(){

? ? ? ? // Try to open reader

? ? ? ? try{

? ? ? ? ? ? readSock = new BufferedReader(new InputStreamReader(sock.getInputStream()));

? ? ? ? } catch (Exception e){

? ? ? ? ? ? System.out.println(e);

? ? ? ? }


? ? ? ? // Open output stream

? ? ? ? try{

? ? ? ? ? ? this.out = new DataOutputStream(sock.getOutputStream()); // Data output

? ? ? ? ? ? this.printOut = new PrintWriter(sock.getOutputStream()); // Print output

? ? ? ? } catch (Exception e){

? ? ? ? ? ? System.out.println(e);

? ? ? ? }


? ? ? ? // Try to read incoming line

? ? ? ? try {

? ? ? ? ? ? this.reqMes = readSock.readLine();

? ? ? ? } catch (IOException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }


? ? ? ? StringTokenizer st = new StringTokenizer(reqMes);


? ? ? ? // Parse the request message

? ? ? ? int count = 0;

? ? ? ? while(st.hasMoreTokens()){

? ? ? ? ? ? String str = st.nextToken();

? ? ? ? ? ? if (count == 1){

? ? ? ? ? ? ? ? this.fileName = "." + str;

? ? ? ? ? ? }

? ? ? ? ? ? count += 1;

? ? ? ? }

? ? ? ? System.out.println("File name received.");


? ? ? ? // Initialize file to be sent

? ? ? ? File file = null;

? ? ? ? // Try to find file and create input stream

? ? ? ? try {

? ? ? ? ? ? file = new File(this.fileName);

? ? ? ? ? ? this.f = new FileInputStream(file); // File input stream

? ? ? ? ? ? this.fileExists = true;

? ? ? ? ? ? System.out.println("File " + this.fileName +? " exists.");

? ? ? ? } catch (FileNotFoundException e) {

? ? ? ? ? ? System.out.println(e);

? ? ? ? ? ? this.fileExists = false;

? ? ? ? ? ? System.out.println("File does not exist.");

? ? ? ? }


? ? ? ? byte[] buffer = new byte[1024];

? ? ? ? // Write status line

? ? ? ? if (this.fileExists) {

? ? ? ? ? ? System.out.println("Trying to write data");

? ? ? ? ? ? try{

? ? ? ? ? ? ? ? this.out.writeBytes("HTTP/1.0 " + "200 OK " + this.CRLF);

? ? ? ? ? ? ? ? this.out.flush();

? ? ? ? ? ? ? ? // Write Header

? ? ? ? ? ? ? ? this.out.writeBytes("Content-type: " + getMime(this.fileName) + this.CRLF);

? ? ? ? ? ? ? ? this.out.flush();

? ? ? ? ? ? ? ? this.out.writeBytes(this.CRLF);

? ? ? ? ? ? ? ? this.out.flush();


? ? ? ? ? ? ? ? // Read file data

? ? ? ? ? ? ? ? byte[] fileData = new byte[1024];


? ? ? ? ? ? ? ? int i;

? ? ? ? ? ? ? ? while ((i = this.f.read(fileData)) > 0) {

? ? ? ? ? ? ? ? ? ? // Write File data

? ? ? ? ? ? ? ? ? ? try{

? ? ? ? ? ? ? ? ? ? ? ? this.out.write(fileData,0, i);

? ? ? ? ? ? ? ? ? ? } catch (IOException e) {

? ? ? ? ? ? ? ? ? ? ? ? System.out.println(e);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? this.out.flush(); // Flush output stream

? ? ? ? ? ? ? ? System.out.println("Flushed");

? ? ? ? ? ? ? ? closeSock(); // Closes socket

? ? ? ? ? ? } catch (IOException e) {

? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? }


查看完整回答
反對 回復 2024-01-05
  • 1 回答
  • 0 關注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號