[SimpleWebServer]

ho'sยท2022๋…„ 6์›” 9์ผ
0

๐ŸŽ„ SIMPLEWEBSERVER

๐ŸŽ‰ SimpleWebServer ๋งŒ๋“ค๊ธฐ

  1. ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ๋ฅผ ๊ธฐ๋‹ค๋ฆฐ๋‹ค.
  2. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ ‘์†ํ•˜๋ฉด, ํ•ด๋‹น ํด๋ผ์ด์–ธํŠธ์™€ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋Š” Socket์ด ๋ฐ˜ํ™˜๋œ๋‹ค.
  3. Socket์ด ๋ฐ˜ํ™˜๋˜๋ฉด ํด๋ผ์ด์–ธํŠธ์™€ ํ†ต์‹ ํ•˜๋Š” Thread๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค.
  4. 1๋ฒˆ๋ถ€ํ„ฐ ๋‹ค์‹œ ์‚ฌ์ž‘ํ•œ๋‹ค.

๐Ÿ‘Ÿ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ์—ฐ๊ฒฐ ๊ณผ์ •

๐Ÿ“œ 1๋‹จ๊ณ„

์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์†Œ์ผ“์„ ํ†ตํ•ด ์—ฐ๊ฒฐํ•˜๋ ค๋ฉด ServerSocket์„ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.
๊ทธ ํ›„ ServerSocket๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด Socket์„ ์—ฐ๊ฒฐํ•œ๋‹ค.

public class SimpleWebServer{
	public static void main(String[] args) throws Exception{
    // Exception์€ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋Œ€์ถฉ ํ•˜๊ฒ ๋‹ค!
    
    ServerSocket = new ServerSocket(9090);
    // port๋ฒˆํ˜ธ 9090๋ฒˆ์œผ๋กœ ์„œ๋ฒ„์†Œ์ผ“ ๋งŒ๋“ค๊ฒ ๋‹ค.
    
    while(true){
    	Socket socket = ss.accept();
    }
    	
        
    }
}

๐Ÿ“œ 2๋‹จ๊ณ„

SocketThread ๋งŒ๋“ค๊ธฐ
1. SocketThread ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์„œ, ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค.
2. Thread๋ฅผ ์ƒ์† ๋ฐ›์•˜์„ ๋•Œ, run()๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์ค˜์•ผ ํ•œ๋‹ค.

class SocketThread extends Thread{
	Socket socket;
    public SocketThread(Socket socket){
    	this.socket = socket;
    }
		
    public void run(){
    	
    } 
}
  1. SocketThread๋ฅผ ์‚ฌ์šฉํ•˜์ž.
 public class SimpleWebServer{
	public static void main(String[] args) throws Exception{
    // Exception์€ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋Œ€์ถฉ ํ•˜๊ฒ ๋‹ค!
    
    ServerSocket = new ServerSocket(9090);
    // port๋ฒˆํ˜ธ 9090๋ฒˆ์œผ๋กœ ์„œ๋ฒ„์†Œ์ผ“ ๋งŒ๋“ค๊ฒ ๋‹ค.
    
    System.out.println("server start!!");
    while(true){
    	Socket socket = ss.accept();
        System.out.println("client: " + socket);
        SocketThread st = new SocketThread(socket);
        st.start();
    }
    	
    }
}

class SocketThread extends Thread{
	Socket socket;
    public SocketThread(Socket socket){
    	this.socket = socket;
    }
		
    public void run(){
    	System.out.println("์†Œ์ผ“์„ ์ด์šฉํ•ด client์™€ ๋™์ž‘ํ•œ๋‹ค");
    } 
}
  1. ๊ฒฐ๊ณผ

๐Ÿ‘Ÿ socket์„ ํ†ตํ•ด InputStream, OutputStream ๊ตฌํ•˜๋Š” ๊ณผ์ •

๐Ÿ“œ 1๋‹จ๊ณ„ SocketThread์˜ run()๋ฉ”์†Œ๋“œ ์ˆ˜์ •ํ•˜๊ธฐ

class SocketThread extends Thread{
	Socket socket;
    public SocketThread(Socket socket){
    	this.socket = socket;
    }
		
    public void run(){
    	System.out.println("์†Œ์ผ“์„ ์ด์šฉํ•ด client์™€ ๋™์ž‘ํ•œ๋‹ค");
    	try{
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream();
        // 1์ค„์”ฉ socket์—์„œ ์“ฐ๊ธฐ ๊ฐ์ฒด
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
       
       // 1์ค„์”ฉ socket์—์„œ ์ฝ๋Š” ๊ฐ์ฒด
      BufferedReader br = new BufferedReader(new InputStreamReader(in));

    	socket.close();
        }catch(Exception e){
        	e.printStackTrace();
        }
    
    } 
}

๐Ÿ“œ 2๋‹จ๊ณ„ ์ฝ์–ด๋“ค์ด๊ธฐ Request ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ

