文件传输仅在第一次成功,但会连续失败

本文关键字:连续 失败 成功 传输 第一次 文件 | 更新日期: 2023-09-27 18:21:17

  • 这是一个使用套接字TCP的文件传输发送/接收程序。此代码包含在客户端和服务器应用程序中,并且仅在第一次正常工作。

  • 第二次,将要接收的一方获得0,转移完成。我该如何修复它以便可以多次使用?
    public static void sendFile(string filePath)
    {
        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        string fileName = Path.GetFileName(filePath);
        byte[] fileData;
        try
        {
            //sending file name and file size to the server
            busy = true;
            fileSize = fs.Length;
            byte[] fileDetial = null;
            string detail =  fileName + "," + fileSize.ToString();
            fileDetial = Encoding.ASCII.GetBytes(detail);
            client.Send(fileDetial);
            //sending file data to the server
            fileData = new byte[packetSize];
            count = 0;
            sum = 0;
            Program.thFP.Start();                           // running transfer rate
            Program.fp.StatusLabel("Sending data...");
            transferRate.timeLeft();
            while (sum < fileSize)
            {
                fs.Seek(sum, SeekOrigin.Begin);
                fs.Read(fileData, 0, fileData.Length);
                count = client.Send(fileData, 0, fileData.Length, SocketFlags.None);
                sum += count;
                Program.fp.ProgressBarFileHandler(sum, fileSize);
            }
        }
        finally
        {
            busy = false;
            fs.Close();
            fileData = null;
            MessageBox.Show(string.Format("{0} sent successfully", fileName));
        }
    }
    

    我想下面的代码一点问题都没有。。我认为问题出在SENDFILE方法上。。但这是receiveFile代码。。它可能有助于

    public static void ReceiveFile()
    {
        //receving file name and file size from server
        busy = true;
        byte[] commandData = new byte[1024];
        client.Receive(commandData);
        Console.WriteLine(Encoding.ASCII.GetString(commandData));
        string[] Command = Encoding.ASCII.GetString(commandData).Split(',');
        string fileName = Command[0];
        fileSize = Convert.ToInt64(Command[1]);
        Program.thFP.Start();                           // running transfer rate
        Program.fp.StatusLabel("Receiving data...");
        transferRate.timeLeft();
        // receiving the file data from server
        FileStream fs = new FileStream(@"D:'" + fileName, FileMode.Create, FileAccess.Write);
        byte[] fileData = new byte[packetSize];
        try
        {
            count = 0;
            sum = 0;
            while (sum < fileSize)
            {
                count = client.Receive(fileData,0,fileData.Length, SocketFlags.None);
                fs.Seek(sum, SeekOrigin.Begin);
                fs.Write(fileData, 0, fileData.Length);
                sum += count;
                Program.fp.ProgressBarFileHandler(sum,fileSize);
            }
        }
        finally
        {
            busy = false;
            fs.Close();
            fileData = null;
            MessageBox.Show(string.Format("{0} recevied successfully", fileName));
        }
    }
    
  • 文件传输仅在第一次成功,但会连续失败

    我已经修复了代码。问题出现在SendFile方法中,确切地说是在FileStream

    我应该处置它,这样我就可以用新路径再次初始化它

    finally
    {
        busy = false;
        fs.Dispose();   //here i fixed my mistake it was fs.Close()
        fileData = null;
        MessageBox.Show(string.Format("{0} sent successfully", fileName));
    }
    

    我建议您需要处理FileStream。我建议把它包在一个使用块里,这样即使抛出一个excpetion,它也会一直被处理掉。

    // receiving the file data from server
    using (FileStream fs = new FileStream(@"D:'" + fileName, FileMode.Create, FileAccess.Write))
    {
        byte[] fileData = new byte[packetSize];
        try
        {
            ...same code as before here...
        }
        finally
        {
             ...same code as before here...
        }
    }
    

    我总是建议在using块中包装流。这意味着,如果您稍后回来编辑代码,那么引入一个在完成后不处理流的代码路径的危险就更小了。

    我也会重组代码,这样你的"成功"消息就不会出现在finally块中——它可能应该在try块的末尾。当抛出异常时,您当前的实现将显示Success消息,因此文件可能是成功的。