首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >写入AsynchronousSocketChannel并以基于事件的方式处理数据

写入AsynchronousSocketChannel并以基于事件的方式处理数据
EN

Stack Overflow用户
提问于 2013-04-19 22:26:42
回答 1查看 723关注 0票数 0

我正在尝试弄清楚如何在Java中的套接字之间发送数据(这是一个更大项目的一部分,一旦我可以解决这个问题,我会回来回答前面两个与此相关的问题)。我希望在Java中异步连接客户端和服务器套接字,然后在它们之间发送消息,并获得回调,例如,当我从客户端向服务器发送消息时。

我想我已经设法让设置工作起来了。下面是我的代码:

代码语言:javascript
复制
private AsynchronousServerSocketChannel socListener;
private AsycnchrnonousSocketChannel socClient;

//This is the GUI callback for the button that initiates the socket server
private void button_StartSocketServerActionPerformed(ava.awt.event.ActionEvent evt)
{
    try{
        InetAddress ipLocal= InetAddress.getLocalHost(); 
        InetSocketAddress ipSocket=new InetSocketAddress(ipLocal,8221);

        m_socListener= AsynchronousServerSocketChannel.open().bind(ipSocket);

        m_socListener.accept(null, new CompletionHandler<AsynchronousSocketChannel,Void>() 
                                   {
                                       @Override
                                       public void completed(AsynchronousSocketChannel ch, Void att) 
                                       {
                                            // accept the next connection
                                            m_socListener.accept(null, this);
                                            // handle this connection
                                       }
                                       @Override
                                       public void failed(Throwable exc, Void att)  { }
                                   }
                             );
        }
        catch (Exception e){ 
        }
}

//This is the GUI callback for the button that initiates the client socket
private void button_StartClientSocketActionPerformed(java.awt.event.ActionEvent evt) 
{
     try
     {
        socClient=AsynchronousSocketChannel.open();

        InetAddress ipLocal= InetAddress.getLocalHost();
        InetSocketAddress ipSocket=new InetSocketAddress(ipLocal,8221);
        socClient.connect(ipSocket, null, new CompletionHandler<Void,Void>() 
                                   {
                                       @Override
                                       public void completed(Void att1, Void att2) 
                                       {
                                            // handle this connection
                                       }
                                       @Override
                                       public void failed(Throwable exc, Void att) {}
                                   }
                            );
    }
    catch (Exception e){  
    }
}

为了简化测试,我将服务器和客户端包含在同一个文件中。

因此,假设连接已成功建立,并且我有一个定时器(比方说)上的进程正在向服务器套接字写入数据,我希望客户端套接字‘监听’从服务器发送的新数据,然后在写入发生时生成一个回调(而不是通过定时器和while循环定期检查是否添加了新数据)。这可以在C#中实现,在http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/2/上可以找到一个很好的教程

任何关于如何做到这一点的提示都将非常感谢。谢谢!克里斯

EN

回答 1

Stack Overflow用户

发布于 2013-04-20 01:42:58

您可以使用RMI来完成此任务,文档可以在以下位置找到:http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136424.html

这样,您的服务器就可以根据您的需要通知您的客户端。

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

https://stackoverflow.com/questions/16106957

复制
相关文章

相似问题

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