当我关闭服务器套接字时,如何阻止应用程序关闭。

时间:2012-01-12 01:39:22

标签: vb.net multithreading sockets asynchronous thread-safety

我使用异步套接字创建了一个服务器 - 客户端应用程序。我在服务器上关闭套接字时发现问题,应用程序如此关闭。我关闭套接字服务器的过程是:

  1. 终止监听线程。
  2. 关闭客户端套接字仍然连接到服务器。
  3. 关闭服务器套接字。
  4. 当我关闭服务器套接字时,如何阻止应用程序关闭?感谢

        Protected Friend Sub CloseServerSocket(ByVal IPAddress As IPAddress, ByVal socketPort As Integer)
    
        ' Terminate the listening thread. 
        If listeningThread.IsAlive Then listeningThread.Abort()
    
        ' Close the client socket is still connected to the server.
        For Each sock As Socket In workSocketList
            If sock.Connected Then
                Interlocked.Decrement(workCount)
                sock.Close()
            End If
        Next
    
        ' Close the server socket.
        serverSocket.Close()
        serverSocket = Nothing
    End Sub
    

1 个答案:

答案 0 :(得分:0)

Protected Friend Sub CloseServerSocket(ByVal IPAddress As IPAddress, ByVal socketPort As Integer)

' Terminate the listening thread. 
If listeningThread.IsAlive Then listeningThread.Abort()

' Close the server socket.
serverSocket.Close()
serverSocket = Nothing

' Close the client socket is still connected to the server.
For Each sock As Socket In workSocketList
    If sock.Connected Then
        Interlocked.Decrement(workCount)
        sock.Close()
    End If
Next

End Sub