无法让端口关闭

时间:2012-08-30 23:53:30

标签: vb.net sockets tcp

我正在尝试修复使用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方法非常庞大且令人费解。如果它能提供洞察力,我可以获得额外的代码。非常感谢任何帮助。

EDITED

用于检查端口状态的代码如下:

    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

此功能在调用时将返回“已连接”。我已经跟踪过了,发现我使用的端口仍然受到限制。再次感谢您的帮助。

ADDITION

我只是在两个独立的系统之间运行它,使用WireShark捕获数据。有一个发件人和接收器我没有源代码,我正在尝试与之集成,发件人和接收器我正在更新代码以使其与现有代码正确通信。在两种情况下发送的流都是相同的(除了ACK中的日期时间)。如果我从现有发件人向正在升级的接收器发送第二条消息,则会将消息发送到端口,但不会通知开发中的Receiver,这会挂起两个程序。

1 个答案:

答案 0 :(得分:0)

在倾倒了提供给我的代码后,我终于意识到它正在关闭并断开错误的套接字。而不是在用于接收消息的套接字上调用Shutdown和Disconnect,而是在侦听套接字上调用它。

一旦我意识到这一点,我就能够关闭正确的套接字,应用程序开始工作。现在我只需要通过并纠正这些由底层代码引起的所有问题,事情应该是好的。

感谢所有回复的人。你让我思考正确的方向。