아래코드를 컴파일한 후 클래스를 특정 위치에 놓는다. 그리고 사용하지 않는포트(:1234)를 찾아 포트를 지정하여 다음과 같이 실행한다.

> Java  TinyHttpd   1234

 

브라우저에서 운영되는 호스트명과 지정된 포트(:1234)를 입력하고 추가로 요청할 html 소스를 입력하여 요청한다.

 

 

 

 

package ch13;

 

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.net.SocketException;

import java.util.concurrent.Executor;

import java.util.concurrent.Executors;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

public class TinyHttpd {

       public static void main(String argv[]) throws IOException {

              Executor executor = Executors.newFixedThreadPool(3);

              ServerSocket ss = new ServerSocket(Integer.parseInt(argv[0]));

              while (true)

                     executor.execute(new TinyHttpdConnection(ss.accept()));

       }

}

 

class TinyHttpdConnection implements Runnable {

       Socket client;

 

       TinyHttpdConnection(Socket client) throws SocketException {

              this.client = client;

       }

 

       public void run() {

              try {

                     BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream(), "8859_1"));

                     OutputStream out = client.getOutputStream();

                     PrintWriter pout = new PrintWriter(new OutputStreamWriter(out, "8859_1"), true);

                     String request = in.readLine();

                     System.out.println("Request: " + request);

 

                     Matcher get = Pattern.compile("GET /?(\\S*).*").matcher(request);

                     if (get.matches()) {

                           request = get.group(1);

                           if (request.endsWith("/") || request.equals(""))

                                  request = request + "index.html";

                           try {

                                  FileInputStream fis = new FileInputStream(request);

                                  byte[] data = new byte[64 * 1024];

                                  for (int read; (read = fis.read(data)) > -1;)

                                         out.write(data, 0, read);

                                  out.flush();

                           } catch (FileNotFoundException e) {

                                  pout.println("404 Object Not Found");

                           }

                     } else

                           pout.println("400 Bad Request");

                     client.close();

              } catch (IOException e) {

                     System.out.println("I/O error " + e);

              }

       }

}

  

 

 

 

다음의 소스는TinyHttpd를 개선한 서버코드이다. SocketChannels Thread Pool를 이용해 Nonblocking 웹서버를 구성하였다.

 

소스 다운 >>  LargerHttpd.java

'Java > Networking' 카테고리의 다른 글

서버와의 시간동기화 [ 소켓통신 ]  (0) 2013.02.11
소켓통신 프로그램의 기본 구조  (0) 2013.02.07
RMI (Remote Method Invovation)  (0) 2013.02.01
Proxy Server 란  (1) 2013.02.01
Posted by Steven J.S Min
,