1、测试代码来自于 JDK7 AIO初体验 http://www.iteye.com/topic/1113611

  1.1、

package aio;

import java.net.InetSocketAddress;
import java.nio.*;
import java.nio.channels.*;
import java.util.concurrent.*; public class TaioServer
{
public final static int PORT = 9888;
private AsynchronousServerSocketChannel FasyncServer; public TaioServer() throws Exception
{
FasyncServer = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(PORT));
} // Future方式
public void StartWithFuture() throws Exception
{
System.out.println("Server listen on " + PORT);
Future<AsynchronousSocketChannel> future = FasyncServer.accept();
AsynchronousSocketChannel socket = future.get();
ByteBuffer readBuf = ByteBuffer.allocate(1024);
readBuf.clear();
socket.read(readBuf).get(100, TimeUnit.SECONDS);
readBuf.flip();
System.out.printf("received message:" + new String(readBuf.array()));
System.out.println(Thread.currentThread().getName());
} // CompletionHandler方式
public void StartWithCompletionHandler() throws Exception
{
System.out.println("Server listen on " + PORT);
//注册事件和事件完成后的处理器
FasyncServer.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>()
{
final ByteBuffer buffer = ByteBuffer.allocate(1024); public void completed(AsynchronousSocketChannel _rstAsyncSocketChannel, Object _attachment)
{
System.out.println(Thread.currentThread().getName());
System.out.println("start");
try
{
buffer.clear();
System.out.println("TimeUnit.SECONDS : "+TimeUnit.SECONDS);
int iRst = _rstAsyncSocketChannel.read(buffer).get(100, TimeUnit.SECONDS);
System.out.println("iRst : "+iRst);
buffer.put(iRst, (byte)0);
buffer.flip();
System.out.println("received message: "+ new String(buffer.array(), 0, iRst));
} catch (InterruptedException | ExecutionException e) {
//System.out.println(e.toString());
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} finally {
try
{
_rstAsyncSocketChannel.close();
FasyncServer.accept(null, this);
} catch (Exception e) {
//System.out.println(e.toString());
e.printStackTrace();
}
}
System.out.println("end");
} // completed(...) @Override
public void failed(Throwable exc, Object attachment)
{
System.out.println("failed: " + exc);
}
}); // FasyncServer.accept(...) // 主线程继续自己的行为
while (true)
{
System.out.println("main thread");
Thread.sleep(1000);
}
} public static void main(String args[]) throws Exception
{
System.out.println("main in <<==");
new TaioServer().StartWithCompletionHandler();
System.out.println("main out ==>>");
}
}

  1.2、

package aio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.Future; public class TaioClient
{
// http://yunhaifeiwu.iteye.com/blog/1714664
public static void main(String[] args) throws Exception
{
AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
Future<Void> futureConn = client.connect(new InetSocketAddress("localhost", 9888));
futureConn.get(); // Future<?>.get();等待异步事件的完成
Future<Integer> futureWrite = client.write(ByteBuffer.wrap("testAA".getBytes()));
int iWritten = futureWrite.get();
System.out.println("Client send ["+iWritten+"] bytes .");
}
}

2、

3、

