【问题标题】:How to prevent the application is closed, when I close the server socket.当我关闭服务器套接字时,如何防止应用程序关闭。
【发布时间】:2012-02-08 09:19:01
【问题描述】:

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

  1. 终止监听线程。
  2. 关闭客户端套接字仍然连接到服务器。
  3. 关闭服务器套接字。

当我关闭服务器套接字时,如何防止应用程序关闭?谢谢

    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

【问题讨论】:

  • 我尝试将步骤 2 与 3 交换并成功。谢谢:)

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


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    相关资源
    最近更新 更多