2016-11-03 11 views
0

각 데이터 그램 소켓에서 실행중인 반경 서버 목록이 있으며이 시작된 반경 서버 목록을 나와 함께 사용할 수 있습니다. 특정 서버가 중지되었는지 확인하고 중지해야 할 경우 다시 시작해야하는지 확인해야합니다. 다음은 필자가 작성한 코드이지만 이것이 올바른 방법인지 확실하지 않습니다. 상담하십시오.데이터 그램 포트에서 응용 프로그램이 실행되고 있는지 확인하는 방법은 무엇입니까?

....

int port = radiusServer.getAggregation() 
         .getAuthenticationPort(); 
       try { 
        // It will throw IO exception if no application is 
        // running 
        // on that 
        // port. 
        new DatagramSocket(port); 
        LOGGER.info(
          "There is a server running on the Port number {}.", 
          port); 
       } catch (IOException e) { 
        LOGGER.error(
          "Server is not running on port number {}.", 
          port); 
        startServer(radiusServer); 
       } 

....

+0

{시도 { \t \t \t \t \t \t는 // 그것은 IO 예외를 던질 것이다 \t에 \t \t \t \t \t \t // \t \t \t \t \t // 포트. \t \t \t \t \t \t DatagramSocket ds = new DatagramSocket (port); \t \t \t \t \t \t ds.close(); \t \t \t \t \t \t LOGGER.warn ( \t \t \t \t \t \t \t \t, "서버 포트 번호 {} 시작 서버에서 실행되고 있지 않습니다." \t \t \t \t \t \t \t \t 포트); \t \t \t \t \t \t startServer (radiusServer); \t \t \t \t \t} 캐치 (IOException이 전자) { \t \t \t \t \t \t (LOGGER.isDebugEnabled()) { \t \t \t \t \t \t \t LOGGER.debug ( \t \t \t \t \t \t \t \t 경우 \t "Por에서 이미 실행중인 서버가 있습니다. t 번호 {}." \t \t \t \t \t \t \t \t \t 포트) \t \t \t \t \t \t \t} \t \t \t \t}} – Mohit

답변

0

아래의 솔루션은 잘 작동합니다. 어떤 응용 프로그램이 실행 \t \t \t \t \t \t //없는 경우

  try { 
        // It wont throw IO exception if no application is 
        // running 
        // on that 
        // port. 
        DatagramSocket ds = new DatagramSocket(port); 
        ds.close(); 
        LOGGER.warn(
          "Server is not running on port number {}. Starting Server", 
          port); 
        startServer(radiusServer); 
       } catch (IOException e) { 
        if (LOGGER.isDebugEnabled()) { 
         LOGGER.debug(
           "There is a server already running on the Port number {}.", 
           port); 
        } 
       }