using (var connection = new TcpClientConnection())
using (var data = new MemoryStream()) {
connection.NetworkStream = new NetworkStream();
connection.Open(HostResolver.GetIPAddress("192.168.1.1"), 12345); //open the connection
connection.ReadData(data); //read two bytes with the size information
data.Seek(0, SeekOrigin.Begin);
var bytes = new byte[2];
data.Read(bytes, 0, 2);
int dataSize = bytes[1] + bytes[0] << 8; //decode the data size
data.Seek(0, SeekOrigin.End); //move to the end of the stream and read the rest of data
while (data.Length < dataSize + 2) { //two bytes with the size information are not included in data
connection.ReadData(data);
}
data.Seek(2, SeekOrigin.Begin); //skip two bytes with the size before extracting data
bytes = new byte[dataSize];
data.Read(bytes, 0, dataSize); //the received bytes
}
Have questions?
Article ID: 97, Created: December 27, 2017 at 1:39 PM, Modified: April 6, 2020 at 2:10 PM