server/client communication problem
I am confused with my project.
It is a server/client system which uses java nio.
The requiremet is that the server keep sending 10 messages to the client. Then the client read messages. But the cilent can read incomplete message which causes exception.
I have a message size at the beginning of a message. How to let the client read the number of bytes specified in the msgSize.
how to solve this problem? Thanks.
Re: server/client communication problem
What classes and methods are you using?
Re: server/client communication problem
For read:
Code :
n = socket.read(rbuf);
if (((n==0) || (n==-1))) {
logger.logInfo("t=" + t + " count=" + count + " and break."); //5-21-11
break;
}
For write:
Code :
n = socket.write(buffer);
if(n == 0) {
buffer.clear();
break;//Feb14
}
count += n;
if(buffer.hasRemaining()){
buffer.compact();
}
Re: server/client communication problem
Quote:
How to let the client read the number of bytes specified in the msgSize.
What class is socket?
Your code appears to try read rbuf.size number of bytes. Why not read the header and get the message size at the beginning of a message before reading the rest.
Re: server/client communication problem
Thank you. But the SocketChannel class just provide method to read a chunk, instead of byte by byte. Is there any way to read the header and get message size first. Then I can read the required amount of bytes. Thanks.
Re: server/client communication problem
What class is the variable socket that you show in your code sample?
Re: server/client communication problem
socket is an instance of SocketChannel.
If I add an interval between messages, the problem is not there. I just feel that adding an interval may not be a good solution. Is there other ways?
Re: server/client communication problem
Sorry, I haven't worked with that.
Re: server/client communication problem