2016-04-19 194 views
0

我正在創建一個「回顯」服務器,它在收到消息後將其簡單地發送回去。我設法讓多客戶端工作,但我想做出某種斷開連接檢測。我嘗試通過從服務器發送單個字符,然後用客戶端中的另一個字符來回復它。不過,我無法得到這個工作。檢測插座中的斷開連接

你會如何建議我去斷開連接檢測?

MessageServer.java

import java.net.*; 
import java.io.*; 

public class MessageServer { 
    static int clientCount = 0; 
    public static void main(String[] args) throws IOException { 
     try(ServerSocket servSocket = new ServerSocket(16384)){ 
      while(true){ 
       Socket socket = servSocket.accept(); 
       addClient(); 
       new ServerThread(socket, clientCount).start(); 

      } 

     } catch(IOException e) { 
      System.out.println("Exception caught when trying to listen on port 16384 or listening for a connection"); 
      System.out.println(e.getMessage()); 
     } 

    } 

    public static void addClient(){ 
     clientCount++; 
    } 
} 

ServerThread.java

import java.net.*; 
import java.io.*; 

public class ServerThread extends Thread { 
    private Socket cltSocket; 
    private BufferedReader in; 
    private PrintWriter out; 
    private int num; 
    public ServerThread(Socket clientSocket, int count) { 
     cltSocket = clientSocket; 
     num = count; 
    } 
    public void run() { 
     String input; 

     try { 
      out = new PrintWriter(cltSocket.getOutputStream(), true); 
      in = new BufferedReader(new InputStreamReader(cltSocket.getInputStream())); 
      System.out.println("Client " + num + " connected!"); 

      while(cltSocket.isConnected() && !cltSocket.isClosed()){ 
       if(in.ready()){ 
        input = in.readLine(); 
        if(input != null && !(input.equalsIgnoreCase("exit"))){ 
         System.out.print("New input: "); 
         System.out.println(input); 
         out.println(input); 
         out.flush(); 
        } else if(input.equalsIgnoreCase("exit")){ 
         disconnect(); 
        } 
       } 
      } 
     } catch(SocketException e) { 
      disconnect(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return; 
     } 
    } 

    public void disconnect(){ 
     System.out.println("Client " + num + " disconnected!"); 
     out.close(); 
     try { 
      in.close(); 
      cltSocket.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

MessageClient.java

import java.net.*; 
import java.io.*; 

public class MessageClient { 

    public static void main(String[] args) { 

     if(args.length != 2) { 
      System.out.println("Invalid parameters! Format as: (hostname) (port)"); 
      System.exit(1); 
     } 

     String hostname = args[0]; 
     int port = Integer.parseInt(args[1]); 

     try { 
      Socket socket = new Socket(hostname, port); 
      PrintWriter out = new PrintWriter(socket.getOutputStream(), true); 
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      BufferedReader con = new BufferedReader(new InputStreamReader(System.in)); 
      System.out.println("Connected!"); 

      while(socket.isConnected() && !socket.isClosed()){ 
       String output; 
       if(con.ready()) { 
        output = con.readLine(); 
        out.println(output); 
        if(output.equalsIgnoreCase("exit")) { 
         socket.close(); 
        } 
       } 

       if(in.ready()){ 
        String li = in.readLine(); 
        if(li != null) { 
         System.out.println(li); 
        } 
       } 
      } 


      System.out.println("Disconnected!"); 
      con.close(); 
      out.close(); 
      in.close(); 
      System.exit(0); 
     } catch(SocketException e) { 
      System.err.println("Socket error:" + e); 
     } catch(UnknownHostException e) { 
      System.err.println("Invalid host"); 
     } catch(IOException e) { 
      System.err.println("IO Error: " + e); 
     } 

    } 

} 
+3

如果你讀的InputStream和對方插座走的方式,然後你會得到一個例外......這是一種檢查丟失連接的方法 –

+0

哇。我刪除了'if(in.ready())'檢查,並且它完美地工作。非常感謝! – user3433131

回答

1

有一種方法可以做到這一點:

如果你看過的BufferedReader通過調用BufferedReader.getLine()和對方插座走了,那麼你會得到一個SocketException ...這是檢查連接丟失