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的更多相关文章

  1. 解决Exception in thread "main" java.nio.BufferOverflowException报错

    学习bytebuffer时,写了简单的demo报错: 错误的提示:Exception in thread "main" java.nio.BufferOverflowExcepti ...

  2. 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 ...

  3. 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" ...

  4. Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V

    在学习CGlib动态代理时,遇到如下错误: Exception in thread "main" java.lang.NoSuchMethodError: org.objectwe ...

  5. GUI学习中错误Exception in thread "main" java.lang.NullPointerException

    运行时出现错误:Exception in thread "main" java.lang.NullPointerException 该问题多半是由于用到的某个对象只进行了声明,而没 ...

  6. 执行打的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 ...

  7. Exception in thread "main" java.lang.ExceptionInInitializerError

    Exception in thread "main" java.lang.ExceptionInInitializerErrorCaused by: java.util.Missi ...

  8. 编译运行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 ...

  9. dom4j使用xpath报异常 Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext

    Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext      ...

随机推荐

  1. html基础整理(02浮动 问题)

    margin的塌陷现象及解决方案    如果一个大盒子中包含一个小盒子,给小盒子设置margin-top,大盒子会一起向下平移 - 解决方案: 1. 给大盒子加一个边框(border) 2. 给大盒子 ...

  2. C++与C的区别二

    1. new,delete的局部重载: #include <iostream> using namespace std; ; class myclass { public: myclass ...

  3. time 模块学习

    前情提要: time模块是经常使用的模块.主要是用来记录时间,以及时间上的相关操作 一:时间戳 1:第一种形式 import time print(time.time()) 从1970 1 1 0:0 ...

  4. MFC Windows程序开发究竟是什么?

    MFC Windows程序开发究竟是什么? MFC是一个Application Framework.他是一个完整的程序模型. 什么是MFC?MFC是微软公司出品一个Application Framew ...

  5. K8s的POD连接数据库时报错

    [root@cccc xxxx]# ./showlog.sh dr iff-dr-1128668949-lb90g 2017-09-29 03:21:57,575 INFO [org.wildfly. ...

  6. QuantLib 金融计算——收益率曲线之构建曲线(2)

    目录 QuantLib 金融计算--收益率曲线之构建曲线(2) YieldTermStructure 问题描述 Piecewise** 分段收益率曲线的原理 Piecewise** 对象的构造 Fit ...

  7. L05-Linux部署msmtp+mutt发送邮件

    一.前言 首先,得明白发送一封邮件的流程,下面一段理论摘抄自廖雪峰的官网网站https://www.liaoxuefeng.com/article/00137387674890099a71c04005 ...

  8. 禅道与bugfree区别

    bugfree更偏重bug管理, 禅道是以项目管理为目标, 所以出发点不一样. 我们bug追踪管理使用bugfree,项目管理计划使用禅道,配置管理使用svn bugfree很low,安装需要环境特别 ...

  9. bzoj4361:isn(dp+容斥+树状数组)

    题面 darkbzoj 题解 \(g[i]\)表示长度为\(i\)的非降序列的个数 那么, \[ ans = \sum_{i=1}^{n}g[i]*(n-i)!-g[i+1]*(n-i-1)!*(i+ ...

  10. P1525 关押罪犯 题解

    #include<iostream> #include<cstdio> #include<algorithm> using namespace std; //带边权 ...