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 ...
随机推荐
- Kali Linux来袭~老司机带你进击
Kali是BackTrackLinux完全遵循Debian开发标准彻底的完全重建.全新的目录框架,复查并打包所有工具,我们还为VCS建立了Git树. 本次推荐内容主要介绍Kali-Linux的安装,包 ...
- mfix中统计气泡体积
先转换为point data 提取空隙率在0.45-1.0之间的网格,为后面提取气泡内网格做准备 把free board部分去掉 然后积分 选择cell data后就得到气泡内所有网格的体积和,如果网 ...
- 【算法笔记】A1022 Digital Library
题意 输入n本书的信息:id,书名,作者,关键字,出版社,出版年份.搜索图书,输出id. 思路 定义5个map<string, set<int> >,分别存放Title, Au ...
- (转)如何在CentOS / RHEL 7上安装Elasticsearch,Logstash和Kibana(ELK)
原文:https://www.howtoing.com/install-elasticsearch-logstash-and-kibana-elk-stack-on-centos-rhel-7 如果你 ...
- Getway网关管理ZUUL
1.ZUUL微服务网关 微服务架构体系中,通常一个业务系统会有很多的微服务,比如:OrderService.ProductService.UserService...,为了让调用更简单,一般会在这些服 ...
- codeblocks c++ 编译出错
codeblocks编译出错 今天编译一个c++程序调用模板的时候,出现错误 error This file requires compiler and library support for the ...
- android开发之提高应用启动速度_splash页面瞬间响应_避免APP启动闪白屏
Application和Activity中的onCreate都进行了优化,基本没有耗时操作,但是启动应用之后还是会闪现一下白色背景,然后才进入Splash页面,对比了一下QQ.微信.微博等客户端,点击 ...
- Table '.\gts\eventdata#P#p0' is marked as crashed and last (automatic?) repair failed
修复数据表操 MYSQL数据表出现问题,提示:Error: Table './db_name/table_name' is marked as crashed and last (automatic? ...
- python-Event事件线程同步和互斥
#!/usr/bin/python #coding=utf-8 #用于线程间通信,通过事件标识控制 import threading from time import sleep,ctime def ...
- JavaScript数据结构-1.数组
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...