ServerSocket服务器

HttpServer.java

package cn.hackcoder.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.ServerSocket;
import java.net.Socket;

import cn.hackcoder.model.Request;
import cn.hackcoder.model.Response;

public class HttpServer {

	private static final String WEB_ROOT = System.getProperty("user.dir")
			+ File.separator + "webapp";
	private static final String SHUT_DWON = "/shutdown";
	private ServerSocket server;
	private Socket client = null;
	private Request request = new Request();
	private Response response = new Response();
	private InputStream inputStream;
	private OutputStream outputStream;
	HttpServer(int port, int backlog, String serverName) throws IOException {
		server = new ServerSocket(port, backlog,
				Inet4Address.getByName(serverName));
	}

	public void execute() throws IOException {
		while (true) {
			Socket client = server.accept();
			inputStream = client.getInputStream();
			outputStream = client.getOutputStream();
			request.setInputStream(inputStream);
			String fileName = request.parseUri();
			response.setOutputStream(client.getOutputStream());
			response.sendResponse(WEB_ROOT, fileName);
			if(inputStream!=null){
				inputStream.close();
			}
			if(outputStream!=null){
				inputStream.close();
			}
		}
	}

	public static void main(String[] args) {
		try {
			HttpServer httpSever = new HttpServer(8888, 10, "127.1.1.111");
			httpSever.execute();
			httpSever.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			if (server != null && !server.isClosed())
				server.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
Request

package cn.hackcoder.model;

import java.io.IOException;
import java.io.InputStream;

public class Request {
	private InputStream inputStream = null;
	private byte[] bytes = new byte[2048];

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	/**
	 * 返回解析的uri
	 * 
	 * @return
	 * @throws IOException
	 */
	public String parseUri() throws IOException {
		StringBuffer sb = new StringBuffer();
		int ch = inputStream.read(bytes);
		for (int i = 0; i < ch; i++) {
			sb.append((char) bytes[i]);
		}
		int index1 = sb.indexOf(" ");// 获取请求资源
		int index2 = sb.substring(index1 + 1).indexOf(" ");
		if (index1 < index2) {
			return sb.substring(index1 + 1, index1 + index2 + 1);
		}
		return null;
	}
}

Response

package cn.hackcoder.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class Response {
	private OutputStream outputStream;
	private byte[] bytes = new byte[2048];

	public OutputStream getOutputStream() {
		return outputStream;
	}

	public void setOutputStream(OutputStream outputStream) {
		this.outputStream = outputStream;
	}

	/**
	 * 响应消息
	 * 
	 * @throws IOException
	 */
	public void sendResponse(String location, String fileName)
			throws IOException {
		if(location==null||fileName==null){
			return;
		}
		File file = new File(location, fileName);
		if (!file.exists()) {
			PrintStream printStream = new PrintStream(getOutputStream());
			printStream.print("<h1>ERROR</h1>");
			printStream.close();
		}
		// 读取文件内容,用于返回响应
		InputStream inputStream = new FileInputStream(file);
		int len = inputStream.read(bytes);
		while (len != -1) {
			outputStream.write(bytes, 0, len);
			len = inputStream.read(bytes);
		}
		if (inputStream != null) {
			inputStream.close();
		}
		if (outputStream != null) {
			outputStream.close();
		}

	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值