2016-03-19 17 views
2

UWP uygulamasının kapatılması sırasında elimden aldığım bir StreamSocket var. Soket bağlantısı olan istemcim, uygulamanın kapatılmış olmasına rağmen bağlantının hala canlı olduğunu düşünüyor.UWP StreamSocket, uygulamayı yeniden başlatırken sadece zorla kapatılmış

Yalnızca Soketi yeniden başlatırken, istemcim 'Mevcut bağlantı zorla kapatıldı' özel durumu veriyor.

Bağlantılı PC'nin bağlantının kapandığını bilmesi için bir Yuva'yı nasıl kapatabilirim?

+0

Aynı sorun var, bunun için bir çözüm buldunuz mu? –

cevap

-1

Örneğin istemci bileşeni bir ağ bağlantısı yapmak için bir TCP soketi oluşturur, veri göndermek için soketi kullanır ve soketi kapatır. Sunucu bileşeni, her gelen ağ bağlantısı için bağlı bir soket sağlayan bir TCP dinleyicisi kurar, istemciden veri almak için soketi kullanır ve soketi kapatır.

Bu GitHub URL'ye anlamlara gelebilir: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/StreamSocket

Umut bu size yardımcı olabilir.

/// <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

Kodumda zaten imha yöntemini arıyorum, ancak hattın diğer ucu soketin kapalı olduğunu fark etmiyor. Sadece bir bağlantıyı yeniden kurarken bir bağlantı kurulduğunda zorla kapatılan istisnadır ... – WJM

İlgili konular