ServerSocketChannel服务器

NIO服务器,代码如下:

package com.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
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;
import java.util.Set;

public class NIOServer {
	private Selector selector;
	private static final int BLOCK = 4096;
	private final ByteBuffer sendbuffer = ByteBuffer.allocate(BLOCK);
	private final ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK);

	public NIOServer(int port) {
		try {
			ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
			serverSocketChannel.configureBlocking(false);
			ServerSocket serverSocket = serverSocketChannel.socket();
			serverSocket.bind(new InetSocketAddress(port));
			selector = Selector.open();
			serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void listen() throws IOException {
		while (true) {
			selector.select();
			Set<SelectionKey> selectionKeys = selector.selectedKeys();
			Iterator<SelectionKey> iterator = selectionKeys.iterator();
			while (iterator.hasNext()) {
				SelectionKey selectionKey = iterator.next();
				iterator.remove();
				handleKey(selectionKey);
			}
		}
	}

	private StringBuilder recv = new StringBuilder();

	private void handleKey(SelectionKey selectionKey) throws IOException {
		ServerSocketChannel server = null;
		SocketChannel client = null;
		String receiveText;
		String sendText;
		StringBuilder send = new StringBuilder();
		int count = 0;
		if (selectionKey.isAcceptable()) {
			server = (ServerSocketChannel) selectionKey.channel();
			client = server.accept();
			client.configureBlocking(false);
			client.register(selector, SelectionKey.OP_READ);
		} else if (selectionKey.isReadable()) {
			recv.setLength(0);
			client = (SocketChannel) selectionKey.channel();
			System.out.print("client:");
			while (true) {
				receivebuffer.clear();
				count = client.read(receivebuffer);
				if (count == -1) {
					break;
				}
				receiveText = new String(receivebuffer.array(), 0, count);
				recv.append(receiveText);
				if ("\r\n".equals(receiveText)) {
					break;
				}
				System.out.print(receiveText);
			}
			System.out.println();
			client.register(selector, SelectionKey.OP_WRITE);
		} else if (selectionKey.isWritable()) {
			sendbuffer.clear();
			client = (SocketChannel) selectionKey.channel();
			sendText = "server:";
			send.append(sendText);
			send.append(recv);
			sendbuffer.put(send.toString().getBytes());
			sendbuffer.flip();
			client.write(sendbuffer);
			client.register(selector, SelectionKey.OP_READ);
		}
	}

	public static void main(String[] args) throws IOException {
		int port = 8888;
		NIOServer server = new NIOServer(port);
		server.listen();
	}
}

在客户端用telnet或者nc来连接

telnet 127.0.0.1 8888


你可能感兴趣的:(ServerSocketChannel服务器)