2011-10-11 91 views
1

我想創建一個簡單的服務器客戶端應用程序,但我認爲IO Streams存在問題。沒有GUI,因此聊天應該通過控制檯進行(您可以打開2 eclipse來測試它或您使用的任何IDE)。Java簡單服務器/客戶端控制檯聊天應用程序

這裏是我的服務器代碼:

import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.Scanner; 

public class Server { 
    ServerSocket serverSocket; 
    Socket connection; // connection-to-client 
    ObjectOutputStream output; 
    ObjectInputStream input; 

    public void run() { 
     try { 
      serverSocket = new ServerSocket(6000, 100); 
     } catch (IOException e) { 
      System.err.println("Invalid port number"); 
     } 
     while (true) { 
      try { 
       waitForConnection(); 
       getIOStreams(); 
       processConnection(); 
      } finally { 
       closeConnection(); 
      } 
     } 
    } 

    public void closeConnection() { 
     try { 
      input.close(); 
      output.close(); 
      connection.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void waitForConnection() { 
     System.out.println("Server is ready to accept conenctions"); 
     try { 
      connection = serverSocket.accept(); // code will stop here until a 
               // connection occurs 
      System.out.println("Conenction established with the client"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void getIOStreams() { 
     try { 
      output = new ObjectOutputStream(connection.getOutputStream()); 
      output.flush(); // send header information to the client, which 
          // contains info required to create the input stream 
          // object 
      input = new ObjectInputStream(connection.getInputStream()); 
      System.out.println("Server established I/O streams"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void processConnection() { 
     sendData("Connection established with the server"); 
     String inputMessage = ""; 
     new Runnable() { 
      Scanner sc = new Scanner(System.in); 

      public void run() { 
       while (true) { 
        sendData(sc.nextLine()); 
       } 
      } 
     }; 
     do { 

      try { 
       inputMessage = (String) input.readObject(); 
       System.out.println(inputMessage); 
      } catch (ClassNotFoundException e) { 
       System.err.println("Object of an unknown type was recieved"); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } while (inputMessage.equals("QUIT")); 
    } 

    public void sendData(String s) { 
     try { 
      output.writeObject(s); 
      output.flush(); 
      System.out.println("Server: " + s); 
     } catch (IOException e) { 
      System.err.println("Error writting the message"); 
     } 
    } 
} 

,這是我的客戶端代碼

import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.Scanner; 

public class Client { 
    ServerSocket serverSocket; 
    Socket clientSocket; 
    ObjectOutputStream output; 
    ObjectInputStream input; 
    String serverAddress = "127.0.0.1"; 

    public void run() { 
     connect2Server(); 
     getIOStreams(); 
     processConnection(); 
     closeConnection(); 
    } 

    public void connect2Server() { 
     System.out.println("Trying to connect to the server"); 
     try { 
      clientSocket = new Socket(serverAddress, 6000); 
     } catch (UnknownHostException e) { 
      System.err.println("Server unavailable"); 
      System.exit(1); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public void getIOStreams() { 
     try { 
      output = new ObjectOutputStream(clientSocket.getOutputStream()); 
      output.flush(); 
      input = new ObjectInputStream(clientSocket.getInputStream()); 
      System.out.println("Client established I/O Stream"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void processConnection() { 
     sendData("Connection established with the client"); 
     String inputMessage = ""; 
     new Runnable() { 
      Scanner sc = new Scanner(System.in); 

      public void run() { 
       String outputMessage = ""; 
       do { 
        outputMessage = sc.nextLine(); 
        sendData("Client: " + outputMessage); 
       } while (outputMessage.equals("QUIT")); 
      } 
     }; 
     while (true) { 

      try { 
       inputMessage = (String) input.readObject(); 
       System.out.println(inputMessage); 
      } catch (ClassNotFoundException e) { 
       System.err.println("Object of an unknown type was recieved"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       System.exit(1); 
      } 
     } 
    } 

    public void sendData(String s) { 
     try { 
      output.writeObject(s); 
      output.flush(); 
      System.out.println("Client: " + s); 
     } catch (IOException e) { 
      System.err.println("Error writting the message"); 
     } 
    } 

    public void closeConnection() { 
     try { 
      output.close(); 
      input.close(); 
      clientSocket.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

運行服務器或客戶機只在主客戶端/ server.run寫(); 請告訴我什麼是我的錯誤,以及如何解決它:)

+0

我懶得去通過你的代碼...請指出你得到什麼錯誤或你期望得到什麼 – PeterMmm

+0

inputMessage =(String)input.readObject();在這一行發生異常(這是從客戶端類)...我期望的是一個掃描儀讀取我在控制檯寫什麼,它將顯示在服務器的控制檯上,反之亦然 – blenddd

回答

3

幾點:

1)輸入迴路的結束不正確的條件:

while (inputMessage.equals("QUIT")); // Server#processConnection 

while (outputMessage.equals("QUIT")) // Client#processConnection 

這些應該被否定(「! 「)。

2)你應該開始你System.in閱讀線程:

new Thread() { // instead of `Runnable` 
    ... 
}.start(); 

3),你應該打破服務器偵聽環的一些例外,比如EOFException這意味着客戶端已斷開。

+0

是啊,你的權利點1,2:D thx,至於你的第三點我相信我試圖捕捉EOFException但它給了我一些錯誤,毫米,就好像它永遠不會被拋出,所以我不能抓住它..如果你得到什麼我的意思是。 – blenddd

+0

我這樣做,這可能是因爲你試圖在catch(IOException e)後面添加'catch(EOFException e)'子句 - 後者覆蓋前者(因爲'EOFException'擴展了'IOException')。更具體的例外應該放在更具體的例外之後。 – yozh

+0

是啊,試過它,現在它的工作原理,但據我所知,如果EOFException擴展IOException,那麼如果發現IOException異常,則不應引發EOFException;但是,它被拋出:/ – blenddd

相關問題