套接字丢失数据 [英] Missing data from socket

查看:41
本文介绍了套接字丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写 Java 代码以从流式 HTTP 连接接收数据(我想调试我在针对 spring-websocket 服务器运行 Sockjs XHR 流时遇到的问题).

I am trying to write Java code to receive data from a streaming HTTP connection (I would like to debug a problem I have with running Sockjs XHR-streaming against a spring-websocket server).

我使用一些非常简单的代码来连接:

I use some really simple code to connect:

    URI uri = // The server
    Socket socket = new Socket();
    socket.connect(new InetSocketAddress(uri.getHost(), uri.getPort()));
    OutputStream os = socket.getOutputStream();
    PrintWriter pw = new PrintWriter(os);
    int id = new Random().nextInt() % 100000;
    pw.println("POST /socket/" + id + "/xxx/xhr_streaming HTTP/1.1");
    pw.println("Content-Type: application/stomp");
    pw.println("Content-Length: 0");
    pw.println("Connection: keep-alive");
    pw.println();
    pw.flush();
    InputStream is = socket.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[8192];
    int bytes;
    do {
        bytes = is.read(buffer);
        if (bytes > 0) {
            System.out.println("read: "+ bytes);
            System.out.print(new String(buffer, 0, bytes));
            System.out.println("----");
        }
    } while (bytes >= 0);

但是当我运行它时,我只能得到 HTTP 标头.

but when I run it I only get the HTTP headers.

这本身并不会太令人惊讶.我怀疑服务器没有正确发送内容.

That would not be too surprising in itself. I would suspect that the server did not send the contents properly.

但是当我使用 Wireshark 监控流量时,我可以看到我收到了三个 TCP 段以响应我的 POST.一个包含 1460 个字节(+54 个字节的 TCP/IP 标头)、一个 1178 个字节(+54 个字节的标头)和一个 7 个字节(+44 个字节的标头).

But when I monitor the traffic using Wireshark, I can see that I receive three TCP segments in response to my POST. One containing 1460 bytes (+54 bytes TCP/IP headers), one of 1178 bytes (+54 bytes header) and one of 7 bytes (+44 bytes header).

第一个包含 HTTP 标头以及 878 字节的内容(Sockjs XHR 流从发送 2048 小时的前导码开始).下一个包含前导码的其余部分,最后一个包含开放帧".完全符合我对服务器的期望.

