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


Making the Http WebServer(Java·Î web server¸¦ ±¸ÇöÇÏ´Â ¹æ¹ý)
 
Network, Stream I/O, Multi-Thread Programming ÀÌÇØ
Server Application ±¸Á¶, Thread Pooling, Logging, Dynamic Configuration ±â´É ÀÌÇØ ( 2003/02/28 ) 331
Written by specular - ÀüÈ«¼º
2 of 3
 
<font face='±¼¸²'>2. HttpWebServer - Network Server Application  
     À̹ø¿¡´Â Network Server Application±¸ ±¸Á¶¿¡ ´ëÇÑ ÀÌÇØ¿Í HttpWebServer classÀÇ
     ±¸Çö¿¡ ´ëÇÑ ³»¿ëÀ» ¼Ò°³ÇÕ´Ï´Ù. Server App´Â ConnectionÀ» ListenÇÏ´Â Daemon Thread¿Í
     ConnectionÈÄÀÇ request¸¦ ó¸®ÇÏ´Â Handler Thread·Î ±¸¼ºÀÌ µË´Ï´Ù. 
  ConnectionÀ» ListenÇÏ´Â Daemon ThreadÀÇ °£´ÜÇÑ ¿¹´Â ´ÙÀ½°ú °°ÀÌ ±¸ÇöÇÒ¼ö ÀÖ½À´Ï´Ù. 
import java.io.*;
import java.net.*;

/*
 *	Server App Example
 *	2001.05.16
 * 	written by Jeon HongSeong
 */
public class Listen implements Runnable 
{
private ServerSocket server;

public Listen() {
	try {
		//1. Intanciate a ServerSocket object.
		server = new ServerSocket(8080);
	} catch(IOException e) {
		e.printStackTrace();
		System.exit(0);
	}
}

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

public void run() 
{
	try {
		while(true) 
		{
			System.out.println("Listen...");
			//2. listen...
			Socket sck = server.accept();
			String client=sck.getInetAddress().getHostAddress();
			System.out.println(client+" is connected.");
			
			//3. handler thread start(pass the socket)
		}
	} catch(IOException e) {
		e.printStackTrace();
	}
}
}
¢º Listen.java
Test : web browser¿¡¼­ http://localhost:8080 ÇØ º¸¸é µÇ°ÚÁÒ. 

À§¿¡¼­ ServerSocket°´Ã¼ÀÇ accpet() ¸Þ¼Òµå´Â client connectionÀ» ½ÃµµÇØ ¿Ã¶§±îÁö 
blockµÇ¾îÁø´Ù. client°¡ connectionÀ» ½ÃµµÇϸé, À̶§ 8080 port·Î connectionÀ» ½ÃµµÇؾ߰ÚÁÒ. 
nofityµÇ¾î connectionÀ» ½ÃµµÇÑ client¸¦ À§ÇØ Çã¿ë°¡´ÉÇÑ ÀÓÀÇÀÇ port¸¦ ÇÒ´çÇÏ°í, 
client¿¡ connectionÁ¤º¸¸¦ °®°í ÀÖ´Â Socket°´Ã¼¸¦ returnÇÑ´Ù. 
returnµÈ Socket°´Ã¼´Â SocketImpl°´Ã¼¸¦ instanceº¯¼ö·Î °®°í ÀÖ°í, 
SocketImplFactory¸¦ class º¯¼ö·Î °®°í ÀÖ´Ù. SocketImplfactory interface´Â Factory objectÀÌ°í, 
SocketImpl°´Ã¼´Â ½ÇÀç ¿¬°áÁ¤º¸¸¦ °®°í ÀÖ´Â °´Ã¼ÀÔ´Ï´Ù. 
±×·¡¼­, port no, InetAddress, InputStream, OutputStreamÀÇ Á¤º¸¸¦ °®°í ÀÖÁÒ.
clientÀÇ connectionÀÌ ÀÌ·ç¾î Áö¸é, request¸¦ ó¸®Çϱâ À§ÇÑ Handler thread¸¦ 
start½ÃÄÑ, Handler thread¿¡ ÀÇÇØ request¸¦ ó¸®ÇÏ°í responseÇÏ°Ô²û ±¸ÇöÇÏ¸é µË´Ï´Ù. 
HttpWebServerÀÇ application source´Â ´ÙÀ½°ú °°½À´Ï´Ù. 
package com.specular.http;

import java.io.*;
import java.net.*;
import java.util.*;
import com.specular.util.LogManager;
import com.specular.util.PropManager;

/*
*	HttpWebServer.java - Server Application
*	2001.05.17
* 	written by Jeon HongSeong
*/
public class HttpWebServer implements Runnable 
{	
private ServerSocket servSocket;
private boolean	SHUTTDOWN;

public HttpWebServer() 
	throws StartUpException 
{
	try {
		//PropManger »ý¼º
		String port = PropManager.getInstance().getProperty("port","8080");
		PropManager.getInstance().list();
		//MimeDB data¸¦ loading
		MimeDB.list();
		
		servSocket = new ServerSocket(Integer.parseInt(port));
	} catch(IOException e) {
		LogManager.log(e,"at the HttpWebServer() Constructor");
		throw new StartUpException(e.getMessage());	
	}
}
	
public static void main(String args[]) 
	throws StartUpException 
{
	
	HttpWebServer server = new HttpWebServer();
	Thread t = new Thread(server);
	t.start();
	LogManager.log("The HttpWebServer is started.");
	LogManager.log("The Listening port is 8080.");
}

public void run() 
{
	SHUTTDOWN=true;
	//Thread pooling object
	HandlerPool pool = HandlerPool.getInstance();	
	while(SHUTTDOWN) 
	{
		try {
			// waiting...(listening a connection.)
			Socket sck = servSocket.accept();
			LogManager.log(sck.getInetAddress().getHostAddress()+" is connected.");
			
			// Thread pooling....
			pool.execute(sck);				
		} catch(IOException e) {
			LogManager.log(e,"at the HttpWebServer.run() method.");
			SHUTTDOWN=false;
		}
	}
}
}
¢º HttpWebServer.java
¢º StartUpException.java





2001.05.18 written by Jeon HongSeong 
 
1 2 3
References
 
This article Source
webserver.jar
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD