套接字丢失数据

2023-12-27

我正在尝试编写 Java 代码来从流式 HTTP 连接接收数据(我想调试针对 spring-websocket 服务器运行 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 段来响应我的 POST。一份包含 1460 字节(+54 字节 TCP/IP 标头),一份包含 1178 字节(+54 字节标头),一份包含 7 字节(+44 字节标头)。

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

为什么我的 Java 代码收不到剩余数据?当我使用原始 Socket 时,它应该完全不知道 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

----

如何获取剩余数据?

编辑:如果我减少接收缓冲区大小and读取后将一个字节写入输出流,我似乎收到了丢失的数据。这是为什么?


原来是我的病毒扫描仪的“在线屏蔽”造成的。当我关闭 AVG AntiVirus Business Editions 的在线防护后,我丢失的数据就开始出现。看起来它不喜欢流数据。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

套接字丢失数据 的相关文章

随机推荐

  • 从多个 MPI 输出组成 VTK 文件

    对于盖驱动腔 CFD 的格子玻尔兹曼模拟 我将立方域分解为 也是立方 8 个子域 这些子域按 8 个等级独立计算 每个 MPI 等级都会为每个时间步生成一个 VTK 文件 并且由于我使用的是 ParaView 所以我希望将整个事物可视化为一
  • 直播视频延迟

    尝试确定造成延迟的 最大 原因 我的视频从编码器到服务器的往返行程 然后返回到浏览器中的播放器 我现在和我喜欢的球员的距离大约是 12 秒 它在我的播放器中缓冲吗 FMLE 退出时缓冲 我问的原因是我觉得我已经通过下面概述的小测试场景消除了
  • 使用 CSS 在嵌套 div 上重复一组颜色

    我有一组四种颜色 我想将它们应用到嵌套的 div 所以接下来的每个孩子都有不同的颜色 如果有第五层嵌套 我想从第一种颜色开始 并继续进行 即使我有无限深的嵌套 这是否可以仅使用 CSS 选择器来完成 避免 JavaScript 我目前陷入了
  • 如何在没有证书的情况下将 HTTPS 请求重定向到 HTTP (Apache VirtualHosts) 并避免证书警告

    我首先想说 这不是一个好的做法 我们应该努力让所有内容 100 都在 HTTPS 上 但在这种情况下 我对不保存敏感信息的系统提出了一系列尴尬的要求 当我还是初级学生的时候 当我问这个问题时 我对 HTTPS TLS 的工作原理一无所知 但
  • 使用CSS更改当前页面的链接颜色

    当前页面的一种样式链接与其他样式有何不同 我想交换文本和背景的颜色 li a color A60500 li a hover color 640200 background color 000000 ul li class a a href
  • 如何在ANTLR4中实现错误处理

    我有以下语法用于解析应用于图形的一阶逻辑公式 grammar Graph PARSER RULES input formula EOF formula TRUE FALSE formula AND formula formula OR fo
  • 搭建 dbcontext 时出现不明确的列名“name”错误

    我正在尝试从现有数据库构建脚手架 但该数据库有多个具有多个模式的表 并且某些表具有相同的名称但在不同的模式中 我 认为 这是我的问题的根源 我想知道您是否已经遇到过类似的情况吗 例如mySchema1 contacts and mySche
  • 如何从字符串中查找斜杠出现的次数

    如何使用 Excel VBA 宏查找字符串中正斜杠字符 的出现次数 老问题 但我想我会通过在 Excel 论坛上找到的答案来提高答案的质量 显然计数也可以使用找到 count Len string Len Replace string 答案
  • NodeJS:如何从文件中读取(最多)前 N 个字节?

    在 NodeJS 中 从文件中最多读取前 N 个字节的简洁 健壮且优雅的方法是什么 如果数据较少 那么我不希望抛出错误 如果有更多数据 那么我不希望将其读入内存 理想情况下无需安装外部软件包 也许涉及自 NodeJS 12 以来似乎是新的
  • 服务器在rails 3生产环境中找不到公用文件夹

    我正在使用最新的 Rails 3 beta 该应用程序在开发模式下工作正常 但是当我通过以下方式在生产模式下启动服务器时rails server e production 似乎public找不到文件夹 我收到如下错误消息 ActionCon
  • 在PHP中接收UDP数据包数据报

    我正在用 php 为 GPS 跟踪系统构建监听服务器 GPS 通过 UDP 数据包发送数据 我可以通过运行以下脚本来显示数据 然而 实际数据以符号形式出现 所以我猜我错过了转换 Reduce errors error reporting E
  • 以不同顺序遍历 3D 数组

    我有一个 3D 节点数组 我想通过从数组的中间节点开始 并向角落移动来遍历它 就像这样 and So on but for visualization purposes I ve shown in 2D but actually it is
  • swift xcode 从播放器列表中播放声音文件

    我正在寻找一种快速编码 可以从播放器列表中播放声音 而不是将声音作为资源添加到您的项目中 我主要找到了用法 NSURL fileURLWithPath NSBundle mainBundle pathForResource sound na
  • 在ios中使用Quartz 2d进行注释(注释、评论)

    我正在为 iPad 做基于 PDF 的应用程序 它支持缩略图视图 徒手注释和注释 Notes 我和我在网上搜索了很多 但没有找到任何使用 Quartz 2d 在 PDF 上进行注释的相关答案 我们可以使用以下方法获取 PDF 页面信息 CG
  • HID 兼容设备不会在 ChromeOS 上使用 chrome.hid API 进行枚举

    使用 USB HID 传感器我似乎只能用chrome usb API https developer chrome com apps usb在 Chrome 操作系统上 在 OS X 上我可以枚举chrome hid API https d
  • 正则表达式删除每行开头的空格?

    我想删除每行开头的空格 我每行都有数据 开头有一组空格 因此数据出现在中间 我想删除每行开头的空格 tmp tmp replace lt gt gt g 我怎样才能添加 s条件成那个replace 要删除所有前导空格 str str rep
  • 从哪里开始使用 API [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我以前从未使用过 API 并且在 HTML 和 CSS 之外几乎没有编程经验 并且我正在努力寻找任何清晰的教程或解释 所有 API 的工作方
  • 模拟函数调用实际函数

    我有 cassandra python 驱动程序的代码 from cassandra cqlengine management import sync table def sync my tables print sync table pr
  • 应该由 Promise 调用同步代码。然后创建一个新的 Promise

    我已经实现了一些代码 其中异步代码后面跟着一些同步函数 例如 function processSomeAsyncData asyncFuncCall then syncFunction catch error 如果我理解正确的话then也是
  • 套接字丢失数据

    我正在尝试编写 Java 代码来从流式 HTTP 连接接收数据 我想调试针对 spring websocket 服务器运行 Sockjs XHR 流时遇到的问题 我使用一些非常简单的代码来连接 URI uri The server Sock