TCP异步IO_服务端_测试
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_服务端_测试的更多相关文章
- MSDN上的异步socket 服务端例子
MSDN上的异步socket 服务端例子 2006-11-22 17:12:01| 分类: 代码学习 | 标签: |字号大中小 订阅 Imports SystemImports Syste ...
- 安装_oracle11G_客户端_服务端_链接_oracle
在开始之前呢,有一些注细节需要注意,oracle11G_客户端_和_服务端, 分为两种 一种是 开发者使用 一种是 BDA 自己使用(同时也需要根据自己 PC 的系统来做_win7_与 ...
- QTcpSocket-Qt使用Tcp通讯实现服务端和客户端
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QTcpSocket-Qt使用Tcp通讯实现服务端和客户端 本文地址:https:// ...
- linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现
1 TCP简介 tcp是一种基于流的应用层协议,其“可靠的数据传输”实现的原理就是,“拥塞控制”的滑动窗口机制,该机制包含的算法主要有“慢启动”,“拥塞避免”,“快速重传”. 2 TCP socket ...
- 数往知来 ASP.NET 模拟服务器:服务端_静态页面_动态页面的响应<十七>
一.客户端是怎么看到我们的网页的呢/ 在浏览器端,如果用汉语请求的是一普通的HTML网页,呢么我们的IIS服务器, 接收到请求以后,那么从IIS服务器所在的电脑区查找该HTML网页, 找到以后将该 ...
- 网络编程、三要素、Socket通信、UDP传输、TCP协议、服务端(二十五)
1.网络编程概述 * A:计算机网络 * 是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传 ...
- 基于node的tcp客户端和服务端的简单通信
1.简单介绍下TCP/IP TCP/IP是互联网相关协议的集合,分为以下四层:应用层.传输层.网络层.数据链路层. 分成四层的好处是,假如只有一层,某个地方需要改变设计时,就必须把所有整体替换掉,而分 ...
- Winfrom 基于TCP的Socket服务端 多线程(进阶版)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- TCP中的服务端与客户端的实现
TCP中首先要在服务端开启监听,这样才可以从客户端链接 using System; using System.Collections.Generic; using System.Linq; using ...
随机推荐
- selenium调用Firefox和Chrome需要注意的一些问题,和出现的报错selenium:expected [object undefined] undefined to be a string
在高版本selenium下如:selenium3.4.3 1.高版本的selenium需要浏览器安装一些补丁驱动 Firefox:geckodriver 下载网址:http://download.cs ...
- Drupal 8 提供REST服务实例
drupal8 的核心模块已经支持REST服务. 这样的话使用drupal 对外提供web service 变的简单了. 测试一下d8 的webservice : extend 中的 依赖模块:全部启 ...
- IO流入门-第二章-FileOutputStream
FileOutputStreamj基本用法和方法示例 /* java.io.OutputStream java.io.FileOutputStream 文件字节输出流 将计算机内存中的数据写入到硬盘文 ...
- c++智能指针,new之后不需要delete,简单的垃圾回收
http://www.cnblogs.com/TenosDoIt/p/3456704.html 智能指针的初始化: 错误: std::shared_ptr<Serializer> seri ...
- 浅谈REDIS数据库的键值设计(转)
add by zhj: 关系数据库表的一条记录可以映射成Redis中的一个hash类型,其实数据库记录本来就是键值对.这样,要比本文中的键设计用更少的键,更节省内存,因为每个键除了它的键值占用内存外, ...
- 使用Free命令查看Linux服务器内存使用状况(-/+ buffers/cache详解)
free命令可选参数 -b,-k,-m,-g show output in bytes, KB, MB, or GB -h human readable output (automatic unit ...
- mysql内置数据库
1 mysql本地连接数据库 >mysql -h localhost -u root -proot 注意-p后面没有空格,是直接跟上密码 或者 >mysql -h localhost ...
- java反射基础知识(一)
一.反射 反射:JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为 ...
- 【转】XML的几种读写
XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖于内 ...
- Django——认证系统(Day72)
阅读目录 COOKIE 与 SESSION 用户认证 COOKIE 与 SESSION 概念 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因 ...