• java简单的实现socket浏览器返回数据


    package com.yq.day75.tcp;
    
    
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    /**
     * @author 3307235355@qq.com
     * @description
     * @since 2022/07/06 08:30
     * 浏览器显示页面
     */
    public class Server {
        public static void main(String[] args) throws IOException {
            ServerSocket serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();
    
            InputStream inputStream = socket.getInputStream();
            byte[] bytes = new byte[4096];
            int read = inputStream.read(bytes);
            System.out.println("接收到的消息" + new String(bytes, 0, read));
    
            //返回消息
            OutputStream outputStream = socket.getOutputStream();
            // String s = "HTTP/1.1 200 OK\r\n" +
            //         "Content-type:text/html\r\n\r\n"+"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" +  // 解决乱码问题
            //         "<style>body{font-family:\"黑体\"}</style>" +
            //         "欢迎来到小蓝枣的博客<br>" +
            //         "Welcome to xiaolanzao's blog!";
    
            FileReader fileReader = new FileReader("src\\com\\yq\\day75\\tcp\\index.html");
            char[] chars = new char[1024];
            int readCount;
    
            while ((readCount = fileReader.read(chars)) != -1) {
                outputStream.write(new String(chars, 0, readCount).getBytes());
                System.out.println(new String(chars, 0, readCount));
            }
    
    
            fileReader.close();
            socket.close();
            serverSocket.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    html文件

    HTTP/1.1 200 OK\r\n
    Content-type:text/html
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>你好</title>
    </head>
    <body>
    <p>王道训练营java43期</p>
    <a href="https://img2.baidu.com/it/u=4249630208,1769413815&fm=253&fmt=auto&app=138&f=JPEG?w=806&h=480">图片点击</a>
    <img src="https://img2.baidu.com/it/u=4249630208,1769413815&fm=253&fmt=auto&app=138&f=JPEG?w=806&h=480">
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    MySQL 中的排序在底层是怎样实现的呢?
    港联证券:资金融通构成强支撑 “一带一路”金融合作开新局
    让logo设计更有设计感的几个方法
    2374. 边积分最高的节点
    Yii2 关联查询结果AR对象 如何取到表以外的字段
    Spring Boot 创建项目 / 目录介绍 / 配置文件
    .NET、VUE利用RSA加密完成登录并且发放JWT令牌设置权限访问
    马斯克回应OpenAI:"Change your name"
    2.7 配置Keepalived提高吞吐量
    [平台运维、Hadoop]kafka streams概述
  • 原文地址:https://blog.csdn.net/ncnhhm/article/details/125632257