Java網絡編程——使用NIO實現非阻塞Socket通信


       除了普通的Socket與ServerSocket實現的阻塞式通信外,java提供了非阻塞式通信的NIO API。先看一下NIO的實現原理。

 

       從圖中可以看出,服務器上所有Channel(包括ServerSocketChannel和SocketChannel)都需要向Selector註冊,而該Selector則負責監視這些Socket的IO狀態,當其中任意一個或者多個Channel具有可用的IO操作時,該Selector的select()方法將會返回大於0的整數,該整數值就表示該Selector上有多少個Channel具有可用的IO操作,並提供了selectedKeys()方法來返回這些Channel對應的SelectionKey集合。正是通過Selector,使得服務器端只需要不斷地調用Selector實例的select()方法即可知道當前所有Channel是否有需要處理的IO操作。

看個demo

NClient.java

 

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.nio.charset.Charset;
import java.util.Scanner;

public class NClient {
	//定義檢測SocketChannel的Selector對象
	private Selector selector=null;
	//定義處理編碼和解碼的字符集
	private Charset charset=Charset.forName("UTF-8");
	//客戶端SocketChannel
	private SocketChannel sc=null;
	public void init() throws IOException{
		selector=Selector.open();
		InetSocketAddress isa=new InetSocketAddress("127.0.0.1",30000);
		//調用open靜態方法創建連接到指定主機的SocketChannel
		sc=SocketChannel.open(isa);
		//設置該sc以非阻塞方式工作
		sc.configureBlocking(false);
		//將Socketchannel對象註冊到指定Selector
		sc.register(selector, SelectionKey.OP_READ);
		//啓動讀取服務器端數據的線程
		new ClientThread().start();
		//創建鍵盤輸入流
		Scanner scan=new Scanner(System.in);
		while(scan.hasNextLine()){
			//讀取鍵盤輸入
			String line=scan.nextLine();
			//將鍵盤輸入的內容輸出到SocketChannel中
			sc.write(charset.encode(line));
		}
	}
	//定義讀取服務器數據的線程
	private class ClientThread extends Thread{
		public void run(){
			try{
				while(selector.select()>0){
					//遍歷每個有可用IO操作Channel對應的SelectionKey
					for(SelectionKey sk:selector.selectedKeys()){
						//刪除正在處理的SelectionKey
						selector.selectedKeys().remove(sk);
						//如果該SelectionKey對應的Channel中有可讀的數據
						if(sk.isReadable()){
							//使用NIO讀取channel中的數據
							SocketChannel sc=(SocketChannel) sk.channel();
							ByteBuffer buff=ByteBuffer.allocate(1024);
							String content="";
							while(sc.read(buff)>0){
								//sc.read(buff);
								buff.flip();
								content+=charset.decode(buff);
							}
							//打印輸出讀取的內容
							System.out.println("聊天信息"+content);
							//爲下一次讀取做準備
							sk.interestOps(SelectionKey.OP_READ);
						}
					}
				}
			}catch(IOException ex){
				ex.printStackTrace();
			}
		}
	}
	public static void main(String[]args) throws IOException{
		new NClient().init();
	}
}

NServer.java

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

public class NServer {
	//用於檢測所有Channel狀態的Selector
	private Selector selector=null;
	//定義實現編碼、解碼的字符集對象
	private Charset charset=Charset.forName("UTF-8");
	public void init() throws IOException{
		selector=Selector.open();
		//通過open方法來打開一個未綁定的ServerSocketChannel實例
		ServerSocketChannel server=ServerSocketChannel.open();
		InetSocketAddress isa=new InetSocketAddress("127.0.0.1",30000);
		//將該ServerSocketChannel綁定到指定ip地址
		server.socket().bind(isa);
		//設置ServerSocket以非阻塞方式工作
		server.configureBlocking(false);
		//將server註冊到指定Selector對象
		server.register(selector, SelectionKey.OP_ACCEPT);
		while(selector.select()>0){
			//依次處理selector上的每個已選擇的SelectionKey
			for(SelectionKey sk:selector.selectedKeys()){
				//從selector上的已選擇Key集中刪除正在處理的SelectionKey
				selector.selectedKeys().remove(sk);
				//如果sk對應的通信包含客戶端的連接請求
				if(sk.isAcceptable()){
					//調用accept方法接受連接,產生服務器端對應的SocketChannel
					SocketChannel sc=server.accept();
					//設置採用非阻塞模式
					sc.configureBlocking(false);
					sc.register(selector, SelectionKey.OP_READ);
					//將sk對應的Channel設置成準備接受其他請求
					sk.interestOps(SelectionKey.OP_ACCEPT);
				}
				//如果sk對應的通道有數據需要讀取
				if(sk.isReadable()){
					//獲取該SelectionKey對應的Channel,該Channel中有可讀的數據
					SocketChannel sc=(SocketChannel) sk.channel();
					//定義準備之星讀取數據的ByteBuffer
					ByteBuffer buff=ByteBuffer.allocate(1024);
					String content="";
					//開始讀取數據
					try{
						while(sc.read(buff)>0){
							buff.flip();
							content+=charset.decode(buff);
						}
						//打印從該sk對應的Channel裏讀到的數據
						System.out.println("=========="+content);
						//將sk對應的Channel設置成準備下一次讀取
						sk.interestOps(SelectionKey.OP_READ);
						//如果捕捉到該sk對應的channel出現異常,即表明該channel對應的client出現了
						//異常,所以從selector中取消sk的註冊
					}catch(IOException e){
						//從Selector中刪除指定的SelectionKey
						sk.cancel();
						if(sk.channel()!=null){
							sk.channel().close();
						}
					}
					//如果content的長度大於0,即聊天信息不爲空
					if(content.length()>0){
						//遍歷該selector裏註冊的所有SelectKey
						for(SelectionKey key:selector.keys()){
							//選取該key對應的Channel
							Channel targetChannel=key.channel();
							//如果該channel是SocketChannel對象
							if(targetChannel instanceof SocketChannel){
								//將獨到的內容寫入該Channel中
								SocketChannel dest=(SocketChannel) targetChannel;
								dest.write(charset.encode(content));
							}
						}
					}
				}
			}
		}
	}
	public static void main(String[]args) throws IOException{
		new NServer().init();
	}
}


          通過java提供的NIO實現非阻塞Socket通信,大大提高了網絡服務器的性能。

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