采用BIO
单线程
public class tomcat {
public static final String SEPARATOR = "\r\n";
//源码注释:requested maximum length of the queue of incoming connections.
//BACK_LOG影响的accept队列大小
public static final int BACK_LOG = 1024;
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
Socket socket = null;
OutputStream outputStream = null;
BufferedWriter bufferedWriter = null;
try {
serverSocket = new ServerSocket(10000, BACK_LOG);
System.out.println("启动服务器");
for (; ; ) {
//阻塞式监听,会一直等待客户端连接
socket = serverSocket.accept();
//读取客户端消息
outputStream = socket.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(HttpRequest());
bufferedWriter.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String HttpRequest() {
String str = "<h1>tomcat</h1>";
StringBuilder builder = new StringBuilder();
builder.append("HTTP/1.1 200 OK").append(SEPARATOR);
builder.append("Connection: Close").append(SEPARATOR);
builder.append("Content-Type: text/html;charset=utf-8").append(SEPARATOR);
builder.append("Content-Length: " + str.length()).append(SEPARATOR);
builder.append(SEPARATOR);
builder.append(str);
return builder.toString();
}
}
多线程
//采用BIO+线程池
public class tomcatThread {
public static final String SEPARATOR = "\r\n";
//源码注释:requested maximum length of the queue of incoming connections.
//BACK_LOG影响的accept队列大小
public static final int BACK_LOG = 1024;
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(10000, BACK_LOG);
ThreadPoolExecutor threadPoolExecutor = buildThreadPoolExecutor();
System.out.println("启动服务器");
for (; ; ) {
//阻塞式监听,会一直等待客户端连接
Socket socket = serverSocket.accept();
threadPoolExecutor.execute(() -> {
OutputStream outputStream = null;
BufferedWriter bufferedWriter = null;
try {
//读取客户端消息
outputStream = socket.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
//模拟在做事
Thread.sleep(500);
bufferedWriter.write(HttpRequest());
bufferedWriter.flush();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
public static String HttpRequest() {
String str = "<h1>tomcat</h1>";
StringBuilder builder = new StringBuilder();
builder.append("HTTP/1.1 200 OK").append(SEPARATOR);
builder.append("Connection: Close").append(SEPARATOR);
builder.append("Content-Type: text/html;charset=utf-8").append(SEPARATOR);
builder.append("Content-Length: " + str.length()).append(SEPARATOR);
builder.append(SEPARATOR);
builder.append(str);
return builder.toString();
}
//线程池
public static ThreadPoolExecutor buildThreadPoolExecutor() {
return new ThreadPoolExecutor(
100,
100,
0,
TimeUnit.SECONDS,
new SynchronousQueue<>(),
new ThreadPoolExecutor.CallerRunsPolicy()
);
}
}