Exception in thread "main" java.nio.channels.NotYetConnectedException
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import java.nio.ByteBuffer; public class SimpleAIOServer{
static final int PORT = 30000;
public static void main(String[] args) throws Exception{
try(
//创建AsynchronousServerSocketChannel对象
AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open()
){
//指定在指定地址、端口监听
serverChannel.bind(new InetSocketAddress(PORT));
while(true){
//采用循环接受来自客户端的连接
Future<AsynchronousSocketChannel> future = serverChannel.accept();
//获取连接完成后返回的AsynchronousSocketChannel
AsynchronousSocketChannel socketChannel = future.get();
//执行输出
socketChannel.write(ByteBuffer.wrap("欢迎你来到AIO的世界!".getBytes("UTF-8"))).get();
}
}
}
}
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.charset.Charset;
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import java.nio.ByteBuffer; public class SimpleAIOClient{
static final int PORT = 30000;
public static void main(String[] args) throws Exception{
//用于读取数据的ByteBuffer
ByteBuffer buff = ByteBuffer.allocate(1024);
Charset utf = Charset.forName("utf-8");
try(
//创建AsynchronousSocketChannel对象
AsynchronousSocketChannel clientChannel = AsynchronousSocketChannel.open()
){
//连接远程服务器
clientChannel.connect(new InetSocketAddress("127.0.0.1", PORT));
buff.clear();
//从clientChannel中读取数据
clientChannel.read(buff).get();
buff.flip();
//将buff中的内容转换为字符串
String content = utf.decode(buff).toString();
System.out.println("服务器信息:" + content);
}
}
}

运行上述代码会出现Exception in thread "main" java.nio.channels.NotYetConnectedException并提示在at SimpleAIOClient.main(SimpleAIOClient.java:21)报错。
NotYetConnectedException是尚未连接的错误,代码出错原因:
客户端和服务端没有建立连接就执行了Socket通信,代码上的错误位置是:
SimpleAIOClient.java中第18行:
//连接远程服务器
clientChannel.connect(new InetSocketAddress("127.0.0.1", PORT));
因为这一行没有检查异步IO操作是否完成,只有异步IO操作完成客户端和服务端的连接才能建立。
而异步IO操作是否完成的标志是clientChannel有一个Future返回值,得到它才能确保异步IO操作执行完成:
//连接远程服务器
clientChannel.connect(new InetSocketAddress("127.0.0.1", PORT)).get();//得到Future返回值,否则连接不会建立。
Exception in thread "main" java.nio.channels.NotYetConnectedException的更多相关文章
- 解决Exception in thread "main" java.nio.BufferOverflowException报错
学习bytebuffer时,写了简单的demo报错: 错误的提示:Exception in thread "main" java.nio.BufferOverflowExcepti ...
- Jenkins的slave异常:Exception in thread "main" java.lang.ClassNotFoundException: hudson.remoting.Launcher
当任务分配到slave上执行时,报如下错误: Parsing POMs Established TCP socket on 38257 maven33-agent.jar already up to ...
- Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
学习架构探险,从零开始写Java Web框架时,在学习到springAOP时遇到一个异常: "C:\Program Files\Java\jdk1.7.0_40\bin\java" ...
- Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
在学习CGlib动态代理时,遇到如下错误: Exception in thread "main" java.lang.NoSuchMethodError: org.objectwe ...
- GUI学习中错误Exception in thread "main" java.lang.NullPointerException
运行时出现错误:Exception in thread "main" java.lang.NullPointerException 该问题多半是由于用到的某个对象只进行了声明,而没 ...
- 执行打的maven jar包时出现“Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for ...
- Exception in thread "main" java.lang.ExceptionInInitializerError
Exception in thread "main" java.lang.ExceptionInInitializerErrorCaused by: java.util.Missi ...
- 编译运行java程序出现Exception in thread "main" java.lang.UnsupportedClassVersionError: M : Unsupported major.minor version 51.0
用javac编译了一个M.java文件, 然后用java M执行,可是出现了下面这个错误. Exception in thread "main" java.lang.Unsuppo ...
- dom4j使用xpath报异常 Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext ...
随机推荐
- 如何在CentOS 7安装Node.js
最近,我一直对学习Node.js比较感兴趣.这是一个Java平台的服务器端编程 ,它允许开发人员在服务器编写Java代码,并且有许多CentOS的用户正努力学习这个语言的开发环境.这正是我想做这个教程 ...
- mybatis pagehelper多数据源配置的坑
我用spring boot配置了2个数据源的工程用来同步不同库的数据,发现如果配置成如下格式报错 #分页配置pagehelper: helper-dialect: mysql reasonable: ...
- 把序列中的N个元素赋值给多个变量
说明: python中序列和散列 序列:可通过偏移量来进行切片的对象.列表.元组.字符串都属于序列. 散列:无法通过偏移量来进行切片的对象.比如 集合.字典 一. 变量的数量跟序列元素的个数一样 1. ...
- datetime 模块
datetimo 模块和time模块类似,只不过直接帮你定好了格式 import datetime time =datetime.datetime.now() print(time,type(time ...
- Python中 '==' 与'is' 以及它们背后的故事
摘要 比较判断逻辑是在代码中经常使用的,在Python中常用 '==' 和 is 来做比较判断. == : 双等号是用来比较变量所指向内存单元中的值是否相等,它只关心值,并不在意值的内存地址,也就 ...
- python中json库中的load、loads、dump、dumps的区别与用法
一.json.dumps(i): json中的dumps方法是用来将特定格式的数据进行字符串化的操作,比如列表字典都可以进行字符串化操作然后写入json的file:而且如果是要写入json文件就必须要 ...
- 洛谷P3830 [SHOI2012]随机树(期望dp)
题面 luogu 题解 第一问: 设\(f[i]\)表示\(i\)步操作后,平均深度期望 \(f[i] = \frac {f[i - 1] * (i - 1)+f[i-1]+2}{i}=f[i-1]+ ...
- [DPF] DB2 DPF 搭建实战
1. Server 准备 2. NFS 系统设置 3. 创建实例 4. rsh/ssh 5. 测试 Server: 192.168.122.1 dpf01.dpf.com dpf01 192.16 ...
- 【Eclipse】编译使用Makefile的C工程
创建MakeFile project新建src文件夹,将文件复制到里面.右击makefile,make targets->create->名称填上allmake targets->b ...
- 高性能队列Disruptor的使用
一.什么是 Disruptor 从功能上来看,Disruptor 是实现了"队列"的功能,而且是一个有界队列.那么它的应用场景自然就是"生产者-消费者"模型的应 ...