【问题标题】:Larger File Sizes are being incorrectly transfered with a NetworkStream使用 NetworkStream 错误地传输较大的文件大小
【发布时间】:2010-10-15 00:48:31
【问题描述】:

我正在通过 NetworkStream 传输文件,似乎当文件超过 5-10k 时,文件开始丢失数据和/或有巨大的空白间隙。

这是我所拥有的:

private string ReadandSaveFileFromServer(TcpClient clientATF, NetworkStream currentStream, string locationToSave)
{
    int fileSize = 0;
    string fileName = "";
    int bytesRead = 0;

    fileName = ReadStringFromServer(clientATF, currentStream);
    fileSize = ReadIntFromServer(clientATF, currentStream);

    FileStream fs = new FileStream(locationToSave + "\\" + fileName, FileMode.Create);

    byte[] fileSent = new byte[fileSize];

    while (currentStream.DataAvailable)
    {
        if (clientATF.Connected)
        {
            bytesRead = currentStream.Read(fileSent, 0, fileSent.Length);
            fs.Write(fileSent, 0, fileSent.Length);
        }
        else
        {
            break;
        }
    }

    fs.Flush();
    fs.Close();

    return fileName;
}

【问题讨论】:

    标签: c# sockets file-transfer networkstream


    【解决方案1】:

    不,您的缓冲区代码写错了。乍一看,我建议你改为fs.Write(fileSetn, 0, bytesRead)

    【讨论】:

      【解决方案2】:

      这里有问题:fs.Write(fileSent, 0, fileSent.Length);

      当你刚刚得到bytesRead字节时,你将fileSent.Length字节写入文件。

      更正的代码:

      fs.Write(fileSent, 0, bytesRead);
      

      【讨论】:

      • 还有一点:如果你在这行代码后断开连接: if (clientATF.Connected) 那么你在尝试读取流时会得到一个异常。我建议把它放在 try/catch 中
      • 他没有那行代码,如果对等端进行了有序关闭,他根本不会得到异常。你在说什么?
      • 检查对等方是否连接是设计上的异步方法,并且在返回 () 时,当他尝试读取流时会引发异常,因此流上的任何读/写 [bytesRead = currentStream.读取(文件发送,0,文件发送。长度); ] 我们不确定(例如在其他机器上)应该谨慎使用。
      • 除了通过读取或写入套接字之外,在 TCP 中无论是同步还是异步都无法检查对等方是否已连接。
      • 这就是为什么我建议将流读取放在 try/catch 中,因为如果底层套接字关闭,就会出现异常。使用过流的任何人都知道这一点。
      【解决方案3】:

      摆脱可用的测试并阅读,直到获得 EOS。这是对可用的滥用。

      【讨论】:

        【解决方案4】:

        这就是答案。 fs.Write() 错误是我的疏忽。谢谢大家的帮助。

          private string ReadandSaveFileFromServer(TcpClient clientATF, NetworkStream currentStream, string locationToSave)
            {
                int fileSize = 0;
                string fileName = "";
                int bytesRead = 0;
        
                fileName = ReadStringFromServer(clientATF, currentStream);
                fileSize = ReadIntFromServer(clientATF, currentStream);
        
                FileStream fs = new FileStream(locationToSave + "\\" + fileName, FileMode.Create);
        
                byte[] fileSent = new byte[fileSize];
        
                while (fs.Length != fileSize)
                {
        
                        bytesRead = currentStream.Read(fileSent, 0, fileSent.Length);
                        fs.Write(fileSent, 0, bytesRead);
        
                }
        
                fs.Flush();
                fs.Close();
        
                return fileName;
            }
        

        【讨论】:

          猜你喜欢
          • 2011-04-09
          • 1970-01-01
          • 2011-09-18
          • 2011-05-14
          • 1970-01-01
          • 2016-01-28
          • 1970-01-01
          • 2018-12-26
          • 1970-01-01
          相关资源
          最近更新 更多