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