2016-09-27 119 views
0

我想在我的GUI中按停止按鈕時關閉我的服務器。我用這個代碼,插座,如果一個客戶端連接到的ServerSocket這是聽:如何關閉套接字,如果它正在監聽連接

public void run() { 
     while (running) { 
      try { 
       if (serverSocketPassed) { 
        socket = serverSocket.accept(); 
        scanner = new Scanner(socket.getInputStream()); 
        stream = new PrintStream(socket.getOutputStream()); 
       } 
      } catch (IOException e) { 
       System.out.println("err11"); 
       e.printStackTrace(); 
      } 
     } 

    } 

而對於封閉部分代碼如下這樣:

public void stop() { 
    try { 
      serverSocket.close(); 
      running = false; 
     } catch (Exception e) { 
      System.out.println("Err"); 
      e.printStackTrace(); 
     } 
     scanner.close(); 
     stream.close(); 
} 

但是當我打電話停止函數,在客戶端連接之前:出現錯誤。 這是錯誤:

err11 
java.net.SocketException: socket closed 
    at java.net.DualStackPlainSocketImpl.accept0(Native Method) 
    at java.net.DualStackPlainSocketImpl.socketAccept(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.accept(Unknown Source) 
    at java.net.PlainSocketImpl.accept(Unknown Source) 
    at java.net.ServerSocket.implAccept(Unknown Source) 
    at java.net.ServerSocket.accept(Unknown Source) 
    at _47b3n.server.main.Server$SocketRunner.run(Server.java:93) 
    at java.lang.Thread.run(Unknown Source) 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at _47b3n.server.main.Server.stop(Server.java:42) 
    at _47b3n.server.main.ServerGUI$ActionListener.actionPerformed(ServerGUI.java:166) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$500(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

如何正確關閉服務器及其組件?

+0

'scanner'和'stream'沒有定義;檢查'null'。至於你的「'SocketException:socket關閉」錯誤 - 你必須忍受這一點;如果你在等待它時關閉套接字,就會得到這個結果 - 抓住Exception並很好地記錄下來。 –

回答

0

而不是突然關閉serversocket,關閉流。

嘗試這種情況:

stream.close();