2015-09-02 231 views
0

我想檢查我的客戶是否有任何斷開連接。從我的研究,一種可能的方法是,我可以不斷寫信息給客戶端。如果客戶端無法收到消息,則表示它已斷開連接。我發送郵件「確認連接:客戶端」。+ clientNo」到相應的客戶java tcp套接字檢查客戶端斷開連接

我clientNo 1連接到服務器並接收

客戶端1

"Checking Connection: Client1" <-- output every 10secs 
"Checking Connection: Client1" <-- output every 10secs 
"Checking Connection: Client1" <-- output every 10secs 

但是當我連接另一臺客戶端到服務器,我clientNo1 停止從服務器和我clientNo2接收消息現在會收到

客戶端2

"Checking Connection: Client2" <-- output every 10secs  
"Checking Connection: Client1" <-- output every 10secs 
"Checking Connection: Client2" <-- output every 10secs  
"Checking Connection: Client1" <-- output every 10secs 

所需的輸出

客戶端1

"Checking Connection: Client1" <-- output every 10secs 
"Checking Connection: Client1" <-- output every 10secs 
"Checking Connection: Client1" <-- output every 10secs 

客戶端2

"Checking Connection: Client2" <-- output every 10secs 
"Checking Connection: Client2" <-- output every 10secs 
"Checking Connection: Client2" <-- output every 10secs 

請幫助。謝謝。

Server.java

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

public class Server { 
    private int portNo = 0; 
    private ObjectOutputStream out; 
    private ObjectInputStream in; 
    private boolean clientAlive = true; 

    @SuppressWarnings("resource") 
    public Server(int portNo) { 
     Socket socket = null; 
     this.portNo = portNo; 
     ServerSocket sSocket = null; 
     int clientNo = 1; 
     try { 
      sSocket = new ServerSocket(this.portNo); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     while(true) { 
      try { 
       socket = sSocket.accept(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      new Thread(new acceptClient(socket,clientNo)).start(); 
      clientNo += 1; 
     } 
    } 

    class acceptClient implements Runnable { 
     Socket socket; 
     int clientNo = 1; 
     String writeToClient = ""; 

     public acceptClient(Socket socket, int clientNo) { 
      this.socket = socket; 
      this.clientNo = clientNo; 
     } 

     public void run() { 
      try { 
       out = new ObjectOutputStream(socket.getOutputStream()); 
       in = new ObjectInputStream(socket.getInputStream()); 
      } catch(IOException exception) { 
       System.out.println("Error: " + exception); 
      } 

      while(clientAlive) { 
       writeToClient = "Checking Connection: Client" + clientNo; 
       sendData(out, writeToClient.getBytes()); 

       try { 
        Thread.sleep(10000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

       if(!clientAlive) { 
        break; 
       } 

      } 
     } 
    } 

    public synchronized void sendData(ObjectOutputStream os, byte[] byteData) { 
     if (byteData == null) { 
      return; 
     } 
     try { 
      os.write(byteData); 
      os.flush(); 
     } 
     catch (Exception e) { 
      System.out.println("Client Disconnected"); 
      clientAlive = false; 
     } 
    } 

    public static void main(String[] args) { 
     System.out.println("Server started"); 
     new Server(5550); 
    } 
} 

Client.java

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

import javax.swing.JDialog; 
import javax.swing.JOptionPane; 


public class Client { 
    public static void main(String args[]) { 
     clientConnection(); 
    } 

    public static void clientConnection() { 
     Socket clientSocket = null; 
     ObjectOutputStream out = null; 
     ObjectInputStream in = null; 

     try { 
      clientSocket = new Socket("localhost", 5550); 
     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      out = new ObjectOutputStream(clientSocket.getOutputStream()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      in = new ObjectInputStream(clientSocket.getInputStream()); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     byte[] byteData = null; 
     while(true) { 
      try { 
       byteData = receive(in); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      System.out.println(new String(byteData)); 
     } 
    } 

    public static byte[] receive(ObjectInputStream is) throws Exception { 
     try { 
      byte[] inputData = new byte[1024]; 
      is.read(inputData); 
      return inputData; 
     } 
     catch (Exception exception) { 
      throw exception; 
     } 
    } 
} 
+0

你應該在try塊內啓動線程,而不是在catch塊之後。取決於try塊成功的代碼必須位於try塊內。目前你運行在同一個接受的套接字上啓動兩個線程的風險。 – EJP

回答

2

ObjectInputStreamObjectOutputStreamServer類的一部分。讓他們成爲acceptClient類的成員,它應該沒問題。

相關問題