class Request{
	String firstLine;  // HTTP์—์„œ ์ฒซ๋ฒˆ์งธ ์ค„์ด ์ค‘์š”ํ•˜๋‹ค.
    List<String> headers; // ๋‚˜๋จธ์ง€ headers์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด List์„ ์–ธ.
    
    // ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ดˆ๊ธฐํ™”.
    public Request(){
    	headers = new ArrayList<>();
    } 
    
    
    // firstLine์„ get,setํ• ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ. (์ฝ๊ณ  ์“ธ์ˆ˜ ์žˆ์Œ)
   public String getFirstLine(){
        return firstLine;
    }

    public void setFirstLine(String firstLine){
        this.firstLine = firstLine;
    }
    
    //header๋ฅผ ์ฝ์–ด headers์— ์ €์žฅ.
    public void addHeader(String header){
    	headers.add(header);
    }
    
    //Iterator๋Š” ์ฝ์–ด๋“ค์ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
    public Iterator<String> getHeaders(){
    	return headers.iterator();
    }
}

ํด๋ผ์ด์–ธํŠธ์™€ ์ ‘์†์ด ๋˜๋ฉด ProcessRequest ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์ž.
์ฝ์–ด๋“ค์ด๋Š” ๊ฐ์ฒด์ด๋ฏ€๋กœ in,br์„ ์ด์šฉํ•œ๋‹ค.
request๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ processRequest๋ฅผ ๋งŒ๋“ค์ž.

๐Ÿ“œ 3๋‹จ๊ณ„ ProcessRequest ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ

// ๋ธŒ๋ผ์šฐ์ €์˜ ์š”์ฒญ์„ ์ฝ์–ด๋“ค์—ฌ์„œ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
Request requset = processRequest(in, br);
private Request processRequest(InputStream in, BufferedReader br) throws Exception{
	Request request = new Request();
    //request ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด, ์ฒซ๋ฒˆ์งธ ์ค„ ์ฝ๊ธฐ
    request.setFirstLine(br.readLine());
    String line = null;
    while(!(line = br.readLine()).equals("")){
   		request.addHeader(line);
    } 
    return request;
}

์ฒซ๋ฒˆ์งธ ์ค„์„ ์ฝ์–ด๋“ค์ด๊ณ ,
๋‘๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ ๋นˆ์ค„์ด์˜ฌ๋•Œ ๊นŒ์ง€ ์ฝ๋Š”๋‹ค.

๐Ÿ“œ 4๋‹จ๊ณ„ path๊ตฌํ•˜๊ธฐ

class Request{
	String path;
    
     public void setFirstLine(String firstLine){
        this.firstLine = firstLine;
        this.path = firstLine.substring(4, firstLine.lastIndexOf(" "));
        if("/".equals(path)){
            path = "/index.html";
        }
    }
}
  • path๊ฐ€ / ์ผ๋•Œ๋Š” index.html(๊ธฐ๋ณธํŒŒ์ผ)์„ ์ฝ์–ด๋“ค์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

  • index.html ํŒŒ์ผ์„ ์„œ๋ฒ„์—์„œ ์ฝ์–ด๋“ค์—ฌ์„œ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฟŒ๋ ค์•ผ ํ•œ๋‹ค.

class SocketThread extends Thread{
   ...
    }
    // ๋ธŒ๋ผ์šฐ์ €์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ..
    
    
    public void run() {
       ...
        try {
            // ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ •๋ณด๋ฅผ ์–ป๊ณ , ๋‚ด๋ณด๋‚ด๋Š” ์—ญํ• ์„ ํ•œ๋‹ค!
            OutputStream out = socket.getOutputStream();
            InputStream in = socket.getInputStream();
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out)); // ํ•œ์ค„์”ฉ socket์“ฐ๋Š” ๊ฐ์ฒด
            BufferedReader br = new BufferedReader(new InputStreamReader(in)); // ํ•œ์ค„์”ฉ socket์—์„œ ์ฝ๋Š” ๊ฐ์ฒด
            // ๋ธŒ๋ผ์šฐ์ €์˜ ์š”์ฒญ์„ ์ฝ์–ด๋“ค์—ฌ์„œ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 
            Request request = processRequest(in,br);
            System.out.println(request.getFirstLine());
            System.out.println(request.getPath());
            //์‘๋‹ตํ•œ๋‹ค.

            //์‘๋‹ตํ•œ๋‹ค.
            // path์˜ ๋‚ด์šฉ์„ ์ฝ์–ด๋“ค์—ฌ์„œ ํด๋ผ์ด์–ธํŠธ์— ์ „์†กํ•œ๋‹ค.
 **์ถ”๊ฐ€๋œ ๋‚ด์šฉ    
 		processResponse(request.getPath(), out ,pw);
            socket.close();
        }catch(Exception ex){
          ex.printStackTrace();
        }
    } // run

**์ถ”๊ฐ€๋œ ๋‚ด์šฉ
private void processResponse(String path, OutputStream out, PrintWriter pw){

    }
profile
๊ทธ๋ž˜์•ผ๋งŒ ํ•œ๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€