2010-07-02 67 views
1

我想做一個簡單的客戶端服務器應用程序,但是當我執行代碼時,我得到一個異常,客戶端無法獲取套接字的輸入流。請看看我的代碼,嘗試幫助。謝謝:)不能從服務器「獲得」流

PS:抱歉亂碼。執行它作爲兩個不同的應用程序。

import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.net.InetAddress; 
import java.net.Socket; 


public class SimpleClient //implements Runnable 
{ 
    private ObjectOutputStream output; // output stream to server 
    private ObjectInputStream input; // input stream from server 
    private String message = ""; // message from server 
    private String chatServer="localhost"; // host server for this application 
    private Socket client; // socket to communicate with server 

    public void runClient() 
    { 
     try // connect to server, get streams, process connection 
     { 
     connectToServer(); // create a Socket to make connection 
     getStreams(); // get the input and output streams 
     processConnection(); // process connection 
     } // end try 
     catch(IOException ioe) 
     { 
      ioe.printStackTrace(); 
     } 
     finally 
     { 
     // closeConnection(); // close connection 
     } // end finally 
    } 

    private void closeConnection() 
    { 
     try 
     { 
     output.close(); // close output stream 
     input.close(); // close input stream 
     client.close(); // close socket 
     } // end try 
     catch (IOException ioException) 
     { 
     ioException.printStackTrace(); 
     } // end catch 
    } // end method closeConnection 


    private void connectToServer() throws IOException 
    {  
     System.out.println("Attempting connection\n"); 

     // create Socket to make connection to server 
     client = new Socket(InetAddress.getByName(chatServer), 12345); 

     // display connection information 
     System.out.println("Connected to: " + 
     client.getInetAddress().getHostName()); 
    } // end method connectToServer 



    private void getStreams() throws IOException 
    { 
     // set up output stream for objects 
     output = new ObjectOutputStream(client.getOutputStream());   
     output.flush(); // flush output buffer to send header information 

     // set up input stream for objects 
     input = new ObjectInputStream(client.getInputStream());  
     System.out.println("\nGot I/O streams\n"); 
    } // end method getStreams 

    private void processConnection() throws IOException 
    { 

    do { 
     try // read message and display it 
     { 
      message = (String) input.readObject(); // read new message 
      System.out.println("\n" + message); // display message 
     } // end try 
     catch (ClassNotFoundException classNotFoundException) 
     { 
      System.out.println("\nUnknown object type received"); 
     } // end catch 

     } while (!message.equals("SERVER>>> TERMINATE")); 
    } // end method processConnection 

    public String toString() 
{ 
    return "client connected to "+chatServer; 
} 



public static void main(String []args) 
{ 
    SimpleClient c= new SimpleClient(); 
    c.runClient(); 

} 



    } 

// -------------服務器從這裏開始

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


import java.util.ArrayList; 

public class SimpleServer //implements Runnable 
{ 
    private ObjectOutputStream output; // output stream to client 
    private ObjectInputStream input; // input stream from client 
    private ServerSocket server; // server socket 
    private Socket connection; // connection to client 
    private int counter=1; //counts connections 
    ArrayList<SimpleClient> list; 

/*** 
    * Runs forever and takes care of alla 
    */ 
    public void runServer() 
    { 
     try // set up server to receive connections; process connections 
     { 
     server = new ServerSocket(12345, 100); // create ServerSocket 

     while (true) 
     { 
      try 
      { 
       waitForConnection(); // wait for a connection 
       getStreams(); // get input & output streams 
       processConnection(); // process connection 
      } // end try 
      catch (Exception Exception) 
      { 
       System.out.println("\nSomething bad happened"); 
      } // end catch 
      finally 
      { 
       closeConnection(); 
       counter++; // close connection 

      } // end finally 
    } 
     } 

     catch (IOException ioException) 
     { 
     System.out.println("An io error occured while creating a server-socket"); 
     ioException.printStackTrace(); 
     } // end catch 
    } // end method runServer 

    /*** 
    * Waits for a client's request for connection 
    */ 
    public void waitForConnection() 
    { 
     try 
     { 
      connection = server.accept(); 
      System.out.println("Client with IP and hostname "+connection.getInetAddress().getHostName()); 
     } catch (IOException ex) 
     { 
      System.out.println("An error makes connection impossible"); 
      ex.printStackTrace(); 
     } 

    } 


    /*** 
    * Makes the interconnection of client's and server's stream's 
    */ 
    public void getStreams() 
    { 
     try 
     { 

     output=(ObjectOutputStream) connection.getOutputStream(); 
     input=(ObjectInputStream) connection.getInputStream(); 
     output.flush(); 
     System.out.println("Got IO Streams, hell yeah "); 

     } 

     catch(IOException ioe) 
     { 
      System.out.println("Cannot get streams"); 
      ioe.printStackTrace(); 
     } 

    } 

    /*** 
    * Terminates connection 
    */ 
    public void closeConnection() 
    { 
     try 
     { 
      output.flush(); 
      output.close(); 
      input.close(); 
     } catch (IOException ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 

    /*** 
    * Receives messages from client 
    */ 
    public void processConnection() 
    { 
     String message="Connection successful"; 

     do // process messages sent from client 
     { 
     try // read message and display it 
     { 
      message = (String) input.readObject(); // read new message 
      System.out.println("CLIENT>>> "+message); 

     } // end try 
     catch (ClassNotFoundException classNotFoundException) 
     { 
      System.out.println("\nUnknown object type received"); 
     } 

     catch(IOException ioe) 
     { 
      System.out.println("Cannot receive message from clint"); 
      ioe.printStackTrace(); 
     } 

     } while (!message.equals("CLIENT>>> TERMINATE")); 


    } 

    /*** 
    * Sends data to client 
    * @param message 
    */ 
    public void sendData(String message) 
    { 

     try // send object to client 
     { 

     output.writeObject("SERVER>>> " + message); 
     output.flush(); // flush output to client 
     System.out.println("SERVER>>> " + message); 
     } 
     catch(IOException ioe) 
     { 
      ioe.printStackTrace(); 
     } 
    } 


    public String toString() 
    { 
     return "this is a simple server with a total of "+counter+" connections"; 
    } 



    public static void main(String []args) 
{ 
    SimpleServer c= new SimpleServer(); 
    c.runServer(); 

} 


} 
+0

而不是自己寫這個,你應該真的谷歌工作代碼,然後修改。 – 2010-07-02 19:47:06

+0

我做到了,但沒有複製。我試圖看到它是如何工作的,並從頭開始重寫:) – 2010-07-03 08:22:19

回答

2

您的服務器只是把由Socket.getOutputStream()和Socket.getInputStream返回的對象()來對象流。相反,它必須使用「new ObjectInputStream()」和「new ObjectOutputStream()」以與客戶端相同的方式構造對象流。請記住刷新服務器的ObjectOutputStream。

+0

就像我在客戶端做的那樣? 「output = new ObjectOutputStream(client.getOutputStream());」 – 2010-07-03 08:23:54