2013-08-03 90 views
-1

我正在製作一個簡單的Web服務器,每次客戶端連接時都會產生一個新線程。然後我需要使用BufferedOutputStream發送響應(因爲HTTP協議在行尾需要一個/ r/n)。 這裏是我的代碼:使用傳遞給線程的套接字時,「套接字已關閉」異常?

class HttpServer{ 
    public static void main(String args[]){ 
     ServerSocket ss = null; 
     int port = 40000; 

     try{ 
      if(args.length > 0){ 
       port = Integer.parseInt(args[0]); 
      } 

      ss = new ServerSocket(port); 

      while(true){ 
       Socket client = ss.accept(); 
       User user = new User(client); 
       user.start(); 
      } 
     } 
     catch(Exception e){ 
       System.err.println(e.getMessage()); 
     } 
    } 
} 

我的用戶等級:

class User extends Thread{ 

     Socket client; 

     public User(Socket s){ 
      client = s; 
     } 

     public void run(){ 
      try{ 
       BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream()); 

       println(bos, "Hello World"); 
       bos.close(); 

       client.close(); 
      } 
      catch(Exception e){ 
        System.err.println(e.getMessage()); 
      } 
     } 

     private void println(BufferedOutputStream bos, String s) throws IOException { 
      String news = s + "\r\n"; 
      byte[] array = news.getBytes(); 
      for(int i=0; i<array.length; i++){ 
       bos.write(array[i]); 
      } 
      return; 
     } 
    } 

當我嘗試創建的BufferedOutputStream出現的問題;它給了我一個「套接字已關閉」的錯誤,我不知道爲什麼。

任何幫助將不勝感激。

謝謝!

這是不起作用的代碼:

class User extends Thread{ 

    Socket client; 

    public User(Socket s){ 
     client = s; 
    } 

    public void run(){ 
     try{ 
      System.out.println("Address: " + client.getInetAddress().toString()); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream())); 

      String line; 
      Boolean first = true; 
      while(!((line = reader.readLine()).equals(""))){ 
       if(first) 
        System.out.println(line.split(" ")[1]); 
       first = false; 
      } 
      reader.close(); 

      BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream()); 
      println(bos, "Hello World"); 
      bos.close(); 



      // Close socket 
      client.close(); 
     } 
     catch(Exception e){ 
       System.err.println(e.getMessage()); 
     } 
    } 

    private void println(BufferedOutputStream bos, String s) throws IOException { 
     String news = s + "\r\n"; 
     byte[] array = news.getBytes(); 
     for(int i=0; i<array.length; i++){ 
      bos.write(array[i]); 
     } 
     return; 
    } 
} 
+1

很奇怪,我不知道發生了什麼 – tbodt

+0

客戶端如何調用您的服務器?也許客戶在從服務器讀取任何輸出之前關閉連接? – tangens

+0

我一直在瀏覽器中輸入localhost:40000進行測試,這就是結果。我也嘗試使用本地地址,例如192.168.0.100:40000具有相同的結果。 – Neurion

回答

0

到目前爲止,我所知道的,有你的代碼沒有問題。操作系統或瀏覽器似乎更有可能以某種方式限制對服務器的訪問。

-1
Socket client = ss.accept(); 
User user = new User(client); 
user.start(); 

accept呼叫等待連接的塊。您的用戶線程永遠不會被創建。您應該創建一個新的User類,它爲您創建一個新的線程並將您的消息寫入服務器。

public class User extends Thread{ 

      Socket client; 
      int port; 
      public User(int p) throws UnknownHostException, IOException{ 
       port = p; 
       client = new Socket(InetAddress.getLocalHost(), 40000); 
      } 

      public void run(){ 
       try{ 
        BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream()); 

        println(bos, "Hello World"); 
        bos.close(); 

        client.close(); 
       } 
       catch(Exception e){ 
         System.err.println(e.getMessage()); 
       } 
      } 

      private void println(BufferedOutputStream bos, String s) throws IOException { 
       String news = s + "\r\n"; 
       byte[] array = news.getBytes(); 
       for(int i=0; i<array.length; i++){ 
        bos.write(array[i]); 
       } 

       return; 
      } 

      public static void main(String[] args) throws UnknownHostException, IOException { 
       User user = new User(4000); 
       user.start(); 
      } 
     } 

服務器應該只能聽傳入連接。(也許打印)

import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.Scanner; 


class HttpServer{ 
    public static void main(String args[]){ 
     ServerSocket ss = null; 
     int port = 40000; 

     try{ 
      if(args.length > 0){ 
       port = Integer.parseInt(args[0]); 
      } 

      ss = new ServerSocket(port); 

      while(true){ 
       Socket client = ss.accept(); 
       Scanner scanner = new Scanner(client.getInputStream()); 
       while (scanner.hasNext()) { 
        System.out.println(scanner.nextLine()); 
       } 
      } 
     } 
     catch(Exception e){ 
       System.err.println(e.getMessage()); 
     } 
    } 
} 

運行兩者不同的Java程序,你應該在服務器端看到的hello world。 如果你改變你的端口,你可能不得不在客戶端改變它。

+0

User *是*接受的連接,而不是服務器內部的新客戶端連接。你根本沒有理解代碼。 -1 – EJP

2

這個意外意味着關閉了Socket。

關閉輸入流或套接字的輸出流會關閉另外兩個關閉任何FilterInput/OutputStream/Reader/Writer關閉它正在過濾的流/讀取器/寫入器。