发送的文件有一些损坏的数据TCP套接字..如何解决? [英] Sent file has some corrupted data TCP sockets .. how to fix it?

查看:205
本文介绍了发送的文件有一些损坏的数据TCP套接字..如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的传输文件应用程序有问题..一切看起来都不错,并且按应有的方式工作..但后来我发现已发送文件的某些片段已损坏.
就像当我发送视频文件(.MP4)时一样,它发送成功,但是当我播放它的某些帧时,就像在快照图片中一样损坏了..我播放了源Mp4文件,该图片是如此完美,而且从未看过就像那张快照照片中一样.
所以这是我的代码..现在我对使用fs.Seek感到困惑..有人告诉我,它不会使新文件与源文件相同.

I have problem with my transfer files application .. everything looks great and works the way it should be .. but then I discovered that some pieces of the sent file is corrupted.
Like when I send a video file (.MP4) it sent successfully but when I play it some frames of it got corrupted like in the snapshot picture .. I played the source Mp4 file and the picture was so perfect and never looked like in that snapshot picture.
So here's my code .. I'm confused now about using fs.Seek .. someone told me it doesn't make the new file the same as the source file.

我正在使用SOCKETS TCP ..(.Net 4.0)

发送方法(客户):

FileStream fs;
long sum;
long fileSize;
byte[] data = null;
NetworkStream network; 
const int packetSize = 1024*8;

private void SendFile(string srcPath, string destPath)
    {
        string dest = Path.Combine(destPath, Path.GetFileName(srcPath));
        using (fs = new FileStream(srcPath, FileMode.Open, FileAccess.Read))
        {
            try
            {
                sum = 0;
                int count = 0;
                data = new byte[packetSize];
                //send the destination path and the file size to SERVER.
                SendCommand("receive<" + dest + "<" + fs.Length.ToString());
                while (sum < fileSize)
                {
                    if (fileSize - sum < packetSize)
                    {
                        count = fs.Read(data, 0, Convert.ToInt32(fileSize - sum));
                        network.Write(data, 0, Convert.ToInt32(fileSize - sum));
                    }
                    else
                    {
                        count = fs.Read(data, 0, data.Length);
                        network.Write(data, 0, data.Length);
                    }
                    fs.Seek(sum, SeekOrigin.Begin);
                    sum += count;
                    backgroundWorker1.ReportProgress((int)((sum * 100) / fileSize));
                }
                network.Flush();
            }
            finally
            {
                fs.Dispose();
                data = null;
            }
        }
    }

接收方法(服务器):

FileStream fs;
long sum;
long fileSize;
byte[] data = null;
NetworkStream network; 
const int packetSize = 1024*8;

public void Receive(string destPath, long fileSize)
    {
        using (fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
        {
            try
            {
                int count = 0;
                long sum = 0;
                data = new byte[packetSize];
                while (sum < fileSize)
                {
                    if (fileSize - sum < packetSize)
                    {
                        count = network.Read(data, 0, Convert.ToInt32(fileSize - sum));
                        fs.Write(data, 0, Convert.ToInt32(fileSize - sum));
                    }
                    else
                    {
                        count = network.Read(data, 0, data.Length);
                        fs.Write(data, 0, data.Length);
                    }
                    fs.Seek(sum, SeekOrigin.Begin);
                    sum += count;
                }
            }
            finally
            {
                fs.Dispose();
                data = null;
            }
        }
    }

推荐答案

如果您现在还没有解决,请提出以下代码.放进去一会儿(sum< fileSize).

In case you didn't solve it by now, I propose this code. Put it inside while (sum < fileSize).

发送:

    count = fs.Read(data, 0, packetSize);
    network.Write(data, 0, count);
    sum += count;
    backgroundWorker1.ReportProgress((int)((sum * 100) / fileSize));

并接收:

    count = network.Read(data, 0, packetSize);
    fs.Write(data, 0, count);
    sum += count;

这将确保您仅传输已读取的数据.请注意,您不需要 if 部分,因为Read将返回读取的字节数,因此无论您是在文件的开头还是结尾,Read都将读取所有可能的内容.限制您提供的参数.

This will ensure that you transfer just the data you have read. Notice that you do not need if part because Read will return number of bytes read, so it doesn't matter if you are at beginning or at end of the file, Read will read all it can up to limit you provided as parameter.

这篇关于发送的文件有一些损坏的数据TCP套接字..如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