I had this code prior to .net 4.5
_Connection = new TcpClient(hostname, port);
_Stream = _Connection.GetStream();
For Windows 8 App I changed to:
_Connection = new StreamSocket();
await _Connection.ConnectAsync(new HostName(hostname), port.ToString());
_DataReader = _Connection.InputStream.AsStreamForRead();
_DataWriter = _Connection.OutputStream.AsStreamForWrite();
I thought this to be the simplest solution as I don't have to change any underlying code anywhere else, as I am still using Stream
to read/write data.
This code does not work as expected though, I manage to write stuff successfully on the stream, but when it's time to read the stream, I keep getting '\n'
- which is far off from my expected response.
Found a solution to this. Hope someone finds it useful.
All i needed to do was flush the stream in _DataWriter
(this needs to be done everytime something is written to the stream) and then the _DataReader
stream started working as expected.