<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    俊星的BLOG

    我的簡單HTTP服務器

    在BLOGJAVA上看到一篇關于HTTP SERVER的簡單實現,博文網址為:http://www.tkk7.com/beansoft/archive/2007/06/25/126049.html
    關于HTTP 協議更多信息,請參考:WIKIRFC2616。下面我的實現:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.URLDecoder;

    /**
     * 簡單HTTP服務器
     * 
     * 
    @author kinkding
     
    */
    public class MyHTTPServer implements Runnable {
        ServerSocket server;
        
    int port = 80;

        
    public MyHTTPServer() throws IOException {
            server 
    = new ServerSocket(port);
            
    new Thread(this).start();
            System.out.println(
    "HTTP服務器已經啟動");
        }

        
    public void run() {
            
    while (true) {
                
    try {
                    Socket client 
    = server.accept();
                    System.out.println(
    "接收到客戶端:" + client);
                    BufferedReader reader 
    = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    System.out.println(
    "請求內容如下:\n---------------------");
                    String line 
    = reader.readLine();
                    System.out.println(line);
                    
    // 請求的資源
                    String resource = line.substring(line.indexOf("/"), line.lastIndexOf("/"- 5);
                    resource 
    = URLDecoder.decode(resource, "UTF-8");
                    
    // 請求方法:GET/POST
                    String method = line.split(" ")[0];
                    
    int length = 0;
                    
    while ((line = reader.readLine()) != null && line.length() > 0) {
                        
    if (line.startsWith("Content-Length"&& length == 0) {
                            length 
    = Integer.parseInt(line.substring(line.indexOf(":"+ 1).trim());
                        }
                        System.out.println(line);
                    }
                    System.out.println(
    "---------------------");
                    String postData 
    = "";
                    
    if (method.equals("POST"&& length > 0) {
                        
    char cbuf[] = new char[length];
                        reader.read(cbuf, 
    0, length);
                        postData 
    = new String(cbuf);
                    }
                    
    if (resource.endsWith(".jpg")) {
                        
    this.sendFile("happyTime.jpg", client);
                        client.close();
                    } 
    else if (resource.endsWith(".do")) {
                        
    // 返回404
                        PrintWriter out = new PrintWriter(client.getOutputStream(), true);
                        out.println(
    "HTTP/1.0 404 Not found");
                        out.println();
                        out.close();
                    } 
    else {
                        PrintWriter out 
    = new PrintWriter(client.getOutputStream(), true);
                        out.println(
    "HTTP/1.0 200 OK");
                        out.println(
    "Content-Type:text/html;charset=GBK");
                        out.println();
    // HTTP協議:空行表示信息結束

                        out.println(
    "<UL>");
                        out.println(
    "<LI>Hello World");
                        out.println(
    "<LI><img src='anyJPG.jpg'/>");
                        out.println(
    "<LI>resource:" + resource);
                        out.println(
    "<LI>method:" + method);
                        out.println(
    "<LI><a href='foobar.do'>foobar.do</a>");
                        out.println(
    "</UL>");
                        
    // 測試POST
                        out.println("<form method='post' action='/'>");
                        out.println(
    "<input type='text' name='name' value=''/>");
                        out.println(
    "<input type='submit' value='POST'/>");
                        out.println(
    "POST DATA:" + postData);
                        out.println(
    "</form>");
                        
    // 測試GET
                        out.println("<form method='get' action='/'>");
                        out.println(
    "<input type='text' name='job' value=''/>");
                        out.println(
    "<input type='submit' value='GET'/>");
                        out.println(
    "</form>");

                        out.close();
                        client.close();
                    }

                } 
    catch (Exception e) {
                    
    // TODO: handle exception
                    e.printStackTrace();
                }
            }
        }

        
    private void sendFile(String name, Socket client) throws IOException {
            PrintStream out 
    = new PrintStream(client.getOutputStream(), true);
            File fileToSend 
    = new File(name);
            
    if (fileToSend.exists() && !fileToSend.isDirectory()) {
                out.println(
    "HTTP/1.0 200 OK");
                out.println(
    "Content-Type:application/binary");
                out.println(
    "Content-Length:" + fileToSend.length());
                out.println();

                FileInputStream fis 
    = new FileInputStream(fileToSend);
                
    byte buf[] = new byte[1024];
                
    int size = 0;
                
    while ((size = fis.read(buf)) > 0) {
                    out.write(buf, 
    0, size);
                }
                out.close();
                fis.close();
            }
        }

        
    public static void main(String[] args) {
            
    try {
                
    new MyHTTPServer();
            } 
    catch (IOException e) {
                
    // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    運行之后,控制臺的輸出類似如下:
    HTTP服務器已經啟動
    接收到客戶端:Socket
    [addr=/127.0.0.1,port=1198,localport=80]
    請求內容如下:
    ---------------------
    GET / HTTP/
    1.1
    Accept: image/gif
    , image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
    Accept-Language: zh-cn
    Accept-Encoding: gzip
    , deflate
    User-Agent: Mozilla/
    4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: localhost
    Connection: Keep-Alive
    ---------------------
    接收到客戶端:Socket
    [addr=/127.0.0.1,port=1199,localport=80]
    請求內容如下:
    ---------------------
    GET /anyJPG.jpg HTTP/
    1.1
    Accept: */*
    Referer: http://localhost/
    Accept-Language: zh-cn
    Accept-Encoding: gzip
    , deflate
    User-Agent: Mozilla/
    4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: localhost
    Connection: Keep-Alive
    ---------------------
    接收到客戶端:Socket
    [addr=/127.0.0.1,port=1218,localport=80]
    請求內容如下:
    ---------------------
    POST / HTTP/
    1.1
    Accept: image/gif
    , image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
    Referer: http://localhost/
    Accept-Language: zh-cn
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip
    , deflate
    User-Agent: Mozilla/
    4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: localhost
    Content-Length: 
    7
    Connection: Keep-Alive
    Cache-Control: no-cache
    ---------------------
    接收到客戶端:Socket
    [addr=/127.0.0.1,port=1231,localport=80]
    請求內容如下:
    ---------------------
    GET /?job
    =aaa HTTP/1.1
    Accept: image/gif
    , image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
    Referer: http://localhost/
    Accept-Language: zh-cn
    Accept-Encoding: gzip
    , deflate
    User-Agent: Mozilla/
    4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: localhost
    Connection: Keep-Alive
    ---------------------

    頁面運行效果類似如下:
     
    另,推薦一個不錯的命令行“netstat -an |find /i "listening" >good.txt”,查看當前處于監聽狀態的IP及端口,并將得到的信息重定向到good.txt中,形如:
      TCP    0.0.0.0:80             0.0.0.0:0              LISTENING
      TCP    
    0.0.0.0:135            0.0.0.0:0              LISTENING
      TCP    
    0.0.0.0:445            0.0.0.0:0              LISTENING
      TCP    
    0.0.0.0:2366           0.0.0.0:0              LISTENING
      TCP    
    0.0.0.0:19771          0.0.0.0:0              LISTENING
      TCP    
    127.0.0.1:1031         0.0.0.0:0              LISTENING

    posted on 2009-05-23 17:51 俊星 閱讀(515) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 久久亚洲精品无码gv| a级毛片在线免费| 亚洲免费观看视频| 影音先锋在线免费观看| a毛片免费播放全部完整| 久久精品私人影院免费看| 色屁屁在线观看视频免费| 久久夜色精品国产噜噜亚洲a| 国产精品偷伦视频免费观看了 | 成人A片产无码免费视频在线观看 成人电影在线免费观看 | 狼人大香伊蕉国产WWW亚洲| 激情综合亚洲色婷婷五月| 中文字幕亚洲乱码熟女一区二区| 91麻豆国产免费观看| 美景之屋4在线未删减免费| WWW国产成人免费观看视频| 久久精品视频免费播放| 卡1卡2卡3卡4卡5免费视频| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 亚洲AV成人潮喷综合网| 国产成人免费a在线视频色戒 | 亚洲妇女无套内射精| 中文字幕亚洲精品无码| 午夜在线免费视频| 色偷偷尼玛图亚洲综合| 免费看黄的成人APP| 成人无码区免费视频观看| 91福利视频免费| 国产传媒在线观看视频免费观看| 91在线手机精品免费观看| 日本v片免费一区二区三区| 四虎影视免费永久在线观看| 亚洲中文字幕不卡无码| 亚洲综合偷自成人网第页色| 亚洲精华国产精华精华液网站| 亚洲欧美中文日韩视频| 在线亚洲v日韩v| 猫咪免费人成网站在线观看入口| 色一情一乱一伦一视频免费看| 直接进入免费看黄的网站| 1区2区3区产品乱码免费|