The first one contains the HTTP headers as well as 878 bytes of the contents (Sockjs XHR streaming starts with sending a preamble of 2048 h's). The next one contains the rest of the preamble and the final one contains the 'open frame'. Exactly as I would expect from the server.

为什么我的 Java 代码收不到剩余的数据?当我使用原始 Socket 时,它应该完全不知道 HTTP 协议,所以我不明白为什么我只收到 HTTP 标头.服务器可能会在写入标头后刷新数据,但我可以在网络转储中看到标头和数据的第一部分都包含在第一个 TCP 段中.

Why do my Java code not receive the remaining data? When I use a raw Socket it should be completely unaware of the HTTP protocol, so I do not understand why I receive only the HTTP headers. The server probably flushes the data after writing the headers, but I can see in the network-dump that both the headers and the first part of the data is contained in the first TCP segment.

这是第一个收到的 TCP 段的转储:

Here is a dump of the first received TCP segment:

0000   e8 39 35 45 20 df 00 0c 29 09 62 3b 08 00 45 00  .95E ...).b;..E.
0010   05 dc 0c 5e 40 00 80 06 5b 27 c0 a8 06 26 c0 a8  ...^@...['...&..
0020   06 20 1f 90 df d9 92 50 c3 f6 ac 89 8a c3 50 10  . .....P......P.
0030   01 00 fc 88 00 00 48 54 54 50 2f 31 2e 31 20 32  ......HTTP/1.1 2
0040   30 30 20 4f 4b 0d 0a 53 65 72 76 65 72 3a 20 41  00 OK..Server: A
0050   70 61 63 68 65 2d 43 6f 79 6f 74 65 2f 31 2e 31  pache-Coyote/1.1
0060   0d 0a 58 2d 55 41 2d 43 6f 6d 70 61 74 69 62 6c  ..X-UA-Compatibl
0070   65 3a 20 49 45 3d 65 64 67 65 2c 63 68 72 6f 6d  e: IE=edge,chrom
0080   65 3d 31 0d 0a 58 2d 43 6f 6e 74 65 6e 74 2d 54  e=1..X-Content-T
0090   79 70 65 2d 4f 70 74 69 6f 6e 73 3a 20 6e 6f 73  ype-Options: nos
00a0   6e 69 66 66 0d 0a 58 2d 58 53 53 2d 50 72 6f 74  niff..X-XSS-Prot
00b0   65 63 74 69 6f 6e 3a 20 31 3b 20 6d 6f 64 65 3d  ection: 1; mode=
00c0   62 6c 6f 63 6b 0d 0a 43 61 63 68 65 2d 43 6f 6e  block..Cache-Con
00d0   74 72 6f 6c 3a 20 6e 6f 2d 63 61 63 68 65 2c 20  trol: no-cache, 
00e0   6e 6f 2d 73 74 6f 72 65 2c 20 6d 61 78 2d 61 67  no-store, max-ag
00f0   65 3d 30 2c 20 6d 75 73 74 2d 72 65 76 61 6c 69  e=0, must-revali
0100   64 61 74 65 0d 0a 50 72 61 67 6d 61 3a 20 6e 6f  date..Pragma: no
0110   2d 63 61 63 68 65 0d 0a 45 78 70 69 72 65 73 3a  -cache..Expires:
0120   20 30 0d 0a 58 2d 46 72 61 6d 65 2d 4f 70 74 69   0..X-Frame-Opti
0130   6f 6e 73 3a 20 44 45 4e 59 0d 0a 58 2d 46 72 61  ons: DENY..X-Fra
0140   6d 65 2d 4f 70 74 69 6f 6e 73 3a 20 41 4c 4c 4f  me-Options: ALLO
0150   57 2d 46 52 4f 4d 20 2a 0d 0a 58 2d 41 70 70 6c  W-FROM *..X-Appl
0160   69 63 61 74 69 6f 6e 2d 43 6f 6e 74 65 78 74 3a  ication-Context:
0170   20 61 70 70 6c 69 63 61 74 69 6f 6e 0d 0a 43 61   application..Ca
0180   63 68 65 2d 43 6f 6e 74 72 6f 6c 3a 20 6e 6f 2d  che-Control: no-
0190   73 74 6f 72 65 2c 20 6e 6f 2d 63 61 63 68 65 2c  store, no-cache,
01a0   20 6d 75 73 74 2d 72 65 76 61 6c 69 64 61 74 65   must-revalidate
01b0   2c 20 6d 61 78 2d 61 67 65 3d 30 0d 0a 41 63 63  , max-age=0..Acc
01c0   65 73 73 2d 43 6f 6e 74 72 6f 6c 2d 41 6c 6c 6f  ess-Control-Allo
01d0   77 2d 4f 72 69 67 69 6e 3a 20 2a 0d 0a 41 63 63  w-Origin: *..Acc
01e0   65 73 73 2d 43 6f 6e 74 72 6f 6c 2d 41 6c 6c 6f  ess-Control-Allo
01f0   77 2d 43 72 65 64 65 6e 74 69 61 6c 73 3a 20 74  w-Credentials: t
0200   72 75 65 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70  rue..Content-Typ
0210   65 3a 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 6a  e: application/j
0220   61 76 61 73 63 72 69 70 74 3b 63 68 61 72 73 65  avascript;charse
0230   74 3d 55 54 46 2d 38 0d 0a 54 72 61 6e 73 66 65  t=UTF-8..Transfe
0240   72 2d 45 6e 63 6f 64 69 6e 67 3a 20 63 68 75 6e  r-Encoding: chun
0250   6b 65 64 0d 0a 44 61 74 65 3a 20 46 72 69 2c 20  ked..Date: Fri, 
0260   32 36 20 53 65 70 20 32 30 31 34 20 30 39 3a 32  26 Sep 2014 09:2
0270   36 3a 31 37 20 47 4d 54 0d 0a 0d 0a 38 30 31 0d  6:17 GMT....801.
0280   0a 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  .hhhhhhhhhhhhhhh
0290   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02a0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02b0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02c0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02d0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02e0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02f0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0300   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0310   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0320   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0330   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0340   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0350   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0360   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0370   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0380   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0390   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03a0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03b0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03c0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03d0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03e0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03f0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0400   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0410   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0420   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0430   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0440   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0450   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0460   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0470   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0480   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0490   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04a0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04b0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04c0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04d0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04e0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04f0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0500   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0510   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0520   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0530   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0540   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0550   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0560   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0570   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0580   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0590   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
05a0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
05b0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
05c0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
05d0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
05e0   68 68 68 68 68 68 68 68 68 68                    hhhhhhhhhh

这是代码的输出:

read: 582
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-UA-Compatible: IE=edge,chrome=1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
X-Frame-Options: ALLOW-FROM *
X-Application-Context: application
Cache-Control: no-store, no-cache, must-revalidate, max-age=0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Content-Type: application/javascript;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 26 Sep 2014 09:26:17 GMT

----

如何获取剩余数据?

如果我减小接收缓冲区大小在读取后向输出流写入一个字节,我似乎收到了丢失的数据.这是为什么?

If I decrease the receive buffer size and write a single byte to the outputstream after reading, I seem to receive the missing data. Why is that?

推荐答案

原来是我的病毒扫描器的在线防护"引起的.我一关闭 AVG AntiVirus Business Editions 的在线屏蔽,我丢失的数据就开始出现.看起来它不喜欢流式数据.

Turns out that it was caused by my virusscanner's "online shield". As soon as I turned off AVG AntiVirus Business Editions' online shield, my missing data started coming through. Looks like it does not like streaming data.

这篇关于套接字丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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