查找传入文件的大小

时间:2013-12-19 20:15:43

标签: c# file sockets

在我的服务器程序中,我应该从客户端获取一个文件,但这可以是任何大小,所以我怎么能弄清楚它的大小,所以我可以为它设置缓冲区大小。我尝试了这段代码,但最后我只得到了1kb的文件夹,它已经无法使用了。

             private void checkRequest()
        { // Checks if request is a download or upload request and calls function that fits.
...
...
...
            else if (Request.Contains("Upload")) //If request is upload (Client wants to upload)
        {
            info = Request;
            nickName = Request.Substring(0, Request.IndexOf("Upload")); //Takes nickname
            info = info.Replace(nickName, ""); //Takes nickName of the client and deletes
            info = info.Replace("Upload", ""); //Deletes request.

            if (!sList.Contains(nickName)) //If nick name is unique
            {
                info = info.Substring(0, info.IndexOf("end"));

                sList.Add(nickName); //Adds nick name into a list.
                Receive(info); 
            }
        }
        else
        {
            serverSocket.Close(); // If any problem occurs server becomes offline.
        }
    }
    private void Receive(string receivedFileName)
    {
        byte[] buffer = new byte[1024]; //This is the part I can't fit anything.
        activity.AppendText("File downloading to " + fileDir + " destination");
        while (tempSocket.Receive(buffer) != 0)
        {
            File.WriteAllBytes(fileDir + "//" + fileName, buffer); //Creates a new file or overwrites it.

        }
        activity.AppendText("File downloaded..."); // Updates activity log(text box.)

    }

2 个答案:

答案 0 :(得分:1)

在执行File.WriteAllBytes()函数调用之前,请将Int64写入具有文件长度的套接字。

然后让您的客户首先查找该长度,并适当地设置缓冲区。

<强>备注

如果您希望TCP Stream只包含文件中的数据,您可以拥有一个包含多个套接字的协议:

  • Control Socket - 此套接字等待连接告诉它需要上传文件。客户端连接后,客户端将传递信息,例如file size。然后,服务器将使用新套接字的端口进行响应。 (数据套接字
  • 数据套接字 - 当客户端连接到此套接字时,它将立即发送整个文件。一旦服务器收到商定的字节数,它就会关闭套接字。

答案 1 :(得分:0)

由于您将套接字保持打开状态,因此您确实必须首先发送文件的大小,如安德鲁建议的那样。

但即便如此,也不要将所有内容都读入内存中的数组,但请考虑使用FileStream并将数据直接写入较小的块中。

类似的东西:

private void Receive(string receivedFileName)
{
    byte[] buffer = new byte[1024];
    // receive file size
    if (tempSocket.Receive(buffer, sizeof(ulong), SocketFlags.None) != sizeof(ulong))
    {
        // failed to receive the size
        return;
    }
    ulong fileSize = BitConverter.ToUInt64(buffer, 0);
    // receive file data
    activity.AppendText("File downloading to " + fileDir + " destination");
    using (FileStream stream = new FileStream(fileDir + "//" + fileName, FileMode.Create, FileAccess.Write)
    {
        ulong totalBytesReceived = 0;
        while (totalBytesReceived < fileSize)
        {
            int bytesReceived = tempSocket.Receive(buffer);
            if (bytesReceived > 0)
            {
                stream.Write(buffer, 0, bytesReceived);
                totalBytesReceived += (ulong)bytesReceived;
            }
            else
            {
                Thread.Sleep(1);
            }
        }
    }
    activity.AppendText("File downloaded..."); // Updates activity log(text box.)
}