zoukankan      html  css  js  c++  java
  • 模拟服务器1.0——WebServer 、ClientHandler 接收请求、做出响应

    package org.springblade.modules.school.socket;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    
    
    /**
     * socket服务器端的程序
     *
     * @author BladeX
     * @since 2020-09-23
     */
    public class WebServer {
    
        /**
         * 1.声明ServerSocket,代表服务器
         */
        private ServerSocket server;
    
        /**
         * 声明线程池对象
         */
        private ExecutorService pool;
    
        /**
         * 构造函数初始化ServerSocket对象
         */
        public WebServer() {
            try {
                server = new ServerSocket(8082);
                //固定大小的线程池
                pool= Executors.newFixedThreadPool(100);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 创建start方法,用来接收请求,处理业务,响应
         */
        public void start() {
            // 用来接收请求
            try {
                while (true) {
                    // 返回客户端
                    Socket socket = server.accept();
                    //改造start方法
                    pool.execute(new ClientHandler(socket));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    package org.springblade.modules.school.socket;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    
    
    /**
     * socket服务器端的程序
     *
     * @author BladeX
     * @since 2020-09-23
     */
    public class WebServer {
    
        /**
         * 1.声明ServerSocket,代表服务器
         */
        private ServerSocket server;
    
        /**
         * 声明线程池对象
         */
        private ExecutorService pool;
    
        /**
         * 构造函数初始化ServerSocket对象
         */
        public WebServer() {
            try {
                server = new ServerSocket(8082);
                //固定大小的线程池
                pool= Executors.newFixedThreadPool(100);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 创建start方法,用来接收请求,处理业务,响应
         */
        public void start() {
            // 用来接收请求
            try {
                while (true) {
                    // 返回客户端
                    Socket socket = server.accept();
                    //改造start方法
                    pool.execute(new ClientHandler(socket));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    /*
     *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
     *
     *  Redistribution and use in source and binary forms, with or without
     *  modification, are permitted provided that the following conditions are met:
     *
     *  Redistributions of source code must retain the above copyright notice,
     *  this list of conditions and the following disclaimer.
     *  Redistributions in binary form must reproduce the above copyright
     *  notice, this list of conditions and the following disclaimer in the
     *  documentation and/or other materials provided with the distribution.
     *  Neither the name of the dreamlu.net developer nor the names of its
     *  contributors may be used to endorse or promote products derived from
     *  this software without specific prior written permission.
     *  Author: Chill 庄骞 (smallchill@163.com)
     */
    package org.springblade;
    
    import org.springblade.common.constant.CommonConstant;
    import org.springblade.core.launch.BladeApplication;
    import org.springblade.modules.school.socket.WebServer;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    /**
     * 启动器
     *
     * @author Chill
     */
    @EnableScheduling
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            BladeApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args);
            WebServer server = new WebServer();
            server.start();
        }
    
    }
  • 相关阅读:
    2021年通达信指标公式大全,值得收藏!
    网络兼职?威客?为什么我会觉得网络兼职,威客会是人生中应该具备的一种能力!
    SeMusic 音乐网站源代码,PHP音乐系统,人人都是站长人人都可副业创业!
    JavaScript 查看图片,带缩放放大效果
    JS (javascript) 计算循环当前时间,javascript 时间钟表
    关键词被冷藏?关键词没排名?任务网站长们该何去何从?
    关键词任务网被K,对于任务网该何去何从?我认为任务网存活只有一条出路!
    C3属性的轮播图(持续更新)
    自己写的文字轮播(简陋版)
    带锁的3D切割轮播图
  • 原文地址:https://www.cnblogs.com/fangts/p/13730690.html
Copyright © 2011-2022 走看看