-
Java - Webserver HttpJava 2018. 2. 7. 11:07반응형import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class WebServer {public static void main(String[] args) throws Exception {ExecutorService pool = Executors.newFixedThreadPool(4);//1.서버소켓 생성ServerSocket server = new ServerSocket(8085);while(true) {System.out.println("요청 대기");//2. 요청이 있으면 수락Socket socket = server.accept();System.out.println("요청 수락");//스레드가 할 일Runnable run = new Runnable() {@Overridepublic void run() {//3. http 프로토콜로 오는 데이터 수신try {DataInputStream dis = new DataInputStream(socket.getInputStream());byte[] arr = new byte[65536];dis.read(arr);String request = new String(arr);System.out.println(request);arr = new byte[65536];} catch (IOException e) {e.printStackTrace();}//4. 응답}};//submit 은 기본적으로 반환값이 없음/실패한 스레드도 재사용pool.submit(run);}}}결과 ->GET / HTTP/1.1Host: localhost:8085Connection: keep-aliveUser-Agent: Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36Upgrade-Insecure-Requests: 1Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8Accept-Encoding: gzip, deflate, brAccept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7Cookie: darkness_stats={"analyticsId":"59547866d8","installDate":1515546591345,"installVer":"1.0.0","type":"p"}반응형
'Java' 카테고리의 다른 글
Java - JavaFX (0) 2018.02.08 Java - Lambda (0) 2018.02.07 Java - Network - File I/O (0) 2018.02.07 Java - UDP (0) 2018.02.06 Java - Network, MultiChat (0) 2018.02.06 댓글