NIO socket服务端

public class ZZSocketServer extends Thread {

   private final int PORT=8899;
   private final int BLOCK=1024*2;
   /*接受数据缓冲区*/
    private  ByteBuffer sendbuffer = ByteBuffer.allocate(BLOCK);
	/*发送数据缓冲区*/
	private  ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK);

   private  Selector selector;
   
   
   public ZZSocketServer(){
	    // 打开服务器套接字通道
		ServerSocketChannel serverSocketChannel;
		try {
			serverSocketChannel = ServerSocketChannel.open();
			// 服务器配置为非阻塞
			serverSocketChannel.configureBlocking(false);
			// 检索与此通道关联的服务器套接字
			ServerSocket serverSocket = serverSocketChannel.socket();
			// 进行服务的绑定
			serverSocket.bind(new InetSocketAddress(PORT));
			// 通过open()方法找到Selector
			selector = Selector.open();
			// 注册到selector,等待连接
			serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
			System.out.println("Server 8899 Port Start----"+PORT);
		} catch (IOException e) {
			//e.printStackTrace();
		}
		
   }
   
   public void run(){
	   
	   while(true){
		   try {
			selector.select();
			// 返回此选择器的已选择键集。
			Set<SelectionKey> selectionKeys = selector.selectedKeys();
			System.out.println("===selectionKeys.Size=="+selectionKeys.size());
			// 接受请求
			ServerSocketChannel server = null;
			SocketChannel client = null;
			for (SelectionKey selectionKey : selectionKeys) {
				if (selectionKey.isAcceptable()) {
					// 返回为之创建此键的通道。
					server = (ServerSocketChannel) selectionKey.channel();
					// 接受到此通道套接字的连接。
					// 此方法返回的套接字通道(如果有)将处于阻塞模式。
					client = server.accept();
					// 配置为非阻塞
					client.configureBlocking(false);
					// 注册到selector,等待连接
					client.register(selector, SelectionKey.OP_READ);
			    }
				else if (selectionKey.isReadable()){
					handleKey(selectionKey);
				}
			}
			/*Iterator<SelectionKey> iterator = selectionKeys.iterator();
			while (iterator.hasNext()) {		
				SelectionKey selectionKey = iterator.next();
				//iterator.remove();
				handleKey(selectionKey);
			}*/
			selectionKeys.clear();
		} catch (IOException e) {
			//e.printStackTrace();
		}
	   }
   }
   
   public void handleKey(SelectionKey selectionKey)throws IOException{
	   System.out.println("=========handle=========");
		// 接受请求
		ServerSocketChannel server = null;
		SocketChannel client = null;
		String receiveText;
		String sendText;
		int count=0;
		// 测试此键的通道是否已准备好接受新的套接字连接。
		/*if (selectionKey.isAcceptable()) {
			// 返回为之创建此键的通道。
			server = (ServerSocketChannel) selectionKey.channel();
			// 接受到此通道套接字的连接。
			// 此方法返回的套接字通道(如果有)将处于阻塞模式。
			client = server.accept();
			// 配置为非阻塞
			client.configureBlocking(false);
			// 注册到selector,等待连接
			client.register(selector, SelectionKey.OP_READ);
		} else */
			
			if (selectionKey.isReadable()) {
			// 返回为之创建此键的通道。
			client = (SocketChannel) selectionKey.channel();
			//将缓冲区清空以备下次读取
			receivebuffer.clear();
			//读取服务器发送来的数据到缓冲区中
			count = client.read(receivebuffer);	
			if (count > 0) {
				receiveText = new String( receivebuffer.array(),0,count);
				System.out.println("服务器端接受客户端数据--:"+receiveText.trim());
				Client.list.add(selectionKey);
				//将缓冲区清空以备下次写入
				sendbuffer.clear();
				// 返回为之创建此键的通道。
				client = (SocketChannel) selectionKey.channel();
				String res="abc";
				sendbuffer.allocate(res.length());
				//向缓冲区中输入数据
				sendbuffer.put(res.getBytes());
				 //将缓冲区各标志复位,因为向里面put了数据标志被改变要想从中                                读取数据发向服务器,就要复位
				sendbuffer.flip();
				//输出到通道
				client.write(sendbuffer);
				System.out.println("服务器端向客户端发送数据--:"+res);
				//client.register(selector, SelectionKey.OP_WRITE);
			}
		} else if (selectionKey.isWritable()) {
			System.out.println("==========writable=====");
			//将缓冲区清空以备下次写入
			sendbuffer.clear();
			// 返回为之创建此键的通道。
			client = (SocketChannel) selectionKey.channel();
			String res="abc";
			sendbuffer.allocate(res.length());
			//向缓冲区中输入数据
			sendbuffer.put(res.getBytes());
			 //将缓冲区各标志复位,因为向里面put了数据标志被改变要想从中读取数据发向服务器,就要复位
			sendbuffer.flip();
			//输出到通道
			client.write(sendbuffer);
			System.out.println("服务器端向客户端发送数据--:"+res);
			//client.register(selector, SelectionKey.OP_READ);
		}
   }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值