写入一个AsynchronousSocketChannel和处理数据在基于事件的方式 [英] Writing to an AsynchronousSocketChannel and processing the data in an Event-based way

查看:1241
本文介绍了写入一个AsynchronousSocketChannel和处理数据在基于事件的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何发送Java中的插座之间的数据(这是一个更大的项目的一部分,我会回来,并回答我的previous两人一度我可以解决此相关的疑问。 )。我想在Java中异步连接客户端和服务器插座,然后将它们之间发送消息,并得到一个回调,比如说,当我发送一个消息从客户机到服务器。

I'm trying to figure out how to send data between sockets in Java (this is part of a bigger project and I'll get back and answer my previous two questions related to that once I can resolve this..). I would like to connect a client and a server socket asynchronously in Java, and then send messages between them, and get a callback, say, when I have sent a message from the client to the server.

我想我已经设法获得的设立工作。这里是我的code:

I think I have managed to get the set-up working. Here is my code:

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){  
    }
}

我包括在用于测试的简单的同一文件中的服务器和客户端。

I'm including the server and the client in the same file for simplicity of testing.

所以假设连接建立成功,我有一个计时器(说),这是写数据到服务器套接字,我想有客户端的socket'听'从发送新数据的过程服务器,然后在写入(没有做这样的事情通过一个定时器和一个while循环来检查是否新数据已添加定期检查)生成一个回调。这是在C#accomplishable和一个不错的教程,请访问:
http://www.developerfusion.com/文章/ 3918 /插座编程,在C-部分-1/2 /

So supposing the connection is successfully established, and I have a process on a timer (say) that was writing data to the server socket, I'd like to have the client socket 'listen' for this new data being sent from the server and then generate a callback when a write occurs (without doing something like periodically checking via a timer and a while loop to check that whether new data has been added). This is accomplishable in C# and a nice tutorial is available at: http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/2/

这是如何做到这一点的任何提示将不胜AP preciated。谢谢!
克里斯

Any tips on how to do this would be greatly appreciated. Thanks! Chris

推荐答案

您可以使用RMI来实现这个目标,文件可以在那里找到:
http://www.oracle.com/technetwork/java /javase/tech/index-jsp-136424.html

You could use RMI to accomplish that, the documentation can be found there: http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136424.html

有了这个,你的服务器可以尽可能多的,因为你需要通知你的客户。

With this, your server could notify your client as much as you need.

这篇关于写入一个AsynchronousSocketChannel和处理数据在基于事件的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