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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值