无法将 TcpClient 连接到 TcpListener Visual Basic .NET
Cannot connect TcpClient to TcpListener Visual Basic .NET
我正在努力创建 TCP 服务器和客户端。我得到了很多像 here and here 这样的教程。但是还是不能成功!
这是我的代码:
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Runtime.InteropServices
Public Class Form1
Private Server As TcpListener = Nothing
Private ServerThread As Thread = Nothing
Private WithEvents Tray As New NotifyIcon
Private myClient As TcpClient = Nothing
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.115")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown
Server = New TcpListener(localAddr, 40000)
ServerThread = New Thread(AddressOf ConnectionListener)
ServerThread.IsBackground = True
ServerThread.Start()
TextBox1.Text = "Server is ready!"
' function I use to get all the TCP clients I can see in local web.
' You can find it by the first link (see at the top of the post)
lbComputers.DataSource = GetNetworkComputers()
End Sub
Private Sub ConnectionListener()
Try
Server.Start()
While True
myClient = Server.AcceptTcpClient
'myClient.Connect("MSK4", 40000) 'tried this: didn't work
Dim T As New Thread(AddressOf SomeClientActions)
T.Start(myClient)
TextBox2.Text = "Client connected" ' It's always empty :(
End While
myClient.Close()
End While
Catch ex As Exception
MessageBox.Show("Unable to Accept Connections", "Server Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
Application.ExitThread()
End Sub
Private Sub SomeClientActions(ByVal client As Object)
' ... do something with "client" in here ...
Invoke(Sub() TextBox2.Text = "This text is never appears :(")
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
'myClient.Close() 'error is here
Server.Stop()
End Sub
End Class
我有两个文本框(TextBox1 和 TextBox2)。我在第一个中得到 Server is ready!
但在第二个中什么也没有得到!所以我想我无法连接到我的服务器!此行之后的代码:
myClient = Server.AcceptTcpClient
永远不会执行。
我试着只使用
myClient.Connect("MSK4", 40000)
因为我的计算机名称是 MSK4,但后来我得到了我的异常和 MessageBox。
如何实现连接?我哪里做错了?
让服务器监听 127.0.0.115
意味着它将只接受来自特定 IP 的连接 。要侦听来自任何 IP 的连接,您需要指定 0.0.0.0
或 IPAddress.Any
.
至于建立连接,您必须仅使用 IP 地址,计算机名称将不起作用。
我正在努力创建 TCP 服务器和客户端。我得到了很多像 here and here 这样的教程。但是还是不能成功!
这是我的代码:
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Runtime.InteropServices
Public Class Form1
Private Server As TcpListener = Nothing
Private ServerThread As Thread = Nothing
Private WithEvents Tray As New NotifyIcon
Private myClient As TcpClient = Nothing
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.115")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown
Server = New TcpListener(localAddr, 40000)
ServerThread = New Thread(AddressOf ConnectionListener)
ServerThread.IsBackground = True
ServerThread.Start()
TextBox1.Text = "Server is ready!"
' function I use to get all the TCP clients I can see in local web.
' You can find it by the first link (see at the top of the post)
lbComputers.DataSource = GetNetworkComputers()
End Sub
Private Sub ConnectionListener()
Try
Server.Start()
While True
myClient = Server.AcceptTcpClient
'myClient.Connect("MSK4", 40000) 'tried this: didn't work
Dim T As New Thread(AddressOf SomeClientActions)
T.Start(myClient)
TextBox2.Text = "Client connected" ' It's always empty :(
End While
myClient.Close()
End While
Catch ex As Exception
MessageBox.Show("Unable to Accept Connections", "Server Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
Application.ExitThread()
End Sub
Private Sub SomeClientActions(ByVal client As Object)
' ... do something with "client" in here ...
Invoke(Sub() TextBox2.Text = "This text is never appears :(")
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
'myClient.Close() 'error is here
Server.Stop()
End Sub
End Class
我有两个文本框(TextBox1 和 TextBox2)。我在第一个中得到 Server is ready!
但在第二个中什么也没有得到!所以我想我无法连接到我的服务器!此行之后的代码:
myClient = Server.AcceptTcpClient
永远不会执行。 我试着只使用
myClient.Connect("MSK4", 40000)
因为我的计算机名称是 MSK4,但后来我得到了我的异常和 MessageBox。 如何实现连接?我哪里做错了?
让服务器监听 127.0.0.115
意味着它将只接受来自特定 IP 的连接 。要侦听来自任何 IP 的连接,您需要指定 0.0.0.0
或 IPAddress.Any
.
至于建立连接,您必须仅使用 IP 地址,计算机名称将不起作用。