2

我有一个到服务器的 TCP 连接,由套接字和流实现。在会话期间,服务器可以发送任意数量的消息——我必须阅读并处理所有消息。

我创建了一个线程,它在无限循环中检查和读取数据:

in = socket.getInputStream();
ByteArrayOutputStream baos = null;
byte[] buf = new byte[4096];
while(!isInterrupted()) {
   baos = new ByteArrayOutputStream();
   for(int s; ( s = in.read(buf)) != -1; ) {
       baos.write(buf, 0, s);
       if(in.available() <= 0 ) {
           readChunk(baos.toByteArray());
       }
   }
}

但实际上,它效率不高——它使 CPU 处于重负载之下,并且字节与前面的答案粘在一起。

解决这种情况的最有效和最优雅的方法是什么?

4

3 回答 3

6

TCP不是面向消息的,而是面向流的。这意味着,如果您发送两条消息 AA 和 BB,则很有可能在不同的场合读取值 AABB、AABB、A ABB、AAB B、AA BB(其中空格表示不同的读取尝试)。

您将需要自己处理消息大小或消息分隔符,因此不再需要 in.available()。此外,您的代码至少将相同的数据复制到不同的缓冲区 3 次,并考虑在 socket.getInputStream() 上使用 BufferedInputStream。

于 2013-07-25T11:04:48.217 回答
2

删除 available() 调用。InputStream.available()不是对流结束的有效检查,它在其文档中这样说。它也永远不能返回负值。该readChunk()方法也是应该进行阅读的方法。TCP 中也没有消息,因此使用available()或任何其他技术来识别它们是无效的。

编辑

您在其他推荐中说您有一个计数前缀。用那个。用 读取它DataInputStream.readInt(),分配一个byte[]该大小的数组,然后用DataInputStream.readFully():

int len = din.readInt();
byte[] message = new byte[len];
din.readFully(message);
于 2013-07-25T11:06:50.527 回答
0

根据您对消息所说的内容,这是一种方法:

in = socket.getInputStream();
byte[] buff = new byte[4096];
int packLen=0;
int ret=0;
while(!isInterrupted()) {
    int offset=0;
    int bLeft=4;
    // 99% of the times the read will return 4 bytes, 
    // but just in case, put it in a loop.
    while (bLeft > 0) {
        ret = in.read(buff, offset, bLeft);
        if (ret > 0) {
            bLeft-=ret;
            offset+=ret;
        }
        else if (ret == 0) {
            // socket has been closed
        }
        else {
            // soket has an error
        }

    }
    // convert the 4 bytes to an int, depends on the way it's was sent
    // this method is used frecuently
    packLen = (int)((buff[0] & 0xff) << 24) |
                 (int)((buff[1] & 0xff) << 16) |
                 (int)((buff[2] & 0xff) << 8) |
                 (int)(buff[3] & 0xff);

    // if the 4 bytes of the CRC32 is not included in the length, 
    // increment the length
    packLen+=4;
    offset=4;
                if (packLen > 4092)
                {
                   // packet is too big, ignore it or do something else
                   packLen=4092;
                }
    bLeft=packLen;
    // Noew loop until the whole mesage has been read
    while (bLeft > 0) {
        ret = in.read(buff, offset, bLeft);
        if (ret > 0) {
            bLeft-=ret;
            offset+=ret;
        }
        else if (ret == 0) {
            // socket has been closed
        }
        else {
            // soket has an error
        }
    }
    // the readChunk function must be change
    // Need to pass the length of the message. 
    // Is not the buff.length anymore 
    readChunk(buff, packLen+4 /* +4 for the length of the message*/);
}

如果你需要一个 Java CRC32 类,我可以给你,它符合 PKZIP 和以太网标准。

编辑:注意:如果数据包长度大于 4096,则此方法将不起作用。

于 2013-07-25T17:42:25.090 回答