0
votes

I am using Apache Mina in the Server side. I've a client which is written in tradition IO. Here's the CLIENT side code that sends data to server.

class SomeClass extends Thread
{
    Socket socket;

    //Constructor
    SomeClass()
    {
        Socket socket = ...
    }

    public void run()
    {
        while (j++ & lt; 10)
        {
            System.out.println("CLIENT[" + clientNo + "] Send Message =>" + requests[clientNo][j]);
            OutputStream oStrm = socket.getOutputStream();
            byte[] byteSendBuffer = (requests[clientNo][j]).getBytes();
            oStrm.write(byteSendBuffer);
            oStrm.flush();
        }
    }
}

The above thread is run for say 20 times. So 20 sockets are created. And in 1 socket, many messages are send. With a server written using IO socket classes i'm able to retrieve data perfectly.

THe problem comes in the Apache Mina based Server which uses BUFFER! I am not able to get individual messages.

How do i get individual messages (given i'm not able to change anything in client, AND the length of individual messages are not known)

Server Side Code
Socket Creation

    public static void main(String[] args) throws IOException, SQLException {           
        System.out.println(Charset.defaultCharset().name());
        IoAcceptor acceptor = new NioSocketAcceptor();
        ProtocolCodecFilter(charset.newEncoder(),charset.newDecoder() ));
        acceptor.setHandler( new TimeServerHandler() );
        acceptor.getSessionConfig().setReadBufferSize(64 );
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
        acceptor.bind( new InetSocketAddress(PORT) );
    }

Handler Code

    public void messageReceived(IoSession session, Object message) throws Exception {
            AbstractIoBuffer bf = (AbstractIoBuffer)message;
            Charset charset = Charset.forName("UTF-8");
            CharsetDecoder decoder = charset.newDecoder();
            String outString = bf.getString(decoder);

    }
2
Can you please send the crucial part of the server side code? For this question the client side code is of no help.Uwe Plonus

2 Answers

1
votes

How do i get individual messages

You don't. There is no such thing as a message in TCP. It is a byte-stream protocol. There are no message boundaries and there is no guarantee that one read equals one write at the other end.

(given i'm not able to change anything in client, AND the length of individual messages are not known)

Your are going to have to parse the messages to find where they stop according to the definition of the application protocol. If that isn't possible because, say, the protocol is ambiguous, the client will have to be junked. However it seems that as you can't change the client, it must already work with an existing system, so the guy before you had the same problem and solved it somehow.

0
votes

MINA is actually a very elaborate framework to solve your problem in an elegant way. Its basic concept is a filter chain, in which a series of filters are applied on an incoming message.

You should implement a protocol decoder (implementing MessageDecoder) and register it in your MINA filter chain. That decoder should parse byte buffers to the object representation of your choice.

Then, you can register a message handler that handles complete messages.