我遇到了tcp客户端问题。当客户端启动他们的表单时,他们向我发送一个client.connect请求,当我打开我的表单时,我接受传入的请求,这样我们就会连接并显示已连接,但当我关闭客户端表单时,它仍显示我们是即使我们没有连接也是如此。
Dim Listener As New TcpListener(4355)
Dim Client As New TcpClient
Dim Message As String
Private Sub Listen()
Try
Listener.Start()
Catch ex As Exception
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
'IP.Text = My.Settings.IP
Try
Dim Listening As New Thread(New ThreadStart(AddressOf Listen))
Listening.Start()
Catch ex As Exception
Application.Restart()
End Try
Sending1.Focus()
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles
Timer2.Tick
If Client.Connected = False Then
Label5.Text = "Disconnected"
Label5.ForeColor = Color.Red
Sending1.Enabled = False
Client = Listener.AcceptTcpClient() 'looping accept request
End If
If Client.Connected = True Then
Label5.Text = "Connected"
Label5.ForeColor = Color.Green
Sending1.Enabled = True
End If
End Sub
答案 0 :(得分:0)
您可以使用ElseIf
。
If Client.Connected = False Then
Label5.Text = "Disconnected"
Label5.ForeColor = Color.Red
Sending1.Enabled = False
Client = Listener.AcceptTcpClient()
ElseIf Client.Connected = True Then
Label5.Text = "Connected"
Label5.ForeColor = Color.Green
Sending1.Enabled = True
End If
希望它有所帮助:)