第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

胡子哥哥 2024-01-05 16:27:09
我正在為一個(gè)學(xué)校項(xiàng)目開(kāi)發(fā)一個(gè)多線(xiàn)程網(wǎng)絡(luò)服務(wù)器。我應(yīng)該能夠在瀏覽器上進(jìn)入本地主機(jī)并請(qǐng)求 3 個(gè)不同的文件(.htm、.jpeg、.pdf)。但是,當(dāng)我對(duì)其中包含圖片的 .htm 文件執(zhí)行此操作(2 個(gè)請(qǐng)求)時(shí),.htm 文件會(huì)出現(xiàn)在瀏覽器中,但對(duì)于我嘗試在圖片上執(zhí)行的每次寫(xiě)入,都會(huì)出現(xiàn)許多損壞的管道套接字異常(分配需要一次寫(xiě)入 1024 個(gè)字節(jié))。我實(shí)現(xiàn)此方法的方式明顯有問(wèn)題,但當(dāng)我嘗試寫(xiě)入第二個(gè)文件時(shí),我不知道連接在哪里關(guān)閉?我嘗試了一些不同的方法來(lái)嘗試解決此問(wèn)題,包括嘗試讀取套接字輸入流時(shí)的循環(huán),但我認(rèn)為這違背了多線(xiàn)程服務(wù)器的目的。服務(wù)器:    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;        }對(duì)于瀏覽器中的一個(gè) .htm 文件,該文件和 html 看起來(lái)都很好。但看起來(lái)它在 html 文件中對(duì) .jpeg 文件發(fā)出了第二個(gè)請(qǐng)求,并且瀏覽器在每次寫(xiě)入數(shù)據(jù)時(shí)都會(huì)卡住加載 java.net.SocketException: Broken pipeline (Write failed)this.out.write(fileData,0,1024);謝謝,如有任何幫助,我們將不勝感激。
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊

問(wèn)題在于響應(yīng)標(biāo)頭的格式不正確,導(dǎo)致連接過(guò)早結(jié)束。必須在標(biāo)頭之后發(fā)送另一個(gè)空行(“\r\n”)。

以下代碼現(xiàn)在可以運(yù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();

? ? ? ? ? ? }


查看完整回答
反對(duì) 回復(fù) 2024-01-05
  • 1 回答
  • 0 關(guān)注
  • 190 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)