我正在尝试修复使用 TCP 接收文件的应用程序。不幸的是,我没有发送应用程序的源代码。我遇到的问题是,在收到第一个文件后,发送应用程序发送了第二个文件,但接收应用程序没有接收到它。
我认为问题在于接收文件后套接字没有被关闭。我有一个应该关闭它的方法,但是 _socket.Connected = false,所以什么也没做。但是,如果我检查端口,它仍然是绑定的,即使在套接字关闭之后也是如此。
Private Sub CloseSocket(ByVal disconnect As Boolean)
If Not (_socket Is Nothing) Then
If (_socket.Connected = True) Then
_socket.Shutdown(SocketShutdown.Both)
If(disconnect) Then
_socket.Disconnect(True)
End If
End If
_socket.Close()
_socket = Nothing
End If
End Sub
我意识到我没有包含太多代码,但是 Listen 方法非常庞大且令人费解。如果它能提供洞察力,我可以获得额外的代码。任何帮助将不胜感激。
已编辑
用于检查端口状态的代码如下:
Private Shared Function TcpIpGetPortStatus(ByVal port As Int32) As String
Dim properties As NetworkInformation.IPGlobalProperties
Dim listeners As Net.IPEndPoint()
Dim local As Net.IPEndPoint
Dim connections As NetworkInformation.TcpConnectionInformation()
Dim t As NetworkInformation.TcpConnectionInformation
Dim portStatus As String
portStatus = "Disconnected"
properties = NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()
listeners = properties.GetActiveTcpListeners()
Try
' Cycle through all listening TCP connections and find the one that is associated with port.
For Each local In listeners
If (local.Port = port) Then
portStatus = "Connected"
Exit For
End If
Next local
' Port wasn't in the listening state so check if it is established.
If (portStatus = "Disconnected") Then
properties = NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()
connections = properties.GetActiveTcpConnections()
' Cycle through all active TCP connections and find the one that is associated with port.
For Each t In connections
If (t.LocalEndPoint.Port = port) Then
Select Case t.State
Case NetworkInformation.TcpState.Established
portStatus = "Connected"
Exit For
End Select
End If
Next t
End If
Catch ex As Exception
' Handle Exception...
End Try
Return portStatus
End Function
此函数在调用时将返回“已连接”。我追查过了,发现我使用的端口仍然是绑定的。再次感谢您的所有帮助。
添加
我只是在两个独立的系统之间运行它,使用 WireShark 来捕获数据。有一个 Sender 和 Receiver 我没有源代码,并且正在尝试与之集成,还有一个 Sender 和 Receiver,我正在更新代码以使其与现有代码正确通信。在这两种情况下发送的流是相同的(ACK 中的日期时间除外)。如果我从现有发送方向正在升级的接收方发送第二条消息,则消息会发送到端口,但开发中的接收方永远不会收到通知,这会挂起两个程序。