2
votes

I have started with the development of a "WinRT" app ("Metro"-style apps for Windows 8). The app should read and write some data via a TCP stream. Reading works fine, but writing does not work. Below you can find the code which uses the full .NET Framework (which works):

var client = new TcpClient();
client.Connect(IPAddress.Parse("192.168.178.51"), 60128);
var stream = client.GetStream();
var writer = new StreamWriter(stream);
writer.WriteLine("ISCP\0\0\0\x10\0\0\0.....");
writer.Flush();

In comparison the following code does not work:

var tcpClient = new StreamSocket();
await tcpClient.ConnectAsync(new HostName("192.168.178.51"), "60128");
var writer = new DataWriter(_tcpClient.OutputStream);
writer.WriteString("ISCP\0\0\0\x10\0\0\0....");
writer.FlushAsync();

WriteString returns the correct length of the string (25), yet the other end does not receive the correct command. Via Wireshark I also see a correct package for the full .NET version, but not for the WinRT version.

How to fix this?

.NET version:

.NET network traffic

WinRT version:

WinRT network traffic

1
And what happens if you do same things synchronously?Nikolai Fetissov
WinRT does not provide a synchronous interface - async is the only way ;)ollifant
Have you looked at the StreamSocket sample?Jeff Brand

1 Answers

5
votes

After your call to writer.WriteString() you need to actually commit the date that is now on the buffer by calling writer.StoreAsync()

any call to wrtier.WriteXX will only store data in memory. Once you call writer.StoreAsync() that data in memory will be sent.

My guess is that StreamWrtiers.WriteLine does this for you in a single call.