首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java中同时管理AsynchronousSocketChannel或AsynchronousServerSocketChannel

如何在java中同时管理AsynchronousSocketChannel或AsynchronousServerSocketChannel
EN

Stack Overflow用户
提问于 2016-04-02 23:06:06
回答 1查看 96关注 0票数 0

我是用Java NIO写的。我的模块的工作方式就像一个人作为服务器(使用AsynchronousServerSocketChannel)获取一些数据,然后将这些数据发送到AsynchronousSocketChannel作为客户端创建的另一个完全不同的连接

代码语言:javascript
复制
public void completed(AsynchronousSocketChannel result, Void attachment) {
        asynchronousServerSocketChannel.accept(null, this);
        try {
           System.out.println("Incoming connection from: " + result.getRemoteAddress());
           //transmitting data
           while (result.read(buffer).get() != -1) {
                  buffer.flip();
                  result.write(buffer).get();
                  ClientwithFuture anotherDiffrentSocketChannel = new ClientwithFuture(buffer);

使用最后一行是不符合逻辑的,因为它会导致服务器中断并运行AsynchronousSocketChannel。那么,在您看来,对于这个问题有什么好的解决方案呢?

EN

回答 1

Stack Overflow用户

发布于 2016-04-07 14:58:06

好的,我定义了两个线程,在其中一个线程中编写AsynchronousSocketChannel,在另一个线程中编写AsynchronousServerSocketChannel。为了在这两个线程之间进行通信,我使用了一些共享变量并解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36374894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档