我有使用tcp套接字的文件传输应用程序[server-client]。
当我发送单个文件时,它成功,但是当我发送包含许多文件的文件夹时,收到的文件已损坏,请注意来自客户端的已发送文件和来自服务器的已接收文件具有相同的大小(相同)字节数。)
服务器:
private void ReceiveX(Socket client, string destPath, long size, int bufferSize)
{
using (Stream stream = File.Create(destPath))
{
byte[] buffer = new byte[bufferSize];
long sum = 0;
int count = 0;
do
{
count = client.Receive(buffer, 0, buffer.Length, SocketFlags.None);
stream.Write(buffer, 0, count);
sum += count;
} while (sum < size);
}
}
客户:
private void SendX(Socket socket, string filePath, long size, int bufferSize, DoWorkEventArgs e)
{
using (Stream stream = File.OpenRead(filePath))
{
byte[] buffer = new byte[bufferSize];
long sum = 0;
int count = 0;
do
{
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
count = stream.Read(buffer, 0, buffer.Length);
socket.Send(buffer, 0, count, SocketFlags.None);
sum += count;
worker.ReportProgress((int)((sum * 100) / size));
} while (sum < size);
}
}
对于客户端和服务器,bufferSize为[4 * 1024] 上面的代码有什么问题吗?
客户端:在这里我遍历文件夹以发送文件:
private void SendDir(string path, int bufferSize, DoWorkEventArgs e)
{
using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);
listener.Bind(endpoint);
listener.Listen(1);
client.ReceiveFolder((IPEndPoint)listener.LocalEndPoint, fileList, Path.Combine(currAddress,Path.GetFileName(path)),bufferSize);
Socket socket = listener.Accept();
int count = 0;
foreach (_File file in fileList)
{
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
Console.WriteLine(++count);
SendX(socket, file.Path, file.Size, bufferSize, e);
}
socket.Dispose();
}
服务器,循环遍历从服务器接收的list<_File>
中的文件,它包含客户端要发送的文件信息(名称,路径,大小):
private void ReceiveFolderTh(IPEndPoint endpoint, List<_File> Files, string destDir, int bufferSize)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(IPAddress.Parse("127.0.0.1"), endpoint.Port);
foreach (_File file in Files)
{
Directory.CreateDirectory(destDir + Path.GetDirectoryName(file.Name));
ReceiveX(client, destDir + file.Name, file.Size, bufferSize);
}
client.Dispose();
}
所以我该如何解决这个问题?
答案 0 :(得分:0)
很可能问题是您的客户端在第一个文件上收到的数据太多,因为文件之间没有明确的分隔。您的接收代码是:
do
{
count = client.Receive(buffer, 0, buffer.Length, SocketFlags.None);
stream.Write(buffer, 0, count);
sum += count;
} while (sum < size);
您总是收到最多buffer.Length
(您说的是4,096)个字节。因此,如果第一个文件长度为100个字节,下一个文件长度为5,000个字节,那么服务器很可能正在发送一个4,096字节的数据包,您的代码将尽职尽责地接收并存储在第一个文件中。
您需要更改呼叫client.Receive
时要接收的字节数。类似的东西:
int bytesToReceive = Math.Min(buffer.Length, size - sum);
count = client.Receive(buffer, 0, bytesToReceive, SocketFlags.None);
这将阻止您阅读超出一个文件的末尾并进入下一个文件的开头。