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 ...
随机推荐
- [HTML] <meta name="viewport" content="width=device-width,initial-scale=1.0">释义
<meta name="viewport" content="width=device-width,initial-scale=1.0">这是 HT ...
- VMware Workstation 14 pro License Keys
AC5XK-0ZD4H-088HP-9NQZV-ZG2R4 CG54H-D8D0H-H8DHY-C6X7X-N2KG6 ZC3WK-AFXEK-488JP-A7MQX-XL8YF ZC5XK-A6E0 ...
- sqlplus登录远程数据库与数据导出
一.登录 1.cmd中输入sqlplus /nolog 2.链接数据库,root是用户名,root123是密码,ORCL是数据库名.conn root/root123@192.168.1.27:152 ...
- jenkins+appium android app自动化测试
jenkins安装 pytest+jenkins安装+allure报告 新建任务 其他默认,保存 立即构建 test_login.py from src.pages import login_page ...
- Python unittest第二篇:测试夹具
关于测试夹具,我们知道,以类为对象的话,在python里对应的方法分别是test_isupper.test_upper, 那么以测试case为单位呢? 这时候,就要提到我们的setup()和tearD ...
- dubbo集群容错之LoadBalance
原文地址:Dubbo 源码分析 - 集群容错之 LoadBalance dubbo 提供了4种负载均衡实现,分别是基于权重随机算法的 RandomLoadBalance.基于最少活跃调用数算法的 Le ...
- 一段自用javascript代码
function jsontoarray(mjson) { var arr = []; ; for(var x in mjson.data){ arr[i] = new Array(); arr[i] ...
- 使用redux-devtools工具
在vue中型项目开发的过程中,一般都是要用到vuex这个状态管理工具的,这样可以方便我们管理全局的状态,同时,为了在开发的过程中,更加方便地实时查看到state状态,我们会使用 vue-devtool ...
- JS框架设计之命名空间设计一种子模块
命名空间 1.种子模块作为一个框架的最开始,除了负责初始化框架的最基础部分. 2.种子模块作为框架的最开始,那么什么是种子框架的最开始呢?答案是IIFE(立即调用函数表达式); IIFE(立即调用函数 ...
- Asp.Net Cache缓存技术学习
本文参考自Fish Li的细说 ASP.NET Cache 及其高级用法 一.前言,相信大多数做网站开发的都知道缓存技术对于网站的重要性,它对于网站的性能优化起着至关重要的作用. 关于缓存的技术大致有 ...