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 ...
随机推荐
- HDU 1875 畅通工程再续(kruskal)
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- AI篇6====>第一讲
1.人工智能 小米:小爱 百度:AI云平台 科大讯飞AI平台 2.百度语音合成 # Author: studybrother sun from aip import AipSpeech #从文本到声音 ...
- 第四课(2)——mysql配置参数讲解
*****************general***************** user 启动mysql domain的用户 port 数据库端口号 socket 数据库socket文件的路径 p ...
- 使用ADO如何获得SQLSERVER 2K的数据库名的列表
打开数据库连接_ConnectionPtr m_pConn;_RecordsetPtr m_pRs;m_pConn.CreateInstance(__uuidof(Connection));m_pRs ...
- gitlab 阿里邮箱配置
gitlab 阿里邮箱配置 # gitlab_rails['smtp_user_name'] = "smtp user"# gitlab_rails['smtp_password' ...
- IIS 部署WCF时遇到这么个错:
转(http://blog.csdn.net/vic0228/article/details/48806405) 部署WCF时遇到这么个错: "The service cannot be a ...
- logging/re - 总结
logging 模块 很多程序都有记录日志的需求 logging的日志可以分为 debug(), info(), warning(), error() and critical()5个级别 1.输出到 ...
- 洛谷 P2481 [SDOI2010]代码拍卖会
洛谷 这大概是我真正意义上的第一道黑题吧! 自己想出了一个大概,状态转移方程打错了一点点,最后还是得看题解. 一句话题意:求出有多少个\(n\)位的数,满足各个位置上的数字从左到右不下降,且被\(p\ ...
- python创建一个线程和一个线程池
创建一个线程 1.示例代码 import time import threading def task(arg): time.sleep(2) while True: num = input('> ...
- Andrew Ng机器学习总结(自用)
监督学习: 线性回归,逻辑回归,神经网络,支持向量机. 非监督学习: K-means,PCA,异常检测 应用: 推荐系统,大规模机器学习 机器学习系统优化: 偏差/方差,正则化,下一步要进行的工作:评 ...