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


Lightweight Web Server Implementation
 
Àü¿¡ ÂËÀÌ ¸¸µé¾ú´ø HTTP Webserver¸¸µé±â°¡ ¹ö°Ü¿ü´Ù¸é °£´Ü¸í·áÇÏ°Ô À¥¼­¹öÀÇ ±â´ÉÀ» °®´Â ³×Æ®¿÷ÇÁ·Î±×·¥À» ¸¸µé¾îº¸ÀÚ. ( 2003/03/18 ) 281
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 

ÀÏÀü¿¡ ¿Ã·È´ø ÂË~(ÀüÈ«¼º´Ô)ÀÌ ¾´ HTTP Webserver¸¸µé±â Æí¿¡¼­ ¸ðµÎµé ÀÌÇظ¦ ÀßÇß´ÂÁö°¡
»ó´çÈ÷ ±Ã±ÝÇÏ´Ù. À̹ø Æí¿¡¼­´Â ¾ÆÁÖ °£´ÜÇÑ ÃÊ¹Ì´Ï À¥¼­¹ö¸¦ ¸¸µå´Â ¹æ¹ýÀ» 
Çغ¸°Ú´Âµ¥ ¼ÒÄÏ°ú ½º·¹µå¿¡ ´ëÇÑ ³»¿ëÀ» Á¶±Ý¸¸ ¾Ë°í ÀÖ°í, http protocolÀÇ ºê¶ó¿ìÁ®°¡
½Äº°ÇÒ ¼ö ÀÖ´Â Çì´õ¸Þ½ÃÁö¸¦ ¾È´Ù¸é ½±°Ô ¸¸µé¼ö ÀÖÀ» °ÍÀÌ´Ù.

¢º ¹Ì´ÏÀ¥¼­¹ö ¸¸µé±â

ÀÚ. À¥¼­¹öÀÇ ±âº»ÀÛµ¿¿ø¸®´Â ¹«¾ùÀϱî? ÀÚ¹Ù·Î À¥¼­¹ö¸¦ ¸¸µç´Ù¸é ´ç¿¬È÷ ½º·¹µå¸¦ ÀÌ¿ëÇÒ
°ÍÀ̸ç, c·Î¼­ ¸¸µé°Ô µÈ´Ù¸é °¢ ¿äû¿¡ ´ëÇÑ process¸¦ fork½ÃÄÑ ¼­ºñ½ºÇڵ鷯¸¦ ¸¸µé¾î󸮸¦
ÇÏ°Ô µÉ °ÍÀ̶õ°Ô ¸Ó¸®¼Ó¿¡ Á¶±ÝÀº µé¾î¿À´Â°¡?

ÀÚ¹Ù·Î ¼ÒÄÏÇÁ·Î±×·¥À» Â¥º¸¾Ò´Ù¸é ¼ø¼­´Â ¾î¶»°Ô µÇ´Â °ÍÀΰ¡? 

1. ¼­¹öµ¥¸óÀ» ±¸µ¿ÇÑ´Ù.
2. Ŭ¶óÀ̾ðÆ®°¡ ¼­¹öÃøÀÇ ¼­¹ö¼ÒÄÏÀ¸·Î Á¢±ÙÀ» ½ÃµµÇÑ´Ù.
3. ¼­¹ö´Â Ŭ¶óÀ̾ðÆ®¿Í ¿¬°áµÇ¾îÁú ¼ÒÄÏ°´Ã¼¸¦ »ý¼ºÇÑ´Ù.

ÀÚ, ÀÌÁ¦ 4¹øºÎÅÍ°¡ P2P¹æ½ÄÀÎÁö server daemon¹æ½ÄÀÇ ¼­ºñ½ºÀÎÁö°¡ °áÁ¤ÀÌ ³¯¼ö Àִµ¥
½º·¹µå¸¦ »ý¼ºÇÏÁö ¾Ê°í¼­ Á÷Á¢ Ŭ¶óÀ̾ðÆ®ÀÇ request¸¦ ó¸®ÇÑ´Ù°í °¡Á¤ÇÏÀÚ.

¸¸¾à ¼­ºñ½º¸¦ ¿Ï·áÇϴµ¥ °É¸®´Â ½Ã°£ÀÌ 1ºÐÀ̾ú´Ù°í °¡Á¤À» ÇÏ°Ô µÈ´Ù¸é ´ÙÀ½ ¿äûÀ» ÇϰԵǴÂ
Ŭ¶óÀ̾ðÆ®´Â ¾î¶»°Ô µÉ°ÍÀΰ¡? 

