JAVA NIO Server Demo

文件NIO例子:

package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/*******************************
 * 
 * 文件读取Demo
 * @author maoman
 *
 ********************************/
public class Filenio {
    
	public static void main(String args[]) throws IOException{
		
		File file=new File("/home/maoman/test.sh");
		FileInputStream inputStream=new FileInputStream(file);
		FileChannel channel = inputStream.getChannel();
		
		ByteBuffer byteBuffer=ByteBuffer.allocate(20);
		int size=channel.read(byteBuffer);

		System.out.println(new String(byteBuffer.array()));

		
		channel.close();
		inputStream.close();
	
		
	}
}


服务器NIO

使用例子,SocketChannel

NIO Client:

package com.test;

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 {

	public static void main(String args[]) throws IOException {
		
		SocketChannel channel = SocketChannel.open(); //打开Channel
		channel.configureBlocking(false);
		channel.connect(new InetSocketAddress("127.0.0.1", 8085)); //连接
		
		Selector selector = Selector.open();    //打开Selector
		channel.register(selector, SelectionKey.OP_CONNECT); //注册OP_CONNECT
		
		while (true) {
			selector.select();  //	轮询
			Iterator iterator = selector.selectedKeys().iterator(); //获取可读的
			while (iterator.hasNext()) {
				
				SelectionKey key = (SelectionKey) iterator.next();
				iterator.remove();

				if (key.isConnectable()) {
					Handle(key, selector);
				}
			}
		}
	}
	
	public static void Handle(SelectionKey key, Selector sel)
			throws IOException {

		SocketChannel client = (SocketChannel) key.channel();
		
		if (client.isConnectionPending()) {
			if (client.finishConnect()) {

				ByteBuffer byteBuffer = ByteBuffer.allocate(200);
				byteBuffer = ByteBuffer.wrap(new String("hello server")
						.getBytes());
				client.write(byteBuffer);
				client.register(sel, SelectionKey.OP_READ);
			}
		} else if (key.isReadable()) {

		}

	}
}

 NIO Server:

  

package com.test;

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 {
	public static void main(String args[]) throws IOException {
		
		ServerSocketChannel channel = ServerSocketChannel.open();
		channel.socket().bind(new InetSocketAddress(8085), 10);
		channel.configureBlocking(false);
		Selector selector = Selector.open();
		
		channel.register(selector, SelectionKey.OP_ACCEPT);
		
		while (true) {
		    
			selector.select();
			Iterator<?> iterator = selector.selectedKeys().iterator();
			
			while (iterator.hasNext()) {
				
				SelectionKey key = (SelectionKey) iterator.next();
				iterator.remove(); // 删除此消息
				handlekey(key, selector);
				
				
			}

		}
	}
	
	public static void handlekey(SelectionKey key,Selector selector) throws IOException {
		
		ServerSocketChannel server = null;
		SocketChannel client = null;
		
		if (key.isAcceptable()) {
			
			System.out.println("Acceptable");
			server = (ServerSocketChannel) key.channel();
			client = server.accept();// 接受连接请求
			client.configureBlocking(false);
			client.register(selector, SelectionKey.OP_READ);
			
		} else if (key.isReadable()) {
			
			client = (SocketChannel) key.channel();  
			//
			ByteBuffer byteBuffer = ByteBuffer.allocate(200);
			int count=client.read(byteBuffer);
			
			if(count>0){
				
				System.out.println("Readable");
				System.out.println(new String(byteBuffer.array()));	
				
			}else if(count ==-1){
				key.cancel();  //移除
				return;
			}
			
			
		}

	}
}


你可能感兴趣的:(JAVA NIO Server Demo)