本文整理汇总了C#中System.Net.Sockets.NetworkStream.ReadBytes方法的典型用法代码示例。如果您正苦于以下问题:C# NetworkStream.ReadBytes方法的具体用法?C# NetworkStream.ReadBytes怎么用?C# NetworkStream.ReadBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.NetworkStream
的用法示例。
在下文中一共展示了NetworkStream.ReadBytes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Connect
public void Connect(string hostname = "localhost", int port = 5858)
{
this.Dispose();
this.messages = new Queue<string>();
this.connection = new TcpClient(hostname, port);
this.stream = this.connection.GetStream();
this.readLoop = new Thread(() =>
{
byte[] packetBody;
byte[] packetEndMarker = this.EncodePacket("\r\n\r\n");
Regex packetHeaderPattern = new Regex(@"^Content-Length: (\d+)" + Encoding.Default.GetString(packetEndMarker) + "$");
int contentLength;
string message;
string header = this.DecodePacket(this.stream.ReadToMarker(packetEndMarker));
while (true)
{
if (!this.stream.DataAvailable)
{
Thread.Sleep(0);
continue;
}
header = this.DecodePacket(this.stream.ReadToMarker(packetEndMarker));
contentLength = int.Parse(packetHeaderPattern.Match(header).Groups[1].Value);
packetBody = stream.ReadBytes(contentLength);
message = this.DecodePacket(packetBody);
this.messages.Enqueue(message);
}
});
this.readLoop.Start();
}