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
'프로그래밍 Tip > JAVA & JSP' 카테고리의 다른 글
[JAVA source] HTTP POST 여러개의 파라미터를 포함한 request 테스트 (2) | 2014.07.15 |
---|---|
소켓을 통해 네트워크 정보를 보는 기본적인 예제 (0) | 2014.06.02 |
도메인에 대한 IP 주소 얻어내기 (0) | 2014.06.01 |
지정된 폴더내의 모든 파일들을 zip으로 압축하는 샘플 예제 (0) | 2014.05.23 |
지정된 파일을 zip 파일로 압축하기 (0) | 2014.05.23 |