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. C#自动给文章关键字加链接实现代码

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  2. paper reading:gaze tracking

    https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Krafka_Eye_Tracking_for_CVPR_2016_ ...

  3. Angular学习笔记—Rxjs、Promise的区别

    Promises: 异步操作完成或失败时处理单个事件 不可取消 代码可读性强,有try/catch Observables: 可持续监听和响应多个事件 可取消订阅 支持map, filter, red ...

  4. Angular路由参数传递

    一.路由时传递参数的方式 1.在查询参数中传递数据 //页面 <a routerLink="/product" [queryParams]="{id:1}" ...

  5. sql查询原理和Select执行顺序

    一 sql语句的执行步骤 1)语法分析,分析语句的语法是否符合规范,衡量语句中各表达式的意义. 2) 语义分析,检查语句中涉及的所有数据库对象是否存在,且用户有相应的权限. 3)视图转换,将涉及视图的 ...

  6. flask实现获取表单并执行shell

    1.一个HTML form input和一个button提供给用户输入 2.使用flask的request获取用户输入的文件名 3.判断输入异常 4.执行shell命令touch aa.txt 并返回 ...

  7. python16_day07【class】

    一.初识类 1.类的两种作用:属性引用和实例化 class Garen: #定义英雄盖伦的类,不同的玩家可以用它实例出自己英雄; camp='Demacia' #所有玩家的英雄(盖伦)的阵营都是Dem ...

  8. 动态更改WebBrowser数据流内容

    动态更改WebBrowser数据流内容       有时,由于软件的特殊需要,我们希望DELPHI在WebBrowser或embeddedwb里动态更改返回的数据内容,而这需要返回网页的所有原始源码, ...

  9. LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: --异常记录

    升级了JDK之后,启动应用,直接抛出此异常.网上搜罗半天,没有正确的解决方案. 然后想到了是“升级了JDK”,重新检查所有JDK相关的配置的地方,在Debug Configurations里找到启动时 ...

  10. 数据库权限分配(远程共享数据库)(mysql)

    1. 数据库远程权限 mysql -uroot -proot grant all privileges on formal.* to root@'192.168.3.40' identified by ...