Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 套接字中缺少数据_Java_Sockets_Tcp_Spring Websocket - Fatal编程技术网

Java 套接字中缺少数据

Java 套接字中缺少数据,java,sockets,tcp,spring-websocket,Java,Sockets,Tcp,Spring Websocket,我试图编写Java代码来接收来自流式HTTP连接的数据(我想调试针对SpringWebSocket服务器运行Sockjs XHR流式传输时遇到的一个问题) 我使用一些非常简单的代码来连接: URI uri = // The server Socket socket = new Socket(); socket.connect(new InetSocketAddress(uri.getHost(), uri.getPort())); OutputStream os

我试图编写Java代码来接收来自流式HTTP连接的数据(我想调试针对SpringWebSocket服务器运行Sockjs XHR流式传输时遇到的一个问题)

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

    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头

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

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

第一个包含HTTP头和878字节的内容(Sockjs XHR流从发送2048h的前导开始)。下一个包含序言的其余部分,最后一个包含“开放帧”。正如我对服务器的期望

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

以下是第一个接收到的TCP段的转储:

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

----
如何获取剩余数据


编辑:如果我减小接收缓冲区的大小,并在读取后将一个字节写入outputstream,那么我似乎收到了丢失的数据。为什么会这样?

原来是我的virusscanner“在线盾”造成的。当我关闭AVG AntiVirus Business Edition的在线屏蔽后,我丢失的数据就开始显示出来。看起来它不喜欢流式数据。

以某种方式减小接收缓冲区的大小可以使其工作。为什么?把它擦掉。但是,通过将数据写入OutputStream来减小接收缓冲区的大小是可行的。“第一个缓冲区包含HTTP头以及878字节的内容…”并且在显示的日志中显示:
read:582
,然后是头。这不合适。还是我遗漏了什么?@alk:当我使用Wireshark检查网络流量时,我看到三个TCP段,第一个包含HTTP头和878字节的内容。但是我的应用程序只接收HTTP头,read()只在接收HTTP头之后阻塞,而不提供剩余的数据。我同意:这不合适,但我也可能遗漏了什么。啊,在第一个数据包的878字节中,您对
read()
的第一次调用返回582字节。然后呢
read()
阻塞,或者它返回
-1
并退出循环?它是否返回
0
?或者别的什么?