Reputation: 3442
I am using Apache Mina in the server side. I've a client which is written in traditional I/O. Here's the client side code that sends data to the server.
class SomeClass extends Thread
{
Socket socket;
// Constructor
SomeClass()
{
Socket socket = ...
}
public void run()
{
while (j++ < 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 one socket, many messages are send. With a server written using I/O 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 the 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); }
Upvotes: 1
Views: 1077
Reputation: 310860
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.
Upvotes: 1
Reputation: 3630
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.
Upvotes: 0