在Java中读取tcp流的最有效方法 [英] Most efficient way to read in a tcp stream in Java

查看:668
本文介绍了在Java中读取tcp流的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不修改一些客户端代码,因为没有正确定义通信协议。

I'm having to modify some client side code because the comms protocol wasn't defined properly.

我认为来自服务器的tcp消息会终止于一个新行,所以我使用reader.readLine()来读取我的数据。

I'd assummed that a tcp message from the server would terminate in a new line so I used reader.readLine() to read my Data.

现在我被告知事实并非如此,而是第一次消息的4个字符是消息长度,然后我必须阅读消息的其余部分。

Now I've been told that this is not the case and that instead the first 4 chars of the message are the message length and then I have to read the rest of the message.

最有效的合理方法是什么?

What is the most efficient sensible way to do this?

我的一般想法如下:


  1. 创建一个4字符数组

  2. 读入前4个字符

  3. 确定消息长度是什么

  4. 创建新的消息长度数组

  5. 读入新数组。

  1. Create a 4 char array
  2. Read in first 4 characters
  3. Determine what the message length is
  4. Create a new array of message length
  5. read into the new array.

以下是代码示例(reader是创建的BufferedReader其他地方):

Here is an example of the code (reader is a BufferedReader created elsewhere):

char[] chars = new char[4];
int charCount = reader.read(chars);
String messageLengthString = new String(chars);
int messageLength = Integer.parseInt(messageLengthString);
chars = new char[messageLength];
charCount = reader.read(chars);
if (charCount != messageLength)
{
    // Something went wrong...
}

我知道怎么做,但我是否要担心字符缓冲区没有被填充?如果是这样我该如何应对这种情况?

I know how to do this, but Do I have to worry about the character buffers not being filled? if so how should I deal with this happening?

推荐答案

Java中的字符用于 text 数据。您确定协议真的以这种方式定义消息的长度吗?更有可能的是前四个字节表示32位长度。

Chars in Java are for text data. Are you sure the protocol really defines the length of the message this way? More likely it's the first four bytes to represent a 32-bit length.

如果您正在与C或C ++开发人员交谈,他们可能会使用char作为byte的同义词。

If you're talking to C or C++ developers they may be using "char" as a synonym for "byte".

编辑:好的,基于评论:

Okay, based on the comment:

我会创建一个方法,它取一个 Reader 和一个计数并反复调用 read(),直到它读完适量的数据,或抛出异常。这样的事情:

I would create a method which took a Reader and a count and repeatedly called read() until it had read the right amount of data, or threw an exception. Something like this:

public static String readFully(Reader reader, int length) throws IOException
{
    char[] buffer = new char[length];
    int totalRead = 0;
    while (totalRead < length)
    {
        int read = reader.read(buffer, totalRead, length-totalRead);
        if (read == -1)
        {
            throw new IOException("Insufficient data");
        }
        totalRead += read;
    }
    return new String(buffer);
}

然后您的代码可以是:

String lengthText = readFully(reader, 4);
int length = Integer.parseInt(lengthText);
String data = readFully(reader, length);
// Use data now

你应该检查当他们想要发送少于1000个(或超过9999个)字符虽然......

You should check what happens when they want to send fewer than 1000 (or more than 9999) characters though...

这篇关于在Java中读取tcp流的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