码迷,mamicode.com
首页 > 其他好文 > 详细

《how tomcat work》 搬运工 charpter 3:Connector

时间:2015-07-26 20:53:17      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

这一章节主要是介绍了tomcat 的一个重要模块 connector,但只是一个缩减版

connector的作用是创建ServerSocket的实例serverSocket,绑定端口,监听端口,当接受到客户端请求时,就创建一个HttpProcessor实例processor,将serverSocket作为参数给HttpProcessor的实例processor.process(serverSocket)来处理请求

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpConnector implements Runnable {

  boolean stopped;
  private String scheme = "http";

  public String getScheme() {
    return scheme;
  }

  public void run() {
    ServerSocket serverSocket = null;
    int port = 8080;
    try {
      serverSocket =  new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
    }
    catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }
    while (!stopped) {
      // Accept the next incoming connection from the server socket
      Socket socket = null;
      try {
        socket = serverSocket.accept();
      }
      catch (Exception e) {
        continue;
      }
      // Hand this socket off to an HttpProcessor
      HttpProcessor processor = new HttpProcessor(this);
      processor.process(socket);
    }
  }

  public void start() {
    Thread thread = new Thread(this);
    thread.start();
  }
}

 

《how tomcat work》 搬运工 charpter 3:Connector

标签:

原文地址:http://www.cnblogs.com/xuyung/p/4678592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!