qq_36354917
?????JAVA
2017-08-21 11:53

不会写http的有关东西,刚刚学习 还望大神给我详细介绍这个,谢谢大家

  • 浏览器
  • 通信
  • 服务器
  • socket

package com.briup.webServer.v1;

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.net.ServerSocket;
import java.net.Socket;

public class ServerMain {

public static void main(String[] args) throws IOException {
    System.out.println("hello");

    ServerSocket ss = null;
    Socket s = null;
    BufferedReader in = null;
    PrintStream out = null;
    String path;
    String i = null;
    String []bs = null;
    byte []b1 = new byte[1024*4];
    BufferedReader br = null;

    FileInputStream f1 = null;
    FileInputStream f2 = null;



    try {
        f1 = new FileInputStream("files/index.html");
        f2 = new FileInputStream("files/error.html");
        ss = new ServerSocket(9999);
        s = ss.accept();
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));

        i=in.readLine();
        System.out.println(i);
        bs= i.split(" ");
        for (String string : bs)
        {
            System.out.println(string);
        }
        out = new PrintStream(s.getOutputStream());
        out.flush();
        int j = - 1;
        char[] b =new char[1024];
        if (bs[0].equals("GET")&&bs[1].equals("/")) 
        {
            while((j = in.read(b))!=-1) {
                out.print(b);
            }
        }


        String ip = s.getInetAddress().getHostAddress();
        System.out.println("客户端ip:"+ip);



    } catch (Exception e) 
    {
        // TODO: handle exception
    }finally 
    {


    }

}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答