'getKeepAlive'에 해당되는 글 1건


getKeepAlive : 소켓이 살아있는지 죽었는지 확인하는 메소드

 

Server는 socket의 끊어지 상태 확인이 어려운 경우가 많으며
socket보다 방화벽의 connection timeout이 짧은 경우 종종 발생합니다.

서버는 소켓이 끊어진지 여부를 모르기 때문에 아래의 절차를 거쳐 socket close후 처리를 합니다.

if( socket.isConnected() == true && socket.getKeepAlive() == false) {
  socket.setKeepAlive(true);
  if(socket.getKeepAlive() == false) { // Socket 연결이 끊어 졌는지 확인
    socket.close();
    releaseSocket(socket, endpoint);
    socketsPool.clear(socketKey);
    socket = (Socket) socketsPool.borrowObject(socketKey);
  }
}


----- network.java ----
package com.redjava.java.network;

import java.net.*;
import java.io.*;

public class NetGetKeepAlive{
 
 public static final short TIME_PORT = 135;
 
 public static void main(String[] args) throws IOException{
  
  String hostName = null;
  System.out.println();
  
  try{
   Socket socket= new Socket(hostName,TIME_PORT);
   System.out.println("socket =" + socket);
   System.out.println("Keep Alive="+ socket.getKeepAlive());
   System.out.println("Send Buffer size ="+ socket.getSendBufferSize());
   System.out.println("output ="+socket.isOutputShutdown());
   System.out.println("port is =" + socket.getPort());
   System.out.println("socket address ="+socket.getLocalSocketAddress());
   InetAddress in= socket.getInetAddress();
   System.out.println(in);
   System.out.println("\n");
   System.out.print("RAW IP Address - (byte[]) : ");
   byte[] b1 = in.getAddress();
   
   for (int i=0; i< b1.length; i++) {
    if (i > 0) {
     System.out.print(".");}
     System.out.print(b1[i]);
   }

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


--- 실행결과 ---
socket =Socket[addr=localhost/127.0.0.1,port=135,localport=52195]
Keep Alive=false
Send Buffer size =8192
output =false
port is =135
socket address =/127.0.0.1:52195
localhost/127.0.0.1

RAW IP Address - (byte[]) : 127.0.0.1

 

블로그 이미지

슬픈외로움

개발이 어려워? 모든것엔 답이있다...

,