2016-08-24 61 views
0

你好,這是我的第一個話題。多線程回聲服務器

我寫了一些代碼來通過套接字連接客戶端服務器。作爲客戶端,我的意思是打開終端窗口並通過telnet連接到此服務器。應用程序作爲服務器偵聽指定端口上來自客戶端的請求。每一個連接都被視爲一個新線程。一旦連接被綁定,客戶端就可以發送一條消息並作爲迴應得到答案。

問題是我想在客戶端關閉終端窗口(完成線程)時得到通知。我想知道這個關閉的線程的ID。

這是全碼:

public class MultithreadEchoServer 
{ 
    public static void main(String[] args) throws IOException 
    { 
     int count=0; 
     long[] ids = new long[10]; 

     try (ServerSocket ss = new ServerSocket(1234)) 
     { 
      System.out.println("Listening..."); 

      while(true) 
      { 
       count++; 
       Socket s = ss.accept(); 
       Runnable r = new MyThread(s, count); 
       Thread t = new Thread(r); 
       t.start(); 
       ids[count-1] = t.getId(); 

       if(Thread.activeCount() != count+1) 
       { 
        count = Thread.activeCount(); 
        System.out.println("Client "+/*??thread's id??*/" left the session. Now connected: "+count); 
       } 

       System.out.println("Clients connected: "+count); 
       System.out.println(ids[count-1]); 

      } 
     } 
    }   
} 

class MyThread implements Runnable 
{ 
    private Socket s; 
    private int count; 
    private long id; 
    InputStream io; 
    OutputStream os; 
    private PrintWriter pw; 

    MyThread(Socket s, int count) 
    { 
     this.s = s; 
     this.count = count; 
    } 

    public long getId() 
    { 
     id = Thread.currentThread().getId(); 
     return id; 
    } 

    @Override 
    public void run() 
    { 
     try 
     { 
      io = s.getInputStream(); 
      os = s.getOutputStream(); 
     } 
     catch (IOException e) {System.err.println(e);} 
     try (Scanner sc = new Scanner(io)) 
     { 
      pw = new PrintWriter(os, true); 
      pw.println("Connected"); 
      while(sc.hasNextLine()) 
      { 
       String line = sc.nextLine(); 
       System.out.println("Client "+count+": "+line); 
       pw.println("Echo: "+line);  
      } 
     } 
    } 
} 

回答

0

爲什麼你donn't打印線程ID線程執行與finally語句中的try後資源語句:

try (Scanner sc = new Scanner(io)) 
{ 
    pw = new PrintWriter(os, true); 
    pw.println("Connected"); 
    while(sc.hasNextLine()) 
    { 
     String line = sc.nextLine(); 
     System.out.println("Client "+count+": "+line); 
     pw.println("Echo: "+line);  
    } 
} 
finally { 
    System.out.println("Client " + this.id + " left the session.""); 
} 

MultithreadEchoServer

public class MultithreadEchoServer 
{ 
    public static void main(String[] args) throws IOException 
    { 
     int count=0; 
     long[] ids = new long[10]; 

     try (ServerSocket ss = new ServerSocket(1234)) 
     { 
      System.out.println("Listening..."); 

      while(true) 
      { 
       count++; 
       Socket s = ss.accept(); 
       Runnable r = new MyThread(s, count); 
       Thread t = new Thread(r); 
       t.start(); 
       ids[count-1] = t.getId(); 

       if(Thread.activeCount() != count+1) 
       { 
        count = Thread.activeCount(); 
        System.out.println("Now connected: "+count); 
       } 

       System.out.println("Clients connected: "+count); 
       System.out.println(ids[count-1]); 

      } 
     } 
    }   
} 

MyThread的

class MyThread implements Runnable 
{ 
    private Socket s; 
    private int count; 
    private long id; 
    InputStream io; 
    OutputStream os; 
    private PrintWriter pw; 

    MyThread(Socket s, int count) 
    { 
     this.s = s; 
     this.count = count; 
    } 

    public long getId() 
    { 
     id = Thread.currentThread().getId(); 
     return id; 
    } 

    @Override 
    public void run() 
    { 
     try 
     { 
      io = s.getInputStream(); 
      os = s.getOutputStream(); 
     } 
     catch (IOException e) {System.err.println(e);} 
     try (Scanner sc = new Scanner(io)) 
     { 
      pw = new PrintWriter(os, true); 
      pw.println("Connected"); 
      while(sc.hasNextLine()) 
      { 
       String line = sc.nextLine(); 
       System.out.println("Client "+count+": "+line); 
       pw.println("Echo: "+line);  
      } 
     } 
     finally { 
      System.out.println("Client " + this.id + " left the session.""); 
     } 
    } 
} 
+0

是的,它的工作原理,謝謝 – Jawegiel