2013-04-19 4 views
1

나는 java를 처음 사용했습니다. 나는 버튼서버 버튼 닫기 문제

JButton startServerButton = new JButton("Start"); 
    startServerButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if (e.getActionCommand().equals("Start")) { 
       Runnable serverRunnable = new Runnable() { 
        public void run() { 
         runServer(); 
        } 
       }; 
       Thread serverThread = new Thread(serverRunnable); 
       serverThread.start(); 
      } 
     } 
    }); 

내 서버를 시작하고 난 버튼

JButton endServerButton = new JButton("End"); 
    endServerButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if (e.getActionCommand().equals("End")) { 
       closeConnection(); 
      } 
     } 
    }); 

하지만 닫기 버튼 나던 작업으로 닫으시겠습니까 ... 도와주세요!

1) 다음 연결을 소켓 종료 후 시작 스레드 'IO &를 새 스레드에서 끝을 작업을 넣고 : 여기

private void closeConnection() { 
    displayMessage("\nTerminating connection\n"); 
    try { 
     output.close(); 
     input.close(); 
     connection.close(); 

    } catch (IOException ioException) { 
     ioException.printStackTrace(); 
    } 
+0

게시하지 않은 다른 코드에 문제가있는 것 같습니다. 정확하게 오류가 무엇입니까? –

답변

0

이 옵션을 시도해보십시오 가까운 기능을합니다. 종료 작업 스레드에서

2)을 (수단이 다른 스레드에서 1 스레드 소켓 연결을 닫기), 시작 스레드 '소켓 제한 시간을 설정합니다.

감사합니다.