connection timeout ¶Ç´Â connection refused¸¦ °æÇèÇÏ°Ô µÉ°ÍÀÌ´Ù. ±Ã±ÝÇÑ°¡? Á÷Á¢ Â¥º¸¶ó~!
¾Æ·¡¿Í °°Àº Äڵ带 ÀÌ¿ëÇÏ¿© Å×½ºÆ®Çغ¸¸é µÉ°ÍÀÌ´Ù.



¢º ServerÃø
    public MiniWebServer() {
        try
        {
            ServerSocket me = new ServerSocket( 8081 );
            Socket client = me.accept();
            Thread.sleep(1000 * 60);
			System.out.println( "client: " + client.getInetAddress() );
        }
        catch ( Exception  ex )	{
                System.out.println( ex + "server start failed!!" );
        }
    }

¢ºClientÃø
    public void connect() {
        Socket soc = new Socket("127.0.0.1", 8081);
    }


°£´ÜÇÏ°Ô À§Ã³·³¸¸ Â¥º¸°í Å×½ºÆ®Çغ¸¶ó. ¼­¹ö´Â Ŭ¶óÀ̾ðÆ®ÀÇ ¿äûÀÌ µé¾î¿À°Ô µÇ¸é
1ºÐµ¿¾ÈÀ» ¹Ùº¸Ã³·³ °¡¸¸È÷ ÀáÀÚ°Ô µÉ¶§ ´Ù¸¥ Ŭ¶óÀ̾ðÆ®·Î Á¢±ÙÀ» ½ÃµµÇغ¸¶ó~

°á°ú´Â ¿©·¯ºÐµéÀÌ Á÷Á¢ Â¥º¸µµ·Ï ÇÑ´Ù.

°¢¼³ÇÏ°í °è¼Ó ³ª°¡°Ú´Ù. ±×·¯¸é À§¿Í °°Àº ¼­ºñ½ºÀÇ ²÷±èÇö»óÀÌ ¹ß»ýÇÏÁö ¾Ê°í, Ŭ¶óÀ̾ðÆ®ÀÇ
¿äûÀ» Á·Á·ÀÌ ¸ðµÎ ¼­ºñ½º°¡ °¡´ÉÇÏ°Ô ÇÏ·Á¸é ¾î¶»°Ô ÇØ¾ß Çϳª?

´äÀº ¹Ù·Î À§¿¡µµ À̾߱â Çß´ø°Íó·³ ½º·¹µåÀÌ´Ù.

4. Ŭ¶óÀ̾ðÆ®ÀÇ ¿äûÀÌ µé¾î¿À¸é ÇØ´ç ¼ÒÄÏÀ» ¼­ºñ½ºÇڵ鸵À» ÇÒ ¼ö ÀÖ´Â ½º·¹µå·Î ÀüÀ̽ÃŲ´Ù.
5. ¼ÒÄÏÀ» ³Ñ°Ü³ÖÀº ½º·¹µå´Â ½ÇÁ¦ ¿äûÀÌ µé¾î¿Â Ŭ¶óÀ̾ðÆ®¿¡°Ô ¼­ºñ½º¸¦ Çϵµ·Ï ÇÑ´Ù.
6. ¿äûÀ» ¸ðµÎ ó¸®ÇßÀ¸¸é http¼Ó¼º´ä°Ô ¿¬°áÀ» ²÷¾î¹ö¸°´Ù.


À§ÀÇ ½Ã³ª¸®¿À°¡ ÀüºÎÀÌ´Ù.

³×Æ®¿÷ÇÁ·Î±×·¥ÀÇ ºÒº¯ÀÇ ¹ýÄ¢À» ¾Æ´Â°¡? ¾ÆÁÖ °£´ÜÇÏ´Ù.
°£´ÜÇÑ ¸¸Å­ ³×Æ®¿÷ÇÁ·Î±×·¡¹Ö ¹æ¹ýÀ» °£´ÜÇÏ°Ô ¼³¸íÇÏ°Ú´Ù.

1. ¼­¹öÃøÇÁ·Î±×·¥À» ÀÛ¼ºÇÑ´Ù.
    - ¼­¹ö¼ÒÄÏÀ» »ý¼ºÇÑ´Ù.
    - ¹«ÇÑ·çÇÁ¸¦ ÀÌ¿ëÇÏ¿© ¼­¹ö¼ÒÄÏÀÇ accept()·Î Ŭ¶óÀ̾ðÆ®¿äûÀ» ±â´Ù¸°´Ù.
    - Ŭ¶óÀ̾ðÆ®¿äûÀÌ µé¾î¿Ã °æ¿ì ¼­ºñ½º¸¦ ÇÒ ½º·¹µå¸¦ »ý¼ºÇÏ°í accept()¿¡ ÀÇÇÏ¿© ³Ñ°ÜÁø 
    socketÀ» ½º·¹µåÃøÀ¸·Î Àü´ÞÇÑ´Ù.
    - ¼­¹ö¸¦ ´Ù½Ã ¼­ºñ½º¸¦ °è¼ÓÇÑ´Ù.

