参考:http://developer.51cto.com/art/201202/320128.htm
下面代码的基本思想是只用一个线程来一个一个的处理所有的请求。
当服务器正在处理请求时,新到来的连接必须等待直到请求处理完成。然后服务器再次调用accept方法。
package io.multithread;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SingleFileHttpServer02 {
private byte[] content;
private byte[] header;
private int port = 80;
private SingleFileHttpServer02(String data, String encoding,
String MIMEType, int port) throws UnsupportedEncodingException {
this(data.getBytes(encoding), encoding, MIMEType, port);
}
public SingleFileHttpServer02(byte[] data, String encoding, String MIMEType, int port) throws UnsupportedEncodingException {
this.content = data;
this.port = port;
String header = "HTTP/1.0 200 OK\r\n" +
"Server: OneFile 1.0\r\n" +
"Content-length: " + this.content.length + "\r\n" +
"Content-type: " + MIMEType + "\r\n\r\n";
this.header = header.getBytes("ASCII");
}
public void run() {
// System.out.println(Thread.currentThread().getName());
try {
ServerSocket server = new ServerSocket(this.port);
System.out.println("Accepting connections on port " + server.getLocalPort());
System.out.println("Data to be sent:");
System.out.println();
while (true) {
Socket connection = null;
try {
System.out.println(">>>>>等待客户端连接>>>>>");
connection = server.accept();
System.out.println(Thread.currentThread().getName() + "开始处理");
OutputStream out = new BufferedOutputStream(connection.getOutputStream());
InputStream in = new BufferedInputStream(connection.getInputStream());
StringBuffer request = new StringBuffer();
while (true) {
int c = in.read();
if (c == '\r' || c == '\n' || c == -1) {
break;
}
request.append((char) c);
}
//如果检测到是HTTP/1.0及以后的协议,按照规范,需要发送一个MIME首部
if (request.toString().indexOf("HTTP/") != -1) {
out.write(this.header);
}
out.write(this.content);
out.flush();
System.out.println(Thread.currentThread().getName() + "处理结束");
} catch (IOException e) {
} finally {
if (connection != null) {
connection.close();
}
}
}
} catch (IOException e) {
System.err.println("Could not start server. Port Occupied");
}
}
public static void main(String args[]) {
try {
String contentType = "text/plain";
if (args[0].endsWith(".html") || args[0].endsWith(".htm")) {
contentType = "text/html";
}
//从本地文件读取发送的内容
InputStream in = new FileInputStream(args[0]);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
byte[] data = out.toByteArray();
//设置监听端口
int port;
try {
port = Integer.parseInt(args[1]);
if (port < 1 || port > 65535) {
port = 8080;
}
} catch (Exception e) {
port = 8080;
}
String encoding = "ASCII";
if (args.length > 2) {
encoding = args[2];
}
SingleFileHttpServer02 t = new SingleFileHttpServer02(data, encoding, contentType, port);
/**
* 在main线程启动http-server
*/
t.run();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Usage:java SingleFileHTTPServer filename port encoding");
} catch (Exception e) {
System.err.println(e);// TODO: handle exception
}
}
}
从浏览器请求localhost加对应的端口,部分运行结果:
>>>>>等待客户端连接>>>>>
main开始处理
main处理结束
>>>>>等待客户端连接>>>>>
main开始处理
main处理结束
>>>>>等待客户端连接>>>>>
main开始处理
main处理结束
>>>>>等待客户端连接>>>>>
main开始处理
main处理结束
>>>>>等待客户端连接>>>>>
===========END===========