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 ...
随机推荐
- MySQL主从不一致修复
场景: 线上正在服务的库由于紧急主从切换导致主从不一致,报错信息如下: Last_Error: Coordinator stopped because there were error(s) in t ...
- PHP中Trait详解及其应用
w PHP中Trait详解及其应用 - 开发者日常 - SegmentFaulthttps://segmentfault.com/a/1190000008009455
- buffer/interger overflow /return-to-libc攻击实验
buffer/interger overflow /return-to-libc攻击实验 http://blog.sina.com.cn/s/blog_70dd16910100rdgn.html ht ...
- Python图像处理库Pillow入门
http://python.jobbole.com/84956/ Pillow是Python里的图像处理库(PIL:Python Image Library),提供了了广泛的文件格式支持,强大的图像处 ...
- IIS设置文件 Robots.txt 禁止爬虫
robots.txt用于禁止网络爬虫访问网站指定目录.robots.txt的格式采用面向行的语法:空行.注释行(以#打头).规则行.规则行的格式为:Field: value.常见的规则行:User-A ...
- Lua(1)
1.the use of functions in table fields is a key ingredient for some advanced uses of Lua, such as mo ...
- Keras网络层之常用层Core
常用层 常用层对应于core模块,core内部定义了一系列常用的网络层,包括全连接.激活层等 Dense层 keras.layers.core.Dense(units, activation=None ...
- jupyter常用快捷键
Jupyter Notebook 有两种键盘输入模式.即命令模式和编辑模式,这与 vim有些类似. 在编辑模式下,可以往单元中键入代码或文本,此时单元格被绿色的框线包围,且命令模式下的快捷键不生效. ...
- VK Cup 2018 - Round 1+Codeforces Round #470
A. Primal Sport 题意:有两个人轮流玩游戏.给出数X(i-1),轮到的人需要找到一个小于X(i-1)的素数x,然后得到Xi,Xi是x的倍数中大于等于X(i-1)的最小的数.现在已知X2, ...
- Java集合(1):ArrayList
Java容器类的用途是“保存对象”,分为两类:Map——存储“键值对”组成的对象:Collection——存储独立元素.Collection又可以分为List和Set两大块.List保持元素的顺序(有 ...