.net - 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
为什么端口没有连接?我已经关闭了所有的防火墙。让我知道我能做什么。
解决方案
推荐阅读
- sql - 是否可以使用 Spring Data JPA 在没有 JOIN 命令的情况下进行选择操作?
- javascript - 实时显示日期时间问题 (JavaScript/JQuery)
- csv - 如何在 JMETER 中编码任何 CSV 数据并将其用作输入参数 ${}
- c# - 从具有关系的表中获取数据的最佳实践是什么 - C# Entity Framework
- angular - Jasmine 测试:预期为 SPY,但出现功能错误
- node.js - 在 react native 中使用 npm install 安装节点模块时出错
- c# - 如何获得任何 3+ 位整数的最后两位?
- ruby-on-rails - Ruby on Rails Heroku App 以状态 0 退出
- mysql - 更新元素后获取查询集计数的最佳方法
- c# - 每次 API 调用都必须使用常量变量