NIO

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.soli.util.socketNIO;

import java.net.InetAddress;
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;
import java.util.Set;

/**
 * 
 * @author hadeslee
 */
public class ClientSocketNIO {

public static void main(String[] args) throws Exception {
// 打开一个套接字通道
SocketChannel socketChannel = SocketChannel.open();
// 定义字节缓冲区
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
writeBuffer.put("客户端".getBytes());
// 打开一个选择器
Selector selector = Selector.open();
// 设定为非阻塞
socketChannel.configureBlocking(false);
// 把通道注册到选择器
socketChannel.register(selector, SelectionKey.OP_READ);
// 连接到指定的地址及端口
socketChannel.connect(new InetSocketAddress(InetAddress.getLocalHost(), 9876));
// 完成连接
while (!socketChannel.finishConnect())
;
// 反转缓冲区
writeBuffer.flip();
// 将缓冲区的数据写出
socketChannel.write(writeBuffer);
if (selector.select() > 0) {
// 返回此选择器的已选择键集
Set<SelectionKey> keys = selector.selectedKeys();
// 迭代KEY集合
for (Iterator<SelectionKey> it = keys.iterator(); it.hasNext();) {
SelectionKey key = it.next();
// 判断是否为可读
if (key.isReadable()) {
SocketChannel sc = (SocketChannel) key.channel();
while (true) {
// 清空缓冲区
readBuffer.clear();
int r = sc.read(readBuffer);
if (r <= 0) {
// 关闭套接字通道
sc.close();
// 将该套接字移出键集
key.cancel();
break;
}
System.out.println(new String(readBuffer.array()).trim());
}
}
it.remove();
}
}
}

广州-soli<11722984@163.com>  16:07:33
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.soli.util.socketNIO;

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;
import java.util.Set;

/**
 * 
 * @author hadeslee
 */
public class ServerSocketNIO {

public static void main(String[] args) throws Exception {
// 定义一个字节缓冲区
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
writeBuffer.put("服务器端".getBytes());
// 打开一个服务套接字通道
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
// 绑定端口
serverSocketChannel.socket().bind(new InetSocketAddress(9876));
// 设定为非阻塞
serverSocketChannel.configureBlocking(false);
// 打开一个选择器
Selector selector = Selector.open();
// 把通道注册到选择器
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
// 轮询是否具有KEY需要处理
while (true) {
if (selector.select() > 0) {
// 返回此选择器的已选择键集
Set<SelectionKey> keys = selector.selectedKeys();
// 迭代KEY集合
for (Iterator<SelectionKey> it = keys.iterator(); it.hasNext();) {
SelectionKey key = it.next();
// 判断是否为有连接请求
if (key.isAcceptable()) {
// 建立一个请求套接字通道
SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept();
// 设置为非阻塞
socketChannel.configureBlocking(false);
// 把通道注册到选择器
socketChannel.register(selector, SelectionKey.OP_READ);
// 判断是否为可读
} else if (key.isReadable()) {
SocketChannel sc = (SocketChannel) key.channel();
// 读取数据到指定缓冲区
while (true) {
// 清空缓冲区
readBuffer.clear();
int r = sc.read(readBuffer);
if (r <= 0) {
// 反转缓冲区
writeBuffer.flip();
// 将缓冲区的数据写出
sc.write(writeBuffer);
// 关闭套接字通道
sc.close();
// 将该套接字移出键集
key.cancel();
break;
}
System.out.println(new 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值