代码拉取完成,页面将自动刷新
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace WatsonTcp
{
/// <summary>
/// Event arguments for when a stream is received from a client.
/// </summary>
public class StreamReceivedFromClientEventArgs
{
internal StreamReceivedFromClientEventArgs(string ipPort, Dictionary<object, object> metadata, long contentLength, Stream stream)
{
IpPort = ipPort;
Metadata = metadata;
ContentLength = contentLength;
DataStream = stream;
}
/// <summary>
/// The IP:port of the client.
/// </summary>
public string IpPort { get; }
/// <summary>
/// The metadata received from the client.
/// </summary>
public Dictionary<object, object> Metadata
{
get
{
return _Metadata;
}
set
{
if (value == null) _Metadata = new Dictionary<object, object>();
else _Metadata = value;
}
}
/// <summary>
/// The number of data bytes that should be read from DataStream.
/// </summary>
public long ContentLength { get; }
/// <summary>
/// The stream containing the message data.
/// </summary>
public Stream DataStream { get; }
/// <summary>
/// The data from DataStream.
/// Using Data will fully read the contents of DataStream.
/// </summary>
public byte[] Data
{
get
{
if (_Data != null) return _Data;
if (ContentLength <= 0) return null;
_Data = StreamToBytes(DataStream);
return _Data;
}
}
private Dictionary<object, object> _Metadata = new Dictionary<object, object>();
private byte[] _Data = null;
private byte[] StreamToBytes(Stream input)
{
if (input == null) throw new ArgumentNullException(nameof(input));
if (!input.CanRead) throw new InvalidOperationException("Input stream is not readable");
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。