加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java手写服务器(一)

发布时间:2020-12-15 05:32:25 所属栏目:Java 来源:网络整理
导读:客户端在浏览器发送请求,服务器接收请求,并作出响应。 a.html html head title 表单 / title / head body form method ="GET" action ="http://localhost:8870/index.html" 用户名: input type ="text" name ="uname" id ="name" 密码: input type ="pas

客户端在浏览器发送请求,服务器接收请求,并作出响应。

a.html

<html>
    <head>
            <title>表单</title>
    </head>
    <body>
        <form method="GET" action="http://localhost:8870/index.html">
            用户名:<input type="text"name="uname"id="name">
            密码:<input type="password"name="pws"id="pass">
            <input type="submit" value="登录">
        </form>
    </body>
</html>

Server3.java

package cn.server;

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

/*
 * 创建服务器,并启动
 * 请求并响应
 */
public class Server3 {
    private ServerSocket server;
    public static final String CRLF="rn";
    public static final String BLANK=" ";
    public static void main(String[] args) {
        Server3 server=new Server3();
        server.start();
    
        
    }
    //启动方法
    public void start() {
        
        try {
            server = new ServerSocket(8870);
            
            
            this.receive();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    /*
     * 接收客户端
     */
    private void receive() {
        try {
            Socket client=server.accept();
            
            String msg=null; 
            
            byte[] data=new byte[20480];
            int len=client.getInputStream().read(data);
             //接收客户端的请求信息    
            String requestInfo=new String(data,0,len).trim();
            System.out.println(requestInfo);
            
            //响应
            StringBuilder responseContext=new StringBuilder();
            responseContext.append("<html>rn" + 
                    "    <head>rn" + 
                    "            <title>表单</title>rn" + 
                    "    </head>rn" + 
                    "    <body>rn" + 
                    "        <form method="GET" action="http://localhost:8880/index.html">rn" + 
                    "            sun:<input type="text"name="uname"id="name">rn" + 
                    "            pwd:<input type="password"name="pws"id="pass">rn" + 
                    "            <input type="submit" value="登录">rn" + 
                    "        </form>rn" + 
                    "    </body>rn" + 
                    "</html>");
            StringBuilder response=new StringBuilder();
            //1)http协议版本、状态代码    、描述
            response.append("HTTP/1.1").append(BLANK).append("200").append(BLANK).append("ok").append(CRLF);
            //2)响应头(response Head)
            response.append("sun").append(CRLF);
            response.append("Date").append(new Date()).append(CRLF);
            response.append("Content-type:text/html;charset=GBK").append(CRLF);
            //正文的长度 :字节长度
            response.append("Content-Length:").append(responseContext.toString().getBytes().length).append(CRLF);
            //3)正文之前
            response.append(CRLF);
            //4)正文
            response.append(responseContext);
            
            //输出流  将响应发送出去
            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            bw.write(response.toString());
            bw.flush();
            bw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /*
     * 停止服务器
     */
    public void stop() {
        
    }
}

服务器打印结束过来的请求

响应结果

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读