2016-03-19 25 views
2

UWPアプリケーションのシャットダウン中に破棄するStreamSocketがあります。 Socket接続を持っている私のクライアントは、アプリケーションがシャットダウンしていても接続がまだ生きていると考えています。UWP StreamSocketは、アプリケーションの再起動時にのみ強制的に閉じられます。

Socketを再初期化するときだけ、クライアントは「既存の接続は強制的に閉じられました」という例外を出します。

どのように接続されたPCが接続が閉じていることがわかるようにSocketを閉じることができますか?

+0

私は同じ問題を抱えていますが、解決策が見つかりましたか? –

答えて

-1

サンプルのクライアントコンポーネントは、TCPソケットを作成してネットワーク接続を行い、ソケットを使用してデータを送信し、ソケットを閉じます。サーバーコンポーネントは、着信ネットワーク接続ごとに接続ソケットを提供し、ソケットを使用してクライアントからデータを受信し、ソケットを閉じるTCPリスナーを設定します。

あなたはこのGitHubののURLを参照してもよい: 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); 
    } 
+1

私はすでにコード内でdisposeメソッドを呼び出していますが、その行のもう一方の端はソケットが閉じられたことに気づいていないようです。接続をreiniatingすると接続につながるように見えるときだけ、強制的に例外が閉じられます... – WJM

関連する問題