2013-03-27 53 views
2

我想要做的是使用Windows.Networking.Sockets API獲取適用於Windows應用商店的基本界面。到目前爲止,我有這樣的:讓Windows Store Sockets與Netcat聊天?

public async void Test() 
    { 
     using (var socket = new StreamSocket()) 
     { 
      socket.Control.KeepAlive = false; 
      socket.Control.NoDelay = false; 
      await socket.ConnectAsync(new HostName("192.168.1.1"), "5555", SocketProtectionLevel.PlainSocket); 
      using (var writer = new DataWriter(socket.OutputStream)) 
      { 
       writer.UnicodeEncoding = UnicodeEncoding.Utf8; 
       writer.WriteString("yea!"); 
       //writer.WriteByte(0x50); //this doesn't work either to send raw ASCII 
       var t = writer.FlushAsync(); 
       while (t.Status != AsyncStatus.Completed) ; //just in case? 
      } 
     } 
    } 

到目前爲止,我似乎確實成功連接和斷開。但是,我從來沒有收到任何文字。

我的netcat命令(OpenBSD的路由器下運行)

$ nc -lv 5555 

如果我沒有netcat的運行,當我運行測試功能,它會拋出一個異常,一切按預期的方式。我在這裏做錯了什麼?

回答

1

這對我來說絕對沒有意義,但顯然DataWriter需要StoreAsync。我會認爲Flush應該這樣做,但顯然不是。另一個關於WinRT API的有趣部分。我的固定代碼:

 using (var socket = new StreamSocket()) 
     { 
      socket.Control.KeepAlive = false; 
      socket.Control.NoDelay = false; 
      await socket.ConnectAsync(new HostName("192.168.1.1"), "5555", SocketProtectionLevel.PlainSocket); 
      using (var writer = new DataWriter(socket.OutputStream)) 
      { 
       writer.UnicodeEncoding = UnicodeEncoding.Utf8; 
       writer.WriteString("yea!"); 
       await writer.StoreAsync(); 
      } 
     }