C# ReceiveBufferSize 显示不正确的值
C# ReceiveBufferSize shows incorrect value
client.ReceiveBufferSize
没有给出正确的接收字节大小。
所以我尝试使用 client.Client.SendFile("FileName.png")
来代替,但仍然给出相同的结果。我还进行了检查以确保它发送的图像超过 64KB,并且确实显示它发送的图像超过 64KB(来自客户端)。
服务器代码:
TcpListener server = new TcpListener(IPAddress.Any,12345);
TcpClient client = server.AcceptTcpClient();
NetworkStream clientstream = client.GetStream();
byte[] ImageByte = new byte[client.ReceiveBufferSize];
int ReceiveCount = await clientstream.ReadAsync(ImageByte,0,ImageByte.Length);
File.WriteAllBytes("Screenshot.png",ImageByte);
客户代码:
TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse("123.456.789.123"), 12345);
byte[] imagebyte = File.ReadAllBytes("ImageCaptured.temp");
client.GetStream().Write(imagebyte, 0, imagebyte.Length);
File.Delete("ImageCaptured.temp");
client.ReceiveBufferSize
应该显示大约 128KB,但最多只能显示 64KB。
TCP 不是 "byte[] in same byte[] out" 系统。您可以将写入拆分为多个读取,甚至可以将多个写入合并为一个读取。
您需要做的是在您的代码中实现 Message Framing。这意味着您需要发送接收方可以理解的额外数据,以了解在单个 "message".
中发送了多少数据
这是一个非常简单的例子,在图片之前发送长度然后另一端读取长度然后读取该字节数。
客户代码
using(TcpClient client = new TcpClient())
{
client.Connect(IPAddress.Parse("123.456.789.123"), 12345);
using (var clientStream = client.GetStream())
{
int imageLength = reader.ReadInt32();
byte[] imagebyte = new byte[imageLength);
int readBytes = 0;
while (readBytes < imageLength)
{
int nextReadSize = Math.Min(client.Available, imageLength - readBytes);
readBytes += await clientStream.ReadAsync(imagebyte, readBytes, nextReadSize);
}
File.WriteAllBytes("Screenshot.png",imageByte);
}
}
服务器代码
TcpListener server = new TcpListener(IPAddress.Any,12345);
using(TcpClient client = await server.AcceptTcpClientAsync())
{
byte[] imagebyte = File.ReadAllBytes("ImageCaptured.temp");
using(BinaryWriter writer = new BinaryWriter(client.GetStream()))
{
writer.Write(imagebyte.Length)
writer.Write(imagebyte, 0, imagebyte.Length);
}
File.Delete("ImageCaptured.temp");
}
客户端请注意,如果您不打算关闭 TcpClient 并发送更多数据,则需要将 using(BinaryWriter writer = new BinaryWriter(client.GetStream()))
替换为 using(BinaryWriter writer = new BinaryWriter(client.GetStream(), Encoding.UTF8, true))
client.ReceiveBufferSize
没有给出正确的接收字节大小。
所以我尝试使用 client.Client.SendFile("FileName.png")
来代替,但仍然给出相同的结果。我还进行了检查以确保它发送的图像超过 64KB,并且确实显示它发送的图像超过 64KB(来自客户端)。
服务器代码:
TcpListener server = new TcpListener(IPAddress.Any,12345);
TcpClient client = server.AcceptTcpClient();
NetworkStream clientstream = client.GetStream();
byte[] ImageByte = new byte[client.ReceiveBufferSize];
int ReceiveCount = await clientstream.ReadAsync(ImageByte,0,ImageByte.Length);
File.WriteAllBytes("Screenshot.png",ImageByte);
客户代码:
TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse("123.456.789.123"), 12345);
byte[] imagebyte = File.ReadAllBytes("ImageCaptured.temp");
client.GetStream().Write(imagebyte, 0, imagebyte.Length);
File.Delete("ImageCaptured.temp");
client.ReceiveBufferSize
应该显示大约 128KB,但最多只能显示 64KB。
TCP 不是 "byte[] in same byte[] out" 系统。您可以将写入拆分为多个读取,甚至可以将多个写入合并为一个读取。
您需要做的是在您的代码中实现 Message Framing。这意味着您需要发送接收方可以理解的额外数据,以了解在单个 "message".
中发送了多少数据这是一个非常简单的例子,在图片之前发送长度然后另一端读取长度然后读取该字节数。
客户代码
using(TcpClient client = new TcpClient())
{
client.Connect(IPAddress.Parse("123.456.789.123"), 12345);
using (var clientStream = client.GetStream())
{
int imageLength = reader.ReadInt32();
byte[] imagebyte = new byte[imageLength);
int readBytes = 0;
while (readBytes < imageLength)
{
int nextReadSize = Math.Min(client.Available, imageLength - readBytes);
readBytes += await clientStream.ReadAsync(imagebyte, readBytes, nextReadSize);
}
File.WriteAllBytes("Screenshot.png",imageByte);
}
}
服务器代码
TcpListener server = new TcpListener(IPAddress.Any,12345);
using(TcpClient client = await server.AcceptTcpClientAsync())
{
byte[] imagebyte = File.ReadAllBytes("ImageCaptured.temp");
using(BinaryWriter writer = new BinaryWriter(client.GetStream()))
{
writer.Write(imagebyte.Length)
writer.Write(imagebyte, 0, imagebyte.Length);
}
File.Delete("ImageCaptured.temp");
}
客户端请注意,如果您不打算关闭 TcpClient 并发送更多数据,则需要将 using(BinaryWriter writer = new BinaryWriter(client.GetStream()))
替换为 using(BinaryWriter writer = new BinaryWriter(client.GetStream(), Encoding.UTF8, true))