-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from mjj111/feat/#2_web_server
웹 서버 main에 병합
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.*; | ||
import java.net.Socket; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class RequestHandler extends Thread{ | ||
private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); | ||
private Socket connect; | ||
|
||
public RequestHandler(Socket connect) { | ||
this.connect = connect; | ||
} | ||
|
||
public void run() { | ||
log.debug("사용자가 연결되었습니다. Connected IP : {}, Port : {}", connect.getInetAddress(), connect.getPort()); | ||
|
||
try(InputStream in = connect.getInputStream(); | ||
OutputStream out = connect.getOutputStream()) { | ||
DataOutputStream dos = new DataOutputStream(out); | ||
byte[] body = "안녕하세요!".getBytes(StandardCharsets.UTF_8); | ||
response200Header(dos, body.length); | ||
responseBody(dos, body); | ||
} catch (IOException e) { | ||
log.error(e.getMessage()); | ||
} | ||
|
||
|
||
} | ||
|
||
private void responseBody(final DataOutputStream dos, final byte[] body) { | ||
try { | ||
dos.write(body, 0, body.length); | ||
dos.writeBytes("\r\n"); | ||
dos.flush(); | ||
} catch (IOException e) { | ||
log.error(e.getMessage()); | ||
} | ||
} | ||
|
||
private void response200Header(final DataOutputStream dos, final int length) { | ||
try { | ||
dos.writeBytes("HTTP/1.1 200 OK \r\n"); | ||
dos.writeBytes("Content-Type : text/html; charset=utf-8\r\n"); | ||
dos.writeBytes("Content-Length" + length + "\r\n"); | ||
dos.writeBytes("\r\n"); | ||
} catch (IOException e) { | ||
log.error(e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
public class WebServer { | ||
private static final int DEFAULT_PORT = 8080; | ||
public static void main(String[] args) throws IOException { | ||
int port; | ||
|
||
if(args == null || args.length == 0) { | ||
port = DEFAULT_PORT; | ||
} else { | ||
port = Integer.parseInt(args[0]); | ||
} | ||
|
||
try(ServerSocket listenSocket = new ServerSocket(port)) { | ||
Socket connection; | ||
while((connection = listenSocket.accept()) != null) { | ||
RequestHandler requestHandler = new RequestHandler(connection); | ||
requestHandler.start(); | ||
}; | ||
} | ||
} | ||
} |