如何关闭多线程TCP侦听器
我研究了这个听众(到目前为止,我还没有尝试将任何客户连接到它)。 我有这个问题。
启动侦听器时,它可以开始使用,没有任何问题。因此,我在此应用程序中添加了一个停止侦听器按钮,然后尝试进行clucter.stop(),但它引发了一个例外: 在此处输入映像说明
type'system.ObjectDisposedexception'system.dlll中的第一个机会例外。
到目前为止,我还没有尝试在侦听器启动后连接任何客户端,我只使用了“启动”侦听器并停止侦听器按钮,而侦听器会引发异常。
我如何在没有例外的情况下优雅地关闭了这个听众?
Imports System.IO, System.Net, System.Net.Sockets
Public Class frmServer
Dim Listener As TcpListener
Dim Client As TcpClient
Dim ClientList As New List(Of ChatClient)
Dim sReader As StreamReader
Dim cClient As ChatClient
Sub xLoad() Handles Me.Load
Listener = New TcpListener(IPAddress.Any, 3818)
Listener.Start()
xUpdate("Server Started", False)
Listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listener)
End Sub
Sub AcceptClient(ByVal ar As IAsyncResult)
cClient = New ChatClient(Listener.EndAcceptTcpClient(ar))
AddHandler (cClient.MessageRecieved), AddressOf MessageRecieved
AddHandler (cClient.ClientExited), AddressOf ClientExited
ClientList.Add(cClient)
xUpdate("New Client Joined", True)
Listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listener)
End Sub
Sub MessageRecieved(ByVal Str As String)
xUpdate(Str, True)
End Sub
Sub ClientExited(ByVal Client As ChatClient)
ClientList.Remove(Client)
xUpdate("Client Exited", True)
End Sub
Delegate Sub _xUpdate(ByVal Str As String, ByVal Relay As Boolean)
Sub xUpdate(ByVal Str As String, ByVal Relay As Boolean)
On Error Resume Next
If InvokeRequired Then
Invoke(New _xUpdate(AddressOf xUpdate), Str, Relay)
Else
txtMainTextBox.AppendText(Str & vbNewLine)
If Relay Then Send(Str)
End If
End Sub
Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSendTextBox.KeyDown
End Sub
Sub Send(ByVal Str As String)
For i As Integer = 0 To ClientList.Count - 1
Try
ClientList(i).Send(Str)
Catch
ClientList.RemoveAt(i)
End Try
Next
End Sub
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
If Me.txtSendTextBox.Text = "" Then
Exit Sub
End If
xUpdate("Server Says: " & Me.txtSendTextBox.Text, True)
txtSendTextBox.Clear()
End Sub
Private Sub btnStopListener_Click(sender As Object, e As EventArgs) Handles btnStopListener.Click
Listener.Stop()
'this part stopped the listener but it throws an exception
End Sub
End Class
I worked on this listener ( I did not try to connect any clients to it until now).
I have this problem.
When starting the listener it started OK without any problem. so I added a stop listener button to this application and I tried to do listener.stop() but it throws an exception:
enter image description here
A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll
Until now I did not try to connect any client after the listener started, I only used start listener and stop listener buttons and the listener throws an exception.
How I gracefully shut down this listener without the exception ?
Imports System.IO, System.Net, System.Net.Sockets
Public Class frmServer
Dim Listener As TcpListener
Dim Client As TcpClient
Dim ClientList As New List(Of ChatClient)
Dim sReader As StreamReader
Dim cClient As ChatClient
Sub xLoad() Handles Me.Load
Listener = New TcpListener(IPAddress.Any, 3818)
Listener.Start()
xUpdate("Server Started", False)
Listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listener)
End Sub
Sub AcceptClient(ByVal ar As IAsyncResult)
cClient = New ChatClient(Listener.EndAcceptTcpClient(ar))
AddHandler (cClient.MessageRecieved), AddressOf MessageRecieved
AddHandler (cClient.ClientExited), AddressOf ClientExited
ClientList.Add(cClient)
xUpdate("New Client Joined", True)
Listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listener)
End Sub
Sub MessageRecieved(ByVal Str As String)
xUpdate(Str, True)
End Sub
Sub ClientExited(ByVal Client As ChatClient)
ClientList.Remove(Client)
xUpdate("Client Exited", True)
End Sub
Delegate Sub _xUpdate(ByVal Str As String, ByVal Relay As Boolean)
Sub xUpdate(ByVal Str As String, ByVal Relay As Boolean)
On Error Resume Next
If InvokeRequired Then
Invoke(New _xUpdate(AddressOf xUpdate), Str, Relay)
Else
txtMainTextBox.AppendText(Str & vbNewLine)
If Relay Then Send(Str)
End If
End Sub
Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSendTextBox.KeyDown
End Sub
Sub Send(ByVal Str As String)
For i As Integer = 0 To ClientList.Count - 1
Try
ClientList(i).Send(Str)
Catch
ClientList.RemoveAt(i)
End Try
Next
End Sub
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
If Me.txtSendTextBox.Text = "" Then
Exit Sub
End If
xUpdate("Server Says: " & Me.txtSendTextBox.Text, True)
txtSendTextBox.Clear()
End Sub
Private Sub btnStopListener_Click(sender As Object, e As EventArgs) Handles btnStopListener.Click
Listener.Stop()
'this part stopped the listener but it throws an exception
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论