【发布时间】:2016-06-29 23:03:12
【问题描述】:
我遇到了一个奇怪的问题。当我尝试通过 TCP 套接字发送文件时,发送信息的前 4 个字节会中断。
即发送和接收代码。
客户端
for (var i = 0; i < fileContentByte.Length; i += buffer.Length)
{
var size = (i + buffer.Length > fileContentByte.Length) ? fileContentByte.Length - i : buffer.Length;
clientSocket.Write(fileContentByte, i, size);
}
服务器端
using(var file = File.Create("C:\\test\\"+fileName.Substring(0, fileName.IndexOf('\0'))))
while(bytesReceived < numberOfBytes && (count = clientStream.Read(buffer, 0, buffer.Length)) > 0)
{
file.Write(buffer, 0, count);
bytesReceived += count;
}
这里是完整代码的链接 - http://pastebin.com/VwTgTxgb
【问题讨论】:
标签: c# networkstream tcpsocket