java nio

java nio Demo:


NIOServer:

import java.io.IOException;
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 NIOServer {
	private Selector selector;
	
	public void initServer(int port) throws IOException{
		ServerSocketChannel serverChannel=ServerSocketChannel.open();
		serverChannel.configureBlocking(false);
		serverChannel.socket().bind(new InetSocketAddress(port));
		this.selector=Selector.open();
		serverChannel.register(selector, SelectionKey.OP_ACCEPT);
	}
	public void listen() throws IOException{
		System.out.println("服務器啓動");
		while(true){
			selector.select();
			Iterator<SelectionKey> it=this.selector.selectedKeys().iterator();
			while(it.hasNext()){
				SelectionKey key=it.next();
				it.remove();
				if(key.isAcceptable()){
					ServerSocketChannel server= (ServerSocketChannel) key.channel();
					SocketChannel channel=server.accept(); 	
					channel.configureBlocking(false);
					channel.write(ByteBuffer.wrap(new String("向服務端發送一條消息").getBytes()));
					channel.register(selector,SelectionKey.OP_READ);
				}else if(key.isReadable()){
					readKey(key);
				}
			}
		}
	}
	private void readKey(SelectionKey key) throws IOException {
		SocketChannel channel=(SocketChannel) key.channel();
		ByteBuffer buf=ByteBuffer.allocate(256);
		channel.read(buf);
		byte[] msg=buf.array();
		String message=new String(msg).trim();
		System.out.println("服務器收到消息:"+message);
		ByteBuffer backBuf=ByteBuffer.wrap(message.getBytes());
		channel.write(backBuf);
	}
	public static void main(String[] args) throws IOException {
		NIOServer server=new NIOServer();
		server.initServer(8000);
		server.listen();
	}
}


NIOClient:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

public class NIOClient {
	private Selector selector;
	
	public void initClient(String ip,int port) throws IOException{
		SocketChannel channel=SocketChannel.open();
		channel.configureBlocking(false);
		this.selector=Selector.open();
		channel.connect(new InetSocketAddress(ip,port));
		channel.register(selector,SelectionKey.OP_CONNECT);
	}
	public void listen() throws IOException{
		while(true){
			selector.select();
			Iterator<SelectionKey> it=selector.selectedKeys().iterator();
			while(it.hasNext()){
				SelectionKey key=it.next();
				it.remove();
				if(key.isConnectable()){
					SocketChannel channel=(SocketChannel) key.channel();
					if(channel.isConnectionPending()){
						channel.finishConnect();
					}
					channel.configureBlocking(false);
					channel.write(ByteBuffer.wrap(new String("向服務器發送一條信息").getBytes()));
					channel.register(selector,SelectionKey.OP_READ);
				}else if(key.isReadable()){
					read(key);
				}
			}
		}
	}
	private void read(SelectionKey key) throws IOException {
		SocketChannel channel=(SocketChannel) key.channel();
		ByteBuffer buf=ByteBuffer.allocate(256);
		channel.read(buf);
		String str=new String(buf.array()).trim();
		System.out.println("收到服務器信息:"+str);
	}
	public static void main(String[] args) throws IOException {
		NIOClient nioClient=new NIOClient();
		nioClient.initClient("localhost", 8000);
		nioClient.listen();
	}	
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章