每次我读取传入消息的一段时,套接字是否会清除部分缓冲区?
Do sockets clean part of their buffer every time I read a segment of the incoming message?
我已经开始使用 Java 和套接字,但在使用 DataInputStream 时遇到了一些问题。
我收到的电报在消息本身的前 4 个字节中包含消息长度,因此在第一次迭代时我只读取了这个内存部分。
当我再次阅读传入消息时,我注意到前 4 个字节不见了,因此我需要在我创建的方法中减去这 4 个字节来计算消息长度本身。
问题是:传入数据的缓冲区是否丢失了我已经读取的字节?我在 Java 文档中找不到任何内容,但由于我缺乏经验,我可能遗漏了一些内容。
这是读取数据的方法:
/**
* It receives data from a socket.
*
* @param socket The communication socket.
* @param lengthArea The area of the header containing the length of the message to be received.
* @return The received data as a string.
*/
static String receiveData(Socket socket, int lengthArea) {
byte[] receivedData = new byte[lengthArea];
try {
DataInputStream dataStream = new DataInputStream(socket.getInputStream());
int bufferReturn = dataStream.read(receivedData, 0, lengthArea);
System.out.println("Read Data: " + bufferReturn);
} catch (IOException e) {
// Let's fill the byte array with '-1' for debug purpose.
Arrays.fill(receivedData, (byte) -1);
System.out.println("IO Exception.");
}
return new String(receivedData);
}
这是我用来计算消息长度的方法:
/**
* It converts the message length from number to string, decreasing the calculated length by the size of the message
* read in the header. The size is defined in 'Constants.java'.
*
* @param length The message size.
* @return The message size as an integer.
*/
static int calcLength(String length) {
int num;
try {
num = Integer.parseInt(length) + 1 - MESSAGE_LENGTH_AREA_FROM_HEADER;
} catch (Exception e) {
num = -1;
}
return num;
}
Constants.java
MESSAGE_LENGTH_AREA_FROM_HEADER = 4;
does the buffer of the incoming data lose the bytes I've already read
是的,当然可以。 TCP 呈现字节流。你消耗了它的一部分,它就消失了。与读取文件没有区别。
如果是二进制,你应该使用 DataInputStream.readInt()
读取长度字,然后使用 DataInputStream.readFully()
读取数据。
我已经开始使用 Java 和套接字,但在使用 DataInputStream 时遇到了一些问题。 我收到的电报在消息本身的前 4 个字节中包含消息长度,因此在第一次迭代时我只读取了这个内存部分。 当我再次阅读传入消息时,我注意到前 4 个字节不见了,因此我需要在我创建的方法中减去这 4 个字节来计算消息长度本身。 问题是:传入数据的缓冲区是否丢失了我已经读取的字节?我在 Java 文档中找不到任何内容,但由于我缺乏经验,我可能遗漏了一些内容。
这是读取数据的方法:
/**
* It receives data from a socket.
*
* @param socket The communication socket.
* @param lengthArea The area of the header containing the length of the message to be received.
* @return The received data as a string.
*/
static String receiveData(Socket socket, int lengthArea) {
byte[] receivedData = new byte[lengthArea];
try {
DataInputStream dataStream = new DataInputStream(socket.getInputStream());
int bufferReturn = dataStream.read(receivedData, 0, lengthArea);
System.out.println("Read Data: " + bufferReturn);
} catch (IOException e) {
// Let's fill the byte array with '-1' for debug purpose.
Arrays.fill(receivedData, (byte) -1);
System.out.println("IO Exception.");
}
return new String(receivedData);
}
这是我用来计算消息长度的方法:
/**
* It converts the message length from number to string, decreasing the calculated length by the size of the message
* read in the header. The size is defined in 'Constants.java'.
*
* @param length The message size.
* @return The message size as an integer.
*/
static int calcLength(String length) {
int num;
try {
num = Integer.parseInt(length) + 1 - MESSAGE_LENGTH_AREA_FROM_HEADER;
} catch (Exception e) {
num = -1;
}
return num;
}
Constants.java
MESSAGE_LENGTH_AREA_FROM_HEADER = 4;
does the buffer of the incoming data lose the bytes I've already read
是的,当然可以。 TCP 呈现字节流。你消耗了它的一部分,它就消失了。与读取文件没有区别。
如果是二进制,你应该使用 DataInputStream.readInt()
读取长度字,然后使用 DataInputStream.readFully()
读取数据。