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