2011-10-11 130 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

相关问题