1

I have a Client - Server java nio application. In some cases I need to handle situations like that:

on Server:

                SocketChannel socketChannel = (SocketChannel) key.channel();
                for (int i=0;i<30;i++){
                    String message = "message: "+i;
                    ByteBuffer messageBuffer = ByteBuffer.wrap(message.getBytes());
                    socketChannel.write(messageBuffer);
                }

and on client:

while (true){
            try {
                int bytesreadden= channel.read(buffer);
                if (bytesreadden >0){
                    System.out.println(new String(buffer.array(),0,buffer.position()));
                    buffer.clear();
                }
            }catch (IOException e){}
        }

in my situation this 30 messages in client seems like one big message.http://i.imgur.com/hy8PsRj.png

So, what is better solution to handle this situation? Thank you!

Max Max
  • 389
  • 1
  • 2
  • 12
  • 3
    There are no messages in TCP. It is a byte-stream protocol. If you need messages you must implement them yourself, with a length word prefix, type-length-value, XML, etc. – user207421 Dec 13 '15 at 18:34

0 Answers0