문제

I have a Java NIO socket server.

The server is getting JSONObjects from remote clients. i'm using the SocketChannel.read(ByteBuffer) method in order to read from the channel. each message ends with '\n' which marks the end of the current message.

my problem is that sometimes the read() method read more bytes and reach after the '\n'.. is there a way for me to read from the SocketChannel only until the '\n' is found ? i thought about maybe byte by byte read (i just couldn't find documents on how to implement it..)?

any other solutions ?

도움이 되었습니까?

해결책

Process the bytes from your ByteBuffer up to and including the '\n', so the buffer's position is the first byte after the '\n', then call ByteBuffer.compact(). Any bytes which were past the '\n' will remain in the buffer and the next read will append to them.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top