AIO异步读写数据:

客户端:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.TimeUnit;
//AIO下的 异步客户端通道
public class AIOSocketChannelTest {
public static void main(String[] args) throws IOException {
//1.创建异步的客户端通道
AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open();
//2.去连接服务器,采用异步非阻塞方法
socketChannel.connect(new InetSocketAddress(“192.168.3.54”, 6666), null, new CompletionHandler<Void, Object>() {
@Override
public void completed(Void result, Object attachment) {
System.out.println(“连接服务器成功…”);
//3.给服务器发送数据
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put(“你好我是客户端”.getBytes());
//4.切换模式
buffer.flip();
//5.异步的writer(缓冲区,超时时间,时间单位,附件(null),回调接口)
socketChannel.write(buffer, 10, TimeUnit.SECONDS, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
System.out.println(“数据发送成功!”);
//6.释放资源
try {
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void failed(Throwable exc, Object attachment) {
//7.客户端发送失败
System.out.println(“数据发送失败!”);
}
});
}
@Override
public void failed(Throwable exc, Object attachment) {
//8.程序连接失败
System.out.println(“连接服务器失败!”);
}
});
System.out.println(“程序继续执行…”);
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

服务器:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.TimeUnit;
//服务器端
public class AIOServerSocketChannelTest {
public static void main(String[] args) throws IOException {
//1.创建一个异步的服务去通道
AsynchronousServerSocketChannel Serverchannel = AsynchronousServerSocketChannel.open();
//2.绑定本地某个端口
Serverchannel.bind(new InetSocketAddress(6666));
//3.接收异步客户端,采用异步非阻塞方式
Serverchannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() {
@Override
public void completed(AsynchronousSocketChannel result, Object attachment) {
System.out.println(“有客户端连接…”);
//4.从客户端读取数据
//异步的read(字节缓冲区,超时时间,时间单位,附件(null),回调接口)
ByteBuffer buffer = ByteBuffer.allocate(1024);
result.read(buffer, 10, TimeUnit.SECONDS, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
System.out.println(“数据读取完毕…”);
System.out.println(“接收到数据的长度:”+result);
System.out.println(“接收到的数据是:”+new String(buffer.array(),0,result));
//5.释放资源
try {
Serverchannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void failed(Throwable exc, Object attachment) {
//6.回调赌球赌数据失败
System.out.println(“读取数据失败…”);
}
});
}
@Override
public void failed(Throwable exc, Object attachment) {
//7.回调提示信息:客户端连接失败
System.out.println(“客户端连接失败…”);
}
});
System.out.println(“程序继续执行…”);
while (true){
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值