开发者

Apache MINA Java TCP Client-Server Communication

开发者 https://www.devze.com 2023-01-30 17:40 出处:网络
i want co communicate TCP client server communication using apache mina. can anyone give the code ? i cant find it in any开发者_如何学JAVA were....

i want co communicate TCP client server communication using apache mina. can anyone give the code ? i cant find it in any开发者_如何学JAVA were.... google is failed to find it. i want to send and recieve text messages via mina . so please help me....


It is very simple on the server side

 SocketConnectorConfig SOCKET_CONFIG = new SocketConnectorConfig();

        IoFilter charsetFilter = new ProtocolCodecFilter(
        new TextLineCodecFactory(Charset.forName("UTF-8")));
        SOCKET_CONFIG.getFilterChain().addLast("codec", charsetFilter);
theIoAcceptor.bind(new InetSocketAddress(thePort),
                      new TriggerReceiverHandler();

here ioAcceptor is of type org.apache.mina.common.IoAcceptor you can instantiate it by using NioDatagramAcceptor

TriggerReceiverHandler is the class that handles the messages and session related events. it needs to extend the IoHandlerAdapter.

This is for server. Similarly for client the only change is instead of IoAcceptor you use the IoConnector.

Here is an example, well explained too. http://www.techbrainwave.com/?p=912

0

精彩评论

暂无评论...
验证码 换一张
取 消