简单服务器开发多线程服务器

Posted 摘珰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单服务器开发多线程服务器相关的知识,希望对你有一定的参考价值。

处理客户端请求:

package com.zda.httpserver.core;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import com.zda.httpserver.util.Logger;


/**
 * 处理客户端请求
 * @author zda
 * @version 1.0
 * @since    1.0
 */
public class HandlerRequest implements Runnable {
    public Socket clientSocket;
    public HandlerRequest(Socket clientSocket){
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() {
        //处理客户端请求
        BufferedReader br = null;
        Logger.log("httpserver thread: " + Thread.currentThread().getName());
        try {
            //接收客户端消息
            br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            //打印客户端消息
            String temp = null;
            while((temp = br.readLine()) != null){
                System.out.println(temp);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //关闭资源
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(clientSocket != null){
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }

}

主入口

package com.zda.httpserver.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import com.zda.httpserver.util.Logger;

/**
 * httpserver主入口
 * @author zda
 * @version 1.0
 * @since   1.0
 *
 */
public class BootStrap {

    public static void main(String[] args) {
        // 程序入口
        start();
    }
    /**
     * 主程序入口
     */
    public static void start() {
        ServerSocket serverSocket = null;
        Socket clientSocket = null;
        BufferedReader br = null;
        try {
            Logger.log("httpserver start");
            //获取当前时间
            long start = System.currentTimeMillis();
            //获取系统端口号
            int port = ServerParser.getPort();
            Logger.log("httpserver-port: " + port);
            //创建服务器套接字,并且绑定端口号:8080
            serverSocket = new ServerSocket(port);
            //获取结束时间
            long end = System.currentTimeMillis();
            Logger.log("httpserver started: " + (end-start) + " ms");
            while(true){
                //开始监听网络,此时程序处于等待状态,等待接收客户端的消息
                clientSocket = serverSocket.accept();
                /*//接收客户端消息
                br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                String temp = null;
                while((temp = br.readLine()) != null){
                    System.out.println(temp);
                }*/
                new Thread(new HandlerRequest(clientSocket)).start();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //关闭资源
            /*if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(clientSocket != null){
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }*/
            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

}

 

以上是关于简单服务器开发多线程服务器的主要内容,如果未能解决你的问题,请参考以下文章

多线程在javaweb中的应用

Windows多线程开发之并发线程程序研究

多线程 Thread 线程同步 synchronized

golang代码片段(摘抄)

多线程设计模式

你可能不知道的JavaScript代码片段和技巧(下)