gpt4 book ai didi

c# - Begin-EndReceive 看不到到达的数据

转载 作者:可可西里 更新时间:2023-11-01 02:51:54 26 4
gpt4 key购买 nike

我的 TCP 客户端在接收数据时卡住了。

  1. 已建立连接
  2. 客户端发送 21 个字节。 (见#5)
  3. 客户端上的下一个 BeginReceive 调用永远不会调用回调。当我强制它调用 EndReceive 时,它​​永远挂起。 IsCompleted = false。

一些数据在#5 之后到达,但问题是它永远不会到达我的应用程序。 TcpClient.Available 返回 135。

客户端可以发送数据,但不从服务器接收任何数据。

wireshark window

客户端在 Unity3D 和 Mono 2.6 中运行。我只能在 Unity Editor 中首次启动时重现它。第二次它开始正常但随机卡住(也是外部编辑器)。

那么,为什么会这样呢?

Wireshark log (设置过滤器 tcp.port == 21050)。

更新:另一个例子

NoDelay = true,Blocking = false,LingerState.Enabled = false,Linger = 0

发送操作使用Socket.Send,接收操作使用Socket.Begin-End-Receive

服务器日志:

500: Server started
7832: 1: Next receive scheduling for time 25
7863: 1: BeginReceive, available: 0
7925: 1: About to process received data: 21
7925: 1: Processing received data 21 // hello
7941: 1: Send 4 // handshake
7972: 1: Send 8 // init data
8050: 1: Send 41 // init data
8050: 1: Processing received data finished
8050: 1: Next receive scheduling for time 0
8050: 1: BeginReceive, available: 0
8237: 1: Send 86 // init data
8331: 1: About to process received data: 45
8331: 1: Processing received data 45 // set position (x,y,z)
8346: 1: Processing received data finished
8346: 1: Next receive scheduling for time 0
8346: 1: BeginReceive, available: 0
9906: 1: About to process received data: 2
9906: 1: Processing received data 2 // ping
9906: 1: Send 4 // ping response, never reaches client
9906: 1: Processing received data finished
9906: 1: Next receive scheduling for time 0
9906: 1: BeginReceive, available: 0

客户端日志:

1: BeginReceive, available: 0
1: Send 21 // hello
1: EndReceive, available 0
1: About to process received data: 4
1: Processing received data 4 // handshake
1: Processing received data finished
1: Next receive scheduling for time 0
1: BeginReceive, available: 135
1: EndReceive, available 0
1: About to process received data: 135 // 8 + 41 + 86
1: Processing received data 135 // init data
1: Processing received data finished
1: Next receive scheduling for time 0
1: Send 45 // set position (x,y,z)
1: BeginReceive, available: 0
1: Send 2 // ping

Waiting ping result too long
No receive for long time, force finish, completed: False

1: EndReceive, available 0 // blocks forever

Wireshark 显示 4 长度的数据包到达。如果服务器继续发送数据包,那么它们将在客户端套接字上排队(可用 > 0)。

最佳答案

这似乎是单声道错误,因为单声道不支持像 MS .NET 那样将非阻塞套接字与异步 API 一起使用。由于性能原因,我需要使用非阻塞模式进行发送操作。我用 SendAsync 替换了非阻塞 Send,用 ReceiveAsync 替换了 BeginReceive,现在它已修复。通过一些努力,我使 SendAsync 的工作速度与非阻塞 Send 一样快。

关于c# - Begin-EndReceive 看不到到达的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26891551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com