์๋ฒ์ ํด๋ผ์ด์ธํธ๋ฅผ ์์ผ์ ํตํด ์ฐ๊ฒฐํ๋ ค๋ฉด 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();
}
}
}
SocketThread ๋ง๋ค๊ธฐ
1. SocketThread ์์ฑ์๋ฅผ ๋ง๋ค์ด์, ์ด๊ธฐํ ํด์ค๋ค.
2. Thread๋ฅผ ์์ ๋ฐ์์ ๋, run()๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํด์ค์ผ ํ๋ค.
class SocketThread extends Thread{
Socket socket;
public SocketThread(Socket socket){
this.socket = socket;
}
public void run(){
}
}
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์ ๋์ํ๋ค");
}
}
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();
}
}
}
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๋ฅผ ๋ง๋ค์.
// ๋ธ๋ผ์ฐ์ ์ ์์ฒญ์ ์ฝ์ด๋ค์ฌ์ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค.
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;
}
์ฒซ๋ฒ์งธ ์ค์ ์ฝ์ด๋ค์ด๊ณ ,
๋๋ฒ์งธ ์ค๋ถํฐ ๋น์ค์ด์ฌ๋ ๊น์ง ์ฝ๋๋ค.
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";
}
}
}
์์ ์ฝ๋๋ฅผ ์ถ๊ฐํ๊ณ ์คํํ๋ฉด ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์จ๋ค.
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){
}