
public class non_blocking_nio {
/**
* 上传文件
*/
@Test
public void client(){
SocketChannel socketchannel = null;
FileChannel fchannel = null;
try {
socketchannel = SocketChannel.open(new InetSocketAddress("localhost",1001));
socketchannel.configureBlocking(false);//设置成非阻塞
//fchannel = FileChannel.open(Paths.get("C:\\Users\\郭赛\\Desktop\\angle王鸥\\鸥茉莉.jpg"), StandardOpenOption.READ);
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.put("hello".getBytes());
buf.flip();
socketchannel.write(buf);
buf.clear();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(socketchannel == null) {
try {
socketchannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server(){
//SocketChannel schannel = null;
Iterator<SelectionKey> it = null;
try {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ssc.bind(new InetSocketAddress(1001));
//SocketChannel socketchannel = ssc.accept();
//FileChannel fchannel = FileChannel.open(Paths.get("C:\\Users\\郭赛\\Desktop\\鸥茉莉001.jpg"),StandardOpenOption.WRITE,StandardOpenOption.CREATE);
Selector selector = Selector.open();
//设置监听
//int opAccept = SelectionKey.OP_ACCEPT;
ssc.register(selector,SelectionKey.OP_ACCEPT);
//开启轮询
while(selector.select() > 0){
Set<SelectionKey> selectionKeys = selector.selectedKeys();//获取当前所有监听事件,每一次都不同
//取出每一个监听事件,因为在监听的过程中可能会有其他事件来所以采用迭代的方法
it = selectionKeys.iterator();
while(it.hasNext()){
SelectionKey skey = it.next();//获取到监听事件
if(skey.isAcceptable()){
//获取socketchannel,连接和客户端通道
SocketChannel schannel = ssc.accept();
//设置为非阻塞
schannel.configureBlocking(false);
//修改监听模式为读就绪
schannel.register(selector,SelectionKey.OP_READ);
}
else if(skey.isReadable()){
//获取channel
SocketChannel schannel = (SocketChannel) skey.channel();
ByteBuffer buf = ByteBuffer.allocate(1024);
//开始通信
int len;
while((len = schannel.read(buf)) > 0){
buf.flip();
System.out.println(new String(buf.array(),0,len));
//System.out.println(schannel.read(buf)
//初始化,能放更多数据
buf.clear();
}
//schannel.write("上传完成".getBytes());
}
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
it.remove();
}
}
}
/**
* 上传文件
*/
@Test
public void client(){
SocketChannel socketchannel = null;
FileChannel fchannel = null;
try {
socketchannel = SocketChannel.open(new InetSocketAddress("localhost",1001));
socketchannel.configureBlocking(false);//设置成非阻塞
//fchannel = FileChannel.open(Paths.get("C:\\Users\\郭赛\\Desktop\\angle王鸥\\鸥茉莉.jpg"), StandardOpenOption.READ);
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.put("hello".getBytes());
buf.flip();
socketchannel.write(buf);
buf.clear();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(socketchannel == null) {
try {
socketchannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server(){
//SocketChannel schannel = null;
Iterator<SelectionKey> it = null;
try {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ssc.bind(new InetSocketAddress(1001));
//SocketChannel socketchannel = ssc.accept();
//FileChannel fchannel = FileChannel.open(Paths.get("C:\\Users\\郭赛\\Desktop\\鸥茉莉001.jpg"),StandardOpenOption.WRITE,StandardOpenOption.CREATE);
Selector selector = Selector.open();
//设置监听
//int opAccept = SelectionKey.OP_ACCEPT;
ssc.register(selector,SelectionKey.OP_ACCEPT);
//开启轮询
while(selector.select() > 0){
Set<SelectionKey> selectionKeys = selector.selectedKeys();//获取当前所有监听事件,每一次都不同
//取出每一个监听事件,因为在监听的过程中可能会有其他事件来所以采用迭代的方法
it = selectionKeys.iterator();
while(it.hasNext()){
SelectionKey skey = it.next();//获取到监听事件
if(skey.isAcceptable()){
//获取socketchannel,连接和客户端通道
SocketChannel schannel = ssc.accept();
//设置为非阻塞
schannel.configureBlocking(false);
//修改监听模式为读就绪
schannel.register(selector,SelectionKey.OP_READ);
}
else if(skey.isReadable()){
//获取channel
SocketChannel schannel = (SocketChannel) skey.channel();
ByteBuffer buf = ByteBuffer.allocate(1024);
//开始通信
int len;
while((len = schannel.read(buf)) > 0){
buf.flip();
System.out.println(new String(buf.array(),0,len));
//System.out.println(schannel.read(buf)
//初始化,能放更多数据
buf.clear();
}
//schannel.write("上传完成".getBytes());
}
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
it.remove();
}
}
}
哪位大哥能不能运行一下我这个程序,一直提示 java.nio.channels.IllegalBlockingModeException但是找不到原因
[此贴子已经被作者于2020-6-4 22:11编辑过]