TCP异步IO_服务端_测试的更多相关文章

  1. MSDN上的异步socket 服务端例子

    MSDN上的异步socket 服务端例子 2006-11-22 17:12:01|  分类: 代码学习 |  标签: |字号大中小 订阅     Imports SystemImports Syste ...

  2. 安装_oracle11G_客户端_服务端_链接_oracle

    在开始之前呢,有一些注细节需要注意,oracle11G_客户端_和_服务端, 分为两种   一种是  开发者使用    一种是  BDA  自己使用(同时也需要根据自己 PC 的系统来做_win7_与 ...

  3. QTcpSocket-Qt使用Tcp通讯实现服务端和客户端

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QTcpSocket-Qt使用Tcp通讯实现服务端和客户端     本文地址:https:// ...

  4. linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现

    1 TCP简介 tcp是一种基于流的应用层协议,其“可靠的数据传输”实现的原理就是,“拥塞控制”的滑动窗口机制,该机制包含的算法主要有“慢启动”,“拥塞避免”,“快速重传”. 2 TCP socket ...

  5. 数往知来 ASP.NET 模拟服务器:服务端_静态页面_动态页面的响应<十七>

      一.客户端是怎么看到我们的网页的呢/ 在浏览器端,如果用汉语请求的是一普通的HTML网页,呢么我们的IIS服务器, 接收到请求以后,那么从IIS服务器所在的电脑区查找该HTML网页, 找到以后将该 ...

  6. 网络编程、三要素、Socket通信、UDP传输、TCP协议、服务端(二十五)

    1.网络编程概述 * A:计算机网络 * 是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传 ...

  7. 基于node的tcp客户端和服务端的简单通信

    1.简单介绍下TCP/IP TCP/IP是互联网相关协议的集合,分为以下四层:应用层.传输层.网络层.数据链路层. 分成四层的好处是,假如只有一层,某个地方需要改变设计时,就必须把所有整体替换掉,而分 ...

  8. Winfrom 基于TCP的Socket服务端 多线程(进阶版)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. TCP中的服务端与客户端的实现

    TCP中首先要在服务端开启监听,这样才可以从客户端链接 using System; using System.Collections.Generic; using System.Linq; using ...

随机推荐

  1. cygwin简介,安装及卸载(体验UNIX & Linux环境)

    对于爱好者或初学者来说,为了体验UNIX & Linux环境,去安装虚拟机或双系统稍显麻烦,cygwin是一个很好的选择 具/原料   安装windows的电脑一台(可以联网) 法/步骤   ...

  2. VC的CListCtrl控件

    1. CListCtrl 样式及设置 2. 扩展样式设置 3. 数据插入 4. 一直选中Item 5. 选中和取消选中Item 6. 得到CListCtrl中所有行的checkbox的状态 7. 得到 ...

  3. Qunit 和 jsCoverage使用方法(js单元测试)

    Qunit 和 jsCoverage使用方法(js单元测试) 近日在网上浏览过很多有关js单元测试相关的文档,工具,但是,针对Qunit 和 jsCoverage使用方法,缺少详细说明,对于初入前端的 ...

  4. python类的相关知识第二部分

    类的继承.多态.封装 一.类的继承 1.应用场景: 类大部分功能相同,大类包含小类的情况 例如: 动物类 共性:都要吃喝拉撒.都有头有脚 特性: 猫类.走了很轻,叫声特别,喜欢白天睡觉 狗类.的叫声很 ...

  5. Java 语言基础之数组应用

    什么时候使用数组呢? 如果数据出现了对应关系, 而且对应关系的一方是有序的数字编号, 并作为角标使用. 这时,就必须要想到数组的使用. 也就是将这些数据存储到数组中, 根据运算的结果作为角标, 直接去 ...

  6. [转载]Css设置table网格线(无重复)

    原文地址:Css设置table网格线(无重复)作者:依然贰零零柒 效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transition ...

  7. Linux内核学习资料

    1.为什么计算机的学生要学习Linux开源技术 http://tinylab.org/why-computer-students-learn-linux-open-source-technologie ...

  8. kubernetes,Docker网络相关资料链接

    1.Why kubernetes not doesn't use libnetwork http://blog.kubernetes.io/2016/01/why-Kubernetes-doesnt- ...

  9. PHP memcache的使用教程

    (结尾附:完整版资源下载) 首先,为什么要用memcached?如果你看过InnoDB的一些书籍,你应该知道在存储引擎那一层是由一个内存池的.而在内存池中 又有一个缓冲池.而缓冲池就会缓冲查找的数据, ...

  10. 005-Symbol、Proxy、Reflect

    1.Symbol:http://es6.ruanyifeng.com/#docs/symbol 2.Proxy:http://es6.ruanyifeng.com/#docs/proxy Proxy ...