【问题标题】:UWP StreamSocket is only forcibly closed when restarting appUWP StreamSocket 仅在重新启动应用程序时强制关闭
【发布时间】:2016-03-21 12:32:55
【问题描述】:

我有一个 StreamSocket,我在关闭我的 UWP 应用程序期间处理了它。拥有 Socket 连接的客户端仍然认为连接是活动的,即使应用程序已关闭。

只有在重新启动 Socket 时,我的客户端才会给出“现有连接被强制关闭”异常。

如何关闭 Socket 以使连接的 PC 知道连接已关闭?

【问题讨论】:

  • 我也有同样的问题,你找到解决方案了吗?

标签: c# sockets win-universal-app


【解决方案1】:

示例的客户端组件创建一个 TCP 套接字以建立网络连接,使用该套接字发送数据,并关闭该套接字。服务器组件设置一个 TCP 侦听器,为每个传入的网络连接提供一个已连接的套接字,使用该套接字从客户端接收数据,并关闭该套接字。

您可以参考这个 GitHub 网址: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/StreamSocket

希望对你有帮助。

 /// <summary>
    /// This is the click handler for the 'CloseSockets' button.
    /// </summary>
    /// <param name="sender">Object for which the event was generated.</param>
    /// <param name="e">Event's parameters.</param>
    private void CloseSockets_Click(object sender, RoutedEventArgs e)
    {
        object outValue;
        if (CoreApplication.Properties.TryGetValue("clientDataWriter", out outValue))
        {
            // Remove the data writer from the list of application properties as we are about to close it.
            CoreApplication.Properties.Remove("clientDataWriter");
            DataWriter dataWriter = (DataWriter)outValue;

            // To reuse the socket with another data writer, the application must detach the stream from the
            // current writer before disposing it. This is added for completeness, as this sample closes the socket
            // in the very next block.
            dataWriter.DetachStream();
            dataWriter.Dispose();
        }

        if (CoreApplication.Properties.TryGetValue("clientSocket", out outValue))
        {
            // Remove the socket from the list of application properties as we are about to close it.
            CoreApplication.Properties.Remove("clientSocket");
            StreamSocket socket = (StreamSocket)outValue;

            // StreamSocket.Close() is exposed through the Dispose() method in C#.
            // The call below explicitly closes the socket.
            socket.Dispose();
        }

        if (CoreApplication.Properties.TryGetValue("listener", out outValue))
        {
            // Remove the listener from the list of application properties as we are about to close it.
            CoreApplication.Properties.Remove("listener");
            StreamSocketListener listener = (StreamSocketListener)outValue;

            // StreamSocketListener.Close() is exposed through the Dispose() method in C#.
            // The call below explicitly closes the socket.
            listener.Dispose();
        }

        CoreApplication.Properties.Remove("connected");
        CoreApplication.Properties.Remove("adapter");
        CoreApplication.Properties.Remove("serverAddress");

        rootPage.NotifyUser("Socket and listener closed", NotifyType.StatusMessage);
    }

【讨论】:

  • 我已经在我的代码中调用了 dispose 方法,但是该行的另一端似乎没有注意到套接字已关闭。只有当重新发起连接似乎会导致连接被强制关闭异常...
猜你喜欢
  • 2015-08-24
  • 2012-09-15
  • 2012-04-30
  • 2015-06-24
  • 2018-03-17
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多