未接收所有数据包 - Visual Basic 套接字 - 异步
这是我的套接字发送端的代码:
Private Sub Send(ByRef Buffer() As Byte)
Dim obj_StateObject As New StateObject
obj_StateObject.WorkSocket = m_tmpSocket
m_tmpSocket.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, New AsyncCallback(AddressOf OnClientSendComplete), obj_StateObject)
End Sub
Private Sub OnClientSendComplete(ByVal ar As IAsyncResult)
Dim obj_SocketState As StateObject = CType(ar.AsyncState, StateObject)
Dim obj_Socket As Socket = obj_SocketState.WorkSocket
End Sub
这是接收代码:
Private Sub OnServerDataArrival(ByVal ar As IAsyncResult)
Dim obj_SocketState As StateObject = CType(ar.AsyncState, StateObject)
Dim obj_Socket As Socket = obj_SocketState.WorkSocket
Dim BytesRead As Integer = obj_Socket.EndReceive(ar)
If (BytesRead > 0) Then
Dim TempLength As Integer = BitConverter.ToInt32(obj_SocketState.Buffer, 0) - 4
Dim TempBuffer(TempLength - 1) As Byte
Array.Copy(obj_SocketState.Buffer, 4, TempBuffer, 0, TempLength)
RaiseEvent ServerDataArrival(TMP_ID, TempBuffer)
End If
obj_Socket.BeginReceive(obj_SocketState.Buffer, 0, obj_SocketState.BufferSize, 0, New AsyncCallback(AddressOf OnServerDataArrival), obj_SocketState)
End Sub
我通常可以很好地发送和接收数据。例如,我可以通过单击按钮发送 10 字节缓冲区,并且没有任何问题。
但是,如果我设置该按钮来调用一个函数,该函数将循环发送 10 字节缓冲区 10 次,那么我平均不会收到大约 8 个缓冲区。
是什么原因造成的?发送代码执行了10次,但是OnServerDataArrival函数只执行了2次。
Here's the code on the sending side of my sockets:
Private Sub Send(ByRef Buffer() As Byte)
Dim obj_StateObject As New StateObject
obj_StateObject.WorkSocket = m_tmpSocket
m_tmpSocket.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, New AsyncCallback(AddressOf OnClientSendComplete), obj_StateObject)
End Sub
Private Sub OnClientSendComplete(ByVal ar As IAsyncResult)
Dim obj_SocketState As StateObject = CType(ar.AsyncState, StateObject)
Dim obj_Socket As Socket = obj_SocketState.WorkSocket
End Sub
Here's the receiving code:
Private Sub OnServerDataArrival(ByVal ar As IAsyncResult)
Dim obj_SocketState As StateObject = CType(ar.AsyncState, StateObject)
Dim obj_Socket As Socket = obj_SocketState.WorkSocket
Dim BytesRead As Integer = obj_Socket.EndReceive(ar)
If (BytesRead > 0) Then
Dim TempLength As Integer = BitConverter.ToInt32(obj_SocketState.Buffer, 0) - 4
Dim TempBuffer(TempLength - 1) As Byte
Array.Copy(obj_SocketState.Buffer, 4, TempBuffer, 0, TempLength)
RaiseEvent ServerDataArrival(TMP_ID, TempBuffer)
End If
obj_Socket.BeginReceive(obj_SocketState.Buffer, 0, obj_SocketState.BufferSize, 0, New AsyncCallback(AddressOf OnServerDataArrival), obj_SocketState)
End Sub
I can send and receive data just fine usually. For instance, I can send a 10 byte buffer across with a button click and there are no problems.
However, if I set that button to call a function that will loop the sending of the 10 byte buffer 10 times, I will not receive about 8 of the buffers on average.
What is causing this? The sending code is executed 10 times, but the OnServerDataArrival function is only executed 2 times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您提供的代码中尚不清楚,但我的猜测是您正在使用 TCP(而不是 UDP)传输数据。如果这是真的,您不应期望发送计数与接收计数相匹配。如果您想要这种行为,请使用 UDP 而不是 TCP。
使用 TCP 时,数据通常在发送端进行缓冲,直到准备好发送“足够”的数据量。这通常有助于提高传输的整体效率,特别是在一次发送小至 10 个字节的消息时。在接收端,每次读取 TCP 套接字都会从套接字的接收缓冲区读取全部数据,最多可达您提供的缓冲区大小。因此,如果已收到 10 字节“消息”中的 8 个,则下次从套接字读取时会将所有数据拉入缓冲区(前提是缓冲区足够大)。您将需要解析缓冲区以从字节流中提取每条消息。正如您所做的那样,发送消息的大小将帮助您做到这一点。
It is not clear from the code you've provided, but my guess is that you are transmitting the data using TCP (as opposed to UDP). If this is true, you shouldn't expect the send count to match the receive count. If you want that behavior, use UDP instead of TCP.
When using TCP, the data is typically buffered on the send side until a "sufficient" amount of data is ready to be sent. This usually helps improve the overall efficiency of the transmission, especially when sending messages as small as 10 bytes at a time. On the receive side, each read of the TCP socket reads all of the data from the socket's receive buffer up to the size of the buffer you provide. So if 8 of your 10 byte "messages" have been received, the next read from the socket will pull all that data into your buffer (providing it's big enough). You will need to parse your buffer to extract each message from the stream of bytes. Sending the size of the message, as you appear to be doing, will help you do this.