2. Ŭ¶óÀ̾ðÆ®Ãø ÇÁ·Î±×·¥À» ÀÛ¼ºÇÑ´Ù.
    - ¼­¹öÃøÀ¸·Î Á¢¼ÓÀ» ½ÃµµÇÒ ¼ö ÀÖ´Â ¼ÒÄÏÀ» IP¿Í Æ÷Æ®·Î »ý¼ºÇÑ´Ù.
    - º¸³»°íÀÚ ÇÏ´Â µ¥ÀÌÅ͸¦ streamÀ¸·Î ÇÊÅ͸µÇÏ¿© ¼­¹ö·Î Àü¼ÛÇÑ´Ù.

ÀÌ·±~~ ¾µ·Á´Ï Á¶±Ý Èûµé´Ù. ³×Æ®¿÷ÇÁ·Î±×·¡¹ÖÀ» ÀÚÀ¯ÀÚÀç·Î ÇÏ°í ½ÍÀº°¡? ±×·¸´Ù¸é ³î»õÀÇ À̾߱⸦
µé¾îÁÖ±æ ¹Ù¶õ´Ù. 

Thread°³³ä, IO°³³ä¸¸ Á¤È®ÇÏ°Ô ¾È´Ù¸é ³×Æ®¿÷ÇÁ·Î±×·¥Àº 51% Á¤º¹ÇÑ °Å³ª ´Ù¸§ ¾ø´Ù.

±×¸¸ ¶°µé°í ÇÁ·Î±×·¥À̳ª º¸À̶ó´Â ¼Ò¸®°¡ ¿©±âÀú±â¼­ µé¸®´ÂµíÇÏ´Ù. ¾ÆÈå~

ÀÚ. ÇÁ·Î±×·¥À¸·Î µé¾î°£´Ù. ¸ÕÀú ¼­¹ö¼ÒÄÏÀ» ÀÌ¿ëÇÏ¿© Ŭ¶óÀ̾ðÆ®ÀÇ ¿äûÀ» ¹ÞÀ» ¼ö ÀÖ´Â 
¼­¹öÇÁ·Î±×·¥À» ½º·¹µåÇüÅ·ΠÀÛ¼ºÇغ¸ÀÚ. 

¢º ¼­¹öÃø µ¥¸ó

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

public class  MiniWebServer extends Thread {
    private ServerSocket me;

    public MiniWebServer() {
        try
        {
            me = new ServerSocket( 9090 );
        }
        catch ( IOException  ex )	{
                System.out.println( ex + "server start failed!!" );
        }
    }

    public void run() {
        while ( true )	{
            try	{
                Socket client = me.accept();
                System.out.println( "client: " + client.getInetAddress() );
                new ServiceHandler( client );
            }catch ( Exception ex ){
                System.out.println( ex );
            }
        }
    }

    public static void main(String[] args) 	{
        MiniWebServer mini = new MiniWebServer();
        mini.start();
        System.out.println("web server start!!");
    }
}


¢º ¼­¹öÃø ¼­ºñ½º ½º·¹µå

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

class ServiceHandler implements Runnable {
    private Socket client;

    public ServiceHandler( Socket c ) {
        try{
            client = c;
            System.out.println( client.getPort() + " : " + client.getLocalPort() );
            Thread t = new Thread( this );
            t.start();
        }catch(Exception e) {

        }
    }
    public void run()  {
        try	{
            PrintWriter out = new PrintWriter( client.getOutputStream() );
            out.print( "HTTP/1.0 OK\r\n" );
            out.print( "Server: HTTPServer 1.0\r\n" );
            out.print( "Content-Type: text/html\r\n\r\n" );
            out.println( "¹Ý°©½À´Ï´Ù....<hr>" );
            out.println( "<br>³î»õ°¡ ¸¸µç ¼¼»ó... ^^" );
            out.close();
        }catch ( IOException ex ){
            System.out.println( ex );
        }
    }
}


¾Æ. À§ÀÇ ÇÁ·Î±×·¥ÀÌ Á¶±ÝÀÌ¶óµµ ÀÌÇØ°¡ °¡´Â°¡? ±×·¸´Ù¸é ÀÌ ±ÛÀ» Àд ´ç½Å~!!! ¶°³ª¶ó~~~!!

´Ù¸¥ ¾ÆƼŬ·Î ¶°³ª¶ó. ´çÀå ¿©±â¸¦ ´©¸£±â ¹Ù¶õ´Ù. ¿ä±â

