Search code examples
apache-mina

Apache MINA Java TCP Client-Server Communication


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


Solution

  • 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