我有一个从下面读取的服务器SocketChannel
:
private void readDataFromSocket(SocketChannel socketChannel) throws IOException {
BUFFER.clear();
int count = 0;
while ((count = socketChannel.read(BUFFER)) > 0) {
BUFFER.flip();
int limit = BUFFER.limit();
while (limit > 0) {
System.out.print((char) BUFFER.get());
limit--;
}
}
if (count < 0) {
System.out.println("closing the socket!!!");
socketChannel.close();
}
}
下面是客户端写入的客户端SocketChannel
:
private void write(String str, SocketChannel channel) throws IOException{
byte[] b = str.getBytes();
buffer.clear();
buffer.put(b);
buffer.flip();
while(buffer.hasRemaining()){
channel.write(buffer);
}
}
所以我的问题:
- 当恰好在服务器代码中,该
count
值将是 0 (while ((count = socketChannel.read(BUFFER)) > 0)
)? count
如果服务器已经读取了客户端发送的消息的一半,即假设客户端写道:stack overflow
,服务器中是否有可能在count
读取stack
客户端发送的消息的一半后为0(认为消息可以是 1MB 大小)?