Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


SocketPool Implementation Source code Example
 
java.net.Socket 객체에 대한 Pooling 객체정의 및 구현샘플코드를 보도록 한다. ( 2003/03/10 ) 385
Written by specular - 전홍성
2 of 2
 



SocketPool에 대한 예제입니다. 근데.. 앞에서 구현한 SocketPool은 실재로 사용하실려면 좀더 보완을 해야 할겁니다. Socket의 수를 Shrink 시켜주는 Thread도 추가시키고, 그리고, Socket객체를 사용중 서버쪽 문제로 IOExcpetion이 발생할경우 Socket을 close하고 제거하는 부분도 생각해봐야 하겠고요. 하여튼 아래 SocketPool을 사용한 예제입니다.


1. Server Application

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

public class Server implements Runnable {
private ServerSocket server;

public Server() {
	try {
		server = new ServerSocket(5555);
	} catch(IOException e) {
		System.out.println(e.toString());
		System.exit(0);
	}
}

public static void main(String args[]) {
	Server svr = new Server();
	Thread t = new Thread(svr);
	t.start();
}

public void run() {
	while(true) {
		try {
			System.out.println("listen...");
			Socket sck = server.accept();
			Handler handler = new Handler(sck);		
			
		} catch(IOException e) {
			System.out.println(e.toString());
		}
	}
}

public class Handler implements Runnable {
	private DataInputStream in;
	private DataOutputStream out;
	private Socket sck;
	
	public Handler(Socket sck) {
		try {
			this.sck = sck;
			in = new DataInputStream(sck.getInputStream());
			out = new DataOutputStream(sck.getOutputStream());
			Thread t = new Thread(this);
			t.start();
		} catch(IOException e) {
			try {
				sck.close();
			} catch(IOException ex) {
				ex.printStackTrace();
			}
		}
	}
	
	public void run() {
		try {
			while(true) {
				String msg = in.readUTF();
				System.out.println("received msg : "+msg);
				out.writeUTF("Hello "+msg);
			}
		} catch(IOException e) {
			System.out.println(e.toString());
			try {
				in.close();
				out.close();
				sck.close();
			} catch(IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}
}

2. SocketPool을 사용하는 SocketServlet
            
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import specular.net.*;

public class SocketServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse res) 
		throws IOException, ServletException 
	{
		SocketPool pool=null;
		DataOutputStream out=null;
		DataInputStream in=null;
		Socket sck=null;
		try {
			res.setContentType("text/html");
			PrintWriter pout = res.getWriter();
			pool = SocketPool.getInstance();
			sck = pool.getSocket();
			out = new DataOutputStream(sck.getOutputStream());
			in = new DataInputStream(sck.getInputStream());
			out.writeUTF("HongSeong");
			String result=in.readUTF();
			System.out.println(result);
			pout.println("<b>"+result+"</b>");
		} catch(IOException e) {
			e.printStackTrace();
		} finally {
			try {
				pool.release(sck);
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
}

3. pool.propertis
            
host=127.0.0.1
port=5555
maxCons=100
initialCons=10
block=true
timeout=500

2001.07.21 written by Jeon HongSeong

 
 
1 2
References
 
Copyright ⓒ 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD