模拟获取请求协议和返回响应协议

模拟获取请求协议和返回响应协议

使用RESTer观察结果

package cn.com.serverlet02;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class Server02 {

//    //返回响应协议
//    private ServerSocket serverSocket;
//    public static void main(String[] args) {
//        Server01 server = new Server01();
//        server.start();
//    }
//    //启动服务
//    public void start() {
//        try {
//            serverSocket = new ServerSocket(8888);
//            receive();
//        } catch (IOException e) {
//            System.out.println("服务器启动失败。。。");
//        }
//    }
//    //接受连接处理
//    public void receive() {
        try {
            Socket client = serverSocket.accept();
            System.out.println("一个客户端建立了连接。。。");
            InputStream is = client.getInputStream();
            byte[] datas = new byte[1024*1024];
            int len = is.read(datas);
            String requestInfo = new String(datas,0,len);
            System.out.println(requestInfo);
        } catch (IOException e) {
            System.out.println("客户端错误。。。");
        }
//        try {
//            Socket client1 = serverSocket.accept();
//            InputStream is1= client1.getInputStream();
//            byte[] datas = new byte[1024*1024];
//            int len = is1.read(datas);
//            String requestInfo = new String(datas,0,len);
//            System.out.println(requestInfo);
//
//            StringBuilder content = new StringBuilder();
//            content.append("<html>");
//            content.append("<head>");
//            content.append("<title>");
//            content.append("服务器响应成功");
//            content.append("</title>");
//            content.append("</head>");
//            content.append("<body>");
//            content.append("yuxioahao Server终于回来了...");
//            content.append("</body>");
//            content.append("</html>");
//            int size = content.toString().getBytes().length;
//            StringBuilder responseInfo = new StringBuilder();
//            String BLANK=" ";
//            String CRLF="\r\n";
//
//            //返回
//            //1 响应的状态行 HTTP/1.1 200 OK
//            responseInfo.append("HTTP/1.1").append(BLANK);
//            responseInfo.append(200).append(BLANK);
//            responseInfo.append("OK").append(CRLF);
//            //最后一行存在空行
//            //2 响应头:Date:Mon,31Dec209904:25:57GMT
//            //          Server:yuxiaohao Server/0.0.1;charset=GBK
//            //          Content-type:text/html
//            //          Content-length:39725426
            responseInfo.append("Date:").append(new Date()).append(CRLF);
            responseInfo.append("Server:").append("yuxiaohao  Server/0.0.1;charset=GBK").append(CRLF);
            responseInfo.append("Content-type:text/html").append(CRLF);
            responseInfo.append("Content-length:").append(size).append(CRLF);
            responseInfo.append(CRLF);
//            responseInfo.append("Date:").append(new Date()).append(CRLF);
//            responseInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF);
//            responseInfo.append("Content-type:text/html").append(CRLF);
//            responseInfo.append("Content-length:").append(size).append(CRLF);
//            responseInfo.append(CRLF);
//            //3 正文
//            responseInfo.append(content.toString());
//
//            //写出到客户端
//            BufferedWriter bw = new BufferedWriter(
//                    new OutputStreamWriter(client1.getOutputStream()));
//            bw.write(responseInfo.toString());
//            bw.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//    //停止服务
//    public void stop() {
//
//    }
    private ServerSocket serverSocket ;
    public static void main(String[] args) {
        Server02 server = new Server02();
        server.start();
    }
    //启动服务
    public void start() {
        try {
            serverSocket =  new ServerSocket(9999);
            receive();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("服务器启动失败....");
        }
    }
    //接受连接处理
    public void receive() {
        try {
            Socket client = serverSocket.accept();
            System.out.println("一个客户端建立了连接....");
            //获取请求协议
            InputStream is =client.getInputStream();
            byte[] datas = new byte[1024*1024];
            int len = is.read(datas);
            String requestInfo = new String(datas,0,len);
            System.out.println(requestInfo);

            StringBuilder content =new StringBuilder();
            content.append("<html>");
            content.append("<head>");
            content.append("<title>");
            content.append("服务器响应成功");
            content.append("</title>");
            content.append("</head>");
            content.append("<body>");
            content.append("shsxt server终于回来了。。。。");
            content.append("</body>");
            content.append("</html>");
            int size = content.toString().getBytes().length; //必须获取字节长度
            StringBuilder responseInfo =new StringBuilder();
            String blank =" ";
            String CRLF = "\n";
            //返回
            //1、响应行: HTTP/1.1 200 OK
            responseInfo.append("HTTP/1.1").append(blank);
            responseInfo.append(200).append(blank);
            responseInfo.append("OK").append(CRLF);
            //2、响应头(最后一行存在空行):
			/*
			 Date:Mon,31Dec209904:25:57GMT
			Server:shsxt Server/0.0.1;charset=GBK
			Content-type:text/html
			Content-length:39725426
			 */
            responseInfo.append("Date:").append(new Date()).append(CRLF);
            responseInfo.append("Server:shsxtServer/0.0.1;charset=GBK").append(CRLF);
            responseInfo.append("Content-type:text/html").append(CRLF);
            responseInfo.append("Content-length:").append(size).append(CRLF);
            responseInfo.append(CRLF);
            //3、正文
            responseInfo.append(content.toString());

            //写出到客户端
            BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            bw.write(responseInfo.toString());
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("客户端错误");
        }
    }
    //停止服务
    public void stop() {

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值