我是用Java NIO写的。我的模块的工作方式就像一个人作为服务器(使用AsynchronousServerSocketChannel)获取一些数据,然后将这些数据发送到AsynchronousSocketChannel作为客户端创建的另一个完全不同的连接
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。那么,在您看来,对于这个问题有什么好的解决方案呢?
发布于 2016-04-07 14:58:06
好的,我定义了两个线程,在其中一个线程中编写AsynchronousSocketChannel,在另一个线程中编写AsynchronousServerSocketChannel。为了在这两个线程之间进行通信,我使用了一些共享变量并解决了这个问题。
https://stackoverflow.com/questions/36374894
复制相似问题