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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package
testreadline;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.concurrent.*;
import java.io.*;
public class test {
public
static void main(String[] args) throws
IOException {
new
SimpleHttpServer().service();
}
} class
SimpleHttpServer {
private
int port = 80 ;
private
ServerSocketChannel serverSocketChannel = null ;
private
ExecutorService executorService;
private
static final int POOL_MULTIPLE = 4 ;
public
SimpleHttpServer() throws
IOException
{
executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors() * POOL_MULTIPLE);
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().setReuseAddress( true );
serverSocketChannel.socket().bind( new
InetSocketAddress(port));
}
public
void service() {
while
( true ) {
SocketChannel socketChannel = null ;
try
{
socketChannel = serverSocketChannel.accept();
executorService.execute( new
Handler(socketChannel));
} catch
(IOException e) {
e.printStackTrace();
}
}
}
} class
Handler implements
Runnable {
private
SocketChannel socketChannel;
public
Handler(SocketChannel socketChannel) {
this .socketChannel = socketChannel;
}
@Override
public
void run() {
handle(socketChannel);
}
private
void handle(SocketChannel socketChannel) {
try
{
Socket socket = socketChannel.socket();
System.out.println(socket.getInetAddress() + ":"
+ socket.getPort());
String info = "HTTP/1.1 200 OK\n"
+
"Server: Apache-Coyote/1.1\n"
+
"Content-Type: text/html;charset=utf-8\n"
+
"Content-Length: 1021\n"
+
"Date: Wed, 09 Dec 2009 05:00:27 GMT\n"
+
"\n" ;
OutputStream os = socket.getOutputStream();
os.write(info.getBytes( "utf-8" ));
os.flush();
String html= "<H1>港港都是泪,还是早停困!</H1>" ;
os.write(html.getBytes( "utf-8" ));
os.flush();
} catch
(IOException e) {
e.printStackTrace();
} finally
{
try
{
if
(socketChannel != null )
socketChannel.close();
} catch
(IOException e) {
e.printStackTrace();
}
}
}
private
Charset charset = Charset.forName( "GBK" );
private
ByteBuffer encode(String string) {
return
ByteBuffer.allocate(string.length() * 2 ).get(
string.getBytes(charset));
}
private
String decode(ByteBuffer buffer) {
byte [] source = new
byte [buffer.position() + 1 ];
buffer.put(source);
return
new String(source, charset);
}
} |
相关文章
- 09-13使用IPv6格式的URL访问HTTP服务器
- 09-13一个简单的 HTTP 调用,为什么时延这么大?
- 09-13阿里云云服务器 ECS和云数据库 PolarDB的简单使用
- 09-1350行代码实现简单的网站服务器 2
- 09-13node.js-带有Nodejs中的Push的Nodejs http2(nginx或其他Web服务器)
- 09-13一个很简单的淘宝优惠券搜索助手 大家看看有没有用吧 下载地址:http://pan.baidu.com/s/1skRHTDF
- 09-13实现一个简单的Http代理服务器
- 09-13【定制开发】【M10】简单Go语言实现Privoxy的读取配置升级:从本地配置文件config.txt 升级成 http get 网络获取
- 09-13图解HTTP (chap 2 简单的Http协议) HTTP方法(2)其他方法
- 09-13Tornado学习笔记12 tornado.httpserver-.非阻塞的Http服务器