ÀÚ. À§¿¡¼­ º¸°Ô µÇ¸é ¼­¹öÃøÀÇ accept()¸Þ¼Òµå°¡ ¼öÇàµÇ°Ô µÇ¸é ¹Ù·Î ¼­ºñ½º Çڵ鷯ÃøÀ¸·Î
¼­¹ö¼ÒÄÏÀÌ ³Ñ°ÜÁØ Socket°´Ã¼¸¦ ½º·¹µå·Î ³Ñ°ÜÁÖ´Â ¿ªÇÒÀ» ÇÏ°Ô µÇ°í ½ÇÁ¦ ±× ¼­ºñ½º½º·¹µå´Â
PrintWriter°´Ã¼¸¦ Ŭ¶óÀ̾ðÆ®Ãø¿¡ °®´Ù´íÈÄ HTTP ÇÁ·ÎÅäÄÝ·Î ½÷ÁÖ´Â Çü½ÄÀ» ÃëÇÏ°í ÀÖ´Ù.

¿©·¯ºÐµé ¼­ºí¸´À» ÀÛ¼ºÇغ¸¾Ò´Â°¡? º¸¾Ò´Ù¸é reponse.getWriter()¸¦ º¸¾Ò´Â°¡? 
±âº»ÀûÀÎ ¼­ºí¸´ÀÇ getWriter()¸¦ ÇÏ°Ô µÇ¸é ÀÌ¹Ì responseÀÇ ½ºÆ®¸²À§¿¡ Àú·¯ÇÑ http protocolÀÇ
Çì´õ°¡ µé¾îÀÖ´Â °ÍÀÌ´Ù. Àú Á¤º¸´Â ´©°¡ Çؼ®À» ÇÏ°Ô µÉ±î? ±×·¸´Ù..¹Ù·Î ºê¶ó¿ìÁ®ÀÌ´Ù.

½ÇÇàÀ» Çغ¸ÀÚ. ¼³¸¶ ½ÇÇàÇÏ´Â ¹æ¹ýÀ» ¸ð¸£´Â JPerµéÀÌ ÀÖÀ»±îºÁ ³î»õ°¡ Ä£ÀýÇÏ°Ô Àû°í 
±× °á°ú±îÁö Àû¾îº¸µµ·Ï ÇÏ°Ú´Ù.

¸ÕÀú ÄÄÆÄÀÏ.

javac *java

ÀÚ, ÄÄÆÄÀÏÀÌ µÇ¾ú´Ù¸é ¼­¹öµ¥¸óÀ» ±¸µ¿Çϵµ·Ï ÇÏÀÚ.

java MiniWebServer

ÀÚ. ¼­¹ö°¡ ±¸µ¿µÇ¾î¼­ µµ½ºÃ¢ÀÌ ¸ÔÅëÀÌ µÈ´Ù°í ³î¶óÁö ¾Ê¾Æ¾ß ÇÑ´Ù.
"¾î¶ó? Ŭ¶óÀ̾ðÆ® ÇÁ·Î±×·¥ÀÌ ¾ø´Âµ¥ Àú°É ¾î¶»°Ô Å×½ºÆ®ÇØ"¶ó°í ¹°¾îº¸¸é °¡½¿ÀÌ Àú·Á¿Ã°ÍÀÌ´Ù.

Ŭ¶óÀ̾ðÆ® ÇÁ·Î±×·¥Àº ¹«¾ùÀϱî? ¹Ù·Î¹Ù·Î À¥ºê¶ó¿ìÁ®ÀÌ´Ù.
À¥ºê¶ó¿ìÁ®¸¦ ¹Ù·Î Çϳª ¶ç¿öº¸µµ·Ï ÇÑ´Ù.

URLÀÔ·Ââ¿¡ ¾Æ·¡¿Í °°ÀÌ ÀÔ·ÂÇϵµ·Ï ÇÑ´Ù.

http://127.0.0.1:9090/

°á°ú°ªÀÌ ¾Æ·¡¿Í °°ÀÌ ³ª¿Â´Ù¸é ´ë¼º°øÀÌ´Ù.

µµ½ºÃ¢
web server start!!
client: /127.0.0.1
3397 : 9090

À¥ºê¶ó¿ìÁ®



À§¿Í °°ÀÌ ³ª¿À´Â°¡?

½Å±âÇÑ°¡? Àß ºÐ¼®Çغ¸±æ ¹Ù¶ó¸ç, À§ÀÇ ¼­ºñ½º Çڵ鷯¸¦ ÀÌ¿ëÇÏ¿© Á¶ÀÛÇÏ°Ô µÈ´Ù¸é ¿©·¯ºÐµµ
Àü¿¡ ÂËÀÌ ¸¸µé À¥¼­¹ö°°Àº °£´ÜÇÑ ÃÊ°æ·® À¥¼­¹ö? ¹®Á¦µµ ¾Æ´Ï´Ù.

°ÇÅõ¸¦ ºó´Ù.
 
1
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD