java nio http服务器(1)读取http请求

通过nio读取http的头信息。直接看代码:

package com.hcserver.conn;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

public class Server {

	public void start() throws Exception {
		Selector selector = Selector.open();
		ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
		serverSocketChannel.configureBlocking(false);
		serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
		serverSocketChannel.socket().setReuseAddress(true);
		serverSocketChannel.socket().bind(new InetSocketAddress(8084));
		while(true){
			while (selector.select() > 0) {
				Iterator<SelectionKey> selectedKeys = selector.selectedKeys() .iterator();
				while (selectedKeys.hasNext()) {
					SelectionKey key = selectedKeys.next();
					if (key.isAcceptable()) {
						ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
						SocketChannel channel = ssc.accept();
						if(channel != null){
							channel.configureBlocking(false);
							channel.register(selector, SelectionKey.OP_READ);// 客户socket通道注册读操作
						}
					} else if (key.isReadable()) {
						SocketChannel channel = (SocketChannel) key.channel();
						channel.configureBlocking(false);
						String receive = receive(channel);
						BufferedReader b = new BufferedReader(new StringReader(receive));

						String s = b.readLine();
						while (s !=null) {
							System.out.println(s);
							s = b.readLine();
						}
						b.close();
						channel.register(selector, SelectionKey.OP_WRITE);
					} else if (key.isWritable()) {
						SocketChannel channel = (SocketChannel) key.channel();
						String hello = "hello world...";
						ByteBuffer buffer = ByteBuffer.allocate(1024);
						
						byte[] bytes = hello.getBytes();
						buffer.put(bytes);
						buffer.flip();
						channel.write(buffer);
						channel.shutdownInput();
						channel.close();
					}
				}
			}
		}
	}

	// 接受数据
	private String receive(SocketChannel socketChannel) throws Exception {
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		byte[] bytes = null;
		int size = 0;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		while ((size = socketChannel.read(buffer)) > 0) {
			buffer.flip();
			bytes = new byte[size];
			buffer.get(bytes);
			baos.write(bytes);
			buffer.clear();
		}
		bytes = baos.toByteArray();

		return new String(bytes);
	}

}
package com.hcserver;

import com.hcserver.conn.Server;

public class ServerStartUp {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		Server server = new Server();
		
		server.start();
		
	}

}


运行代码,然后打开浏览器在地址栏输入:http://localhost:8084/hello?aa=1&bb=2,
回车后浏览器显示hello world 。而后台则输出http请求头信息:如下所示:

GET /hello?aa=1&bb=2 HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)
Accept-Encoding: gzip, deflate
Host: localhost:8084
DNT: 1
Connection: Keep-Alive


 


 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值