首页 > 解决方案 > Tcp 套接字未连接

问题描述

我有以下代码:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Try
                Dim reply As String = "v1.0"
                If reply <> S_Settings.StubVer Then
                    S_Messages.Messages("{ New update is available! }", "Get to me")
                ElseIf reply = S_Settings.StubVer Then
                    S_Messages.Messages("{ You are using the latest version! }", "Bugs :Contact me")
                End If
        Catch ex As Exception
        End Try


        Dim Client As Net.Sockets.TcpClient = Nothing
        Dim PortLabel As New Text.StringBuilder
        For Each ThePort In S_Settings.PORT
            Try
                If GetExternalAddress() = "127.0.0.1" Then
                    PortLabel.Append(String.Format(" {0}=OPENED ", ThePort))
                    MetroLabel3.Text = String.Format("IP={0} {1} KEY={2}", GetExternalAddress, PortLabel.ToString, "abcd")
                    MetroLabel3.ForeColor = Color.Lime
                Else
                    Client = New Net.Sockets.TcpClient
                    Client.Connect(GetExternalAddress, ThePort)
                    PortLabel.Append(String.Format(" {0}=OPENED ", ThePort))
                    MetroLabel3.Text = String.Format("IP={0} {1} KEY={2}", GetExternalAddress, PortLabel.ToString, "abcd")
                    MetroLabel3.ForeColor = Color.Lime
                End If
            Catch ex As Net.Sockets.SocketException
                MessageBox.Show(ex.ToString() + " 1")
                MetroProgressSpinner1.Spinning = False
                PortLabel.Append(String.Format(" {0}=CLOSED ", ThePort))
                MetroLabel3.Text = String.Format("IP={0}  {1}  KEY={2}", GetExternalAddress, PortLabel.ToString, "abcd")
                MetroLabel3.ForeColor = Color.Red
                'Try : My.Computer.Audio.Play(My.Resources._Error, AudioPlayMode.Background) : Catch : End Try 'https://freesound.org/people/eardeer/sounds/385281/

                If GTV("FW") = Nothing Then
                    Dim result As DialogResult = MessageBox.Show("Port " & ThePort & " seems to be blocked" + Environment.NewLine + "Do you want to add myProg into firewall exception", "", MessageBoxButtons.YesNo)
                    If result = DialogResult.Yes Then
                        Try
                            STV("FW", "1")
                            Dim process As New Process()
                            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
                            process.StartInfo.FileName = "cmd.exe"
                            process.StartInfo.UseShellExecute = True
                            process.StartInfo.Verb = "runas"
                            process.StartInfo.Arguments = "/c netsh advfirewall firewall add rule name=""myProg"" dir=in action=allow program=""" & Application.ExecutablePath & """ enable=yes"
                            process.Start()
                            process.WaitForExit()
                            Process.Start(Application.ExecutablePath)
                            End
                        Catch ex1 As Exception
                            MessageBox.Show(ex1.ToString() + " 2")
                        End Try
                    End If
                End If
            Finally
                Try : Client.Close() : Catch : End Try
            End Try
        Next

    End Sub

我收到以下异常,不知道如何解决: 异常图片

为什么端口没有连接?我已经关闭了所有的防火墙。让我知道我能做什么。

标签: .netvb.netsocketstcpsocket

解决方案


推荐阅读