使用C#将文件写入HDD会导致奇怪的结果... [英] Writing files to HDD using C# causes strange results...

查看:132
本文介绍了使用C#将文件写入HDD会导致奇怪的结果...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

首先,我对我的英语差劲感到抱歉,因为我来自德国.请原谅.

我的问题是,我正在尝试通过套接字将文件从一台计算机发送到另一台计算机.套接字不相互连接,而是连接到处理通信的代理.总而言之,通信工作正常,文件已传输.问题是文件,它是由接收者新创建的.它的大小与发送的大小完全相同,但是内容有误.例如,我制作了一个测试文件,其中包含如下行:

Hi everybody,

first of all I''m sorry about my poor english, as I am from Germany. I please you to excuse that.

My Problem is, I''m trying to send a file via a Socket from one Computer to another. The Sockets aren''t connected to each other, but to a Proxy, which handles the Communication. All in all the Communication is working fine and the File is transferred. The problem is the File, which gets newly created by the receiver. It has exactly the same size as the one which was sent, but the content is wrong. For example, I made a testfile, which contains lines like:

1 Test Test Test ...
2 Test Test Test ...
...



测试文件总共有100行,每行包含行号和30倍于单词"Test".收到的文件部分包含正确的内容,部分包含一些奇怪的东西,例如:



All in all the testfile has got 100 lines, each line containing the line number and 30 times the word "Test". The received File partially contains the right content, and partially contains strange things like:

Ýó’]$(ÇuêñâÊ%×%à–áxFøâQó’]$(ÇuêñâÊ%×%à–á



而且我的测试文件中的相应字符丢失了...

我看了一下是用Wireshark发送的,并弄清楚了测试文件的内容是以正确的方式发送的.没有奇怪的字节或类似的东西.与正在接收的数据包相同.然后,我查看了源代码并尝试对其进行调试.这样做的时候,我发现,非常缓慢地接收文件可以解决该问题,但这不适合我.现在我在想,将文件写入硬盘是问题所在,所以我确实尝试了一系列不同的技术将文件写入接收器的硬盘,但我找不到我的错误. br/>
提前一件事,我正在开发的应用程序是一种ICQ-Client,所以不要被senderUIN的东西弄糊涂了.好的,这是我的接收文件并将其写入硬盘的源代码:



And the corresponding characters within my testfile are missing...

I had a look at was is being sent with Wireshark and figured out, that the Content of the testfile is being sent in the right way. There are no strange Bytes or things like that. Same thing with the packets being received. Then I had a look at my source and tried to debug it. While doing that, I figured out, very very slow receiving of the file fixes the problem, but that isn''t suitable for me. Right now I am thinking, that writing the file to my hdd is the problem, so I really tried a hole bunch of different techniques for writing the file to the hdd of the receiver, but I just can''t find my mistake.

Just one thing in advance, the application I''m currently developing is a kind of ICQ-Client, so don''t be confused by the senderUIN stuff. Ok, here is my sourcecode for receiving the file and writing it to the hdd:

private bool recvFile(Socket tmpSocket, int TotalSize, string filename, string senderUIN, object signal)
        {
            Socket p2pSocket = tmpSocket;
            int totalSize = TotalSize;
            int totalReceived = 0;
            int received = 0;
            byte[] buffer = new byte[1000];
            BinaryWriter writer;

/* Create Directory if it isn''t existing */
            if (!System.IO.Directory.Exists(@"./Received Files"))
                System.IO.Directory.CreateDirectory(@"./Received Files");
            /* userspecific directory */
            string user_dir = "./Received Files/" + uin;
            if (!System.IO.Directory.Exists(@user_dir))<
                System.IO.Directory.CreateDirectory(@user_dir);
            writer = new BinaryWriter(File.Open(user_dir + "/" + filename, FileMode.Create));
            while (totalReceived < totalSize)
            {
                if (totalSize - totalReceived >= 1000)
                {
                    buffer = connection.recv_data(1000, p2pSocket);
                    received = 1000;
                    totalReceived += received;
                    writer.Write(buffer, 0, received);
                    Thread.Sleep(1);
                }
                else
                {
                    buffer = connection.recv_data(totalSize - totalReceived, p2pSocket);
                    received = totalSize - totalReceived;
                    totalReceived += received;
                    writer.Write(buffer, 0, received);
                    Thread.Sleep(1);
                }
                if (!Monitor.TryEnter(signal, 0))
                {
                    writer.Close();
                    try
                    {
                        File.Delete(user_dir + "/" + filename);
                    }
                    catch
                    {
                        MessageBox.Show("The file: " + filename + " could not be deleted.\r\nPlease delete it yourself.");
                    }
                    return false;
                }
                Monitor.Exit(signal);
                sic_.updateFileTransferWindow(senderUIN, (((float)totalReceived / (float)totalSize) * 100));
            }
            writer.Close();
            return true;
        }


谢谢您的事先帮助...


Thank you for your help in advance...

推荐答案

(ÇuêñâÊ%×%à–áx?FøâQó?']
(ÇuêñâÊ%×%à–áxFøâQó’]


(ÇuêñâÊ%×%à–á
(ÇuêñâÊ%×%à–á



而且我的测试文件中的相应字符丢失了...

我看了一下是用Wireshark发送的,并弄清楚了测试文件的内容是以正确的方式发送的.没有奇怪的字节或类似的东西.与接收的数据包相同.然后,我查看了源代码并尝试对其进行调试.这样做的时候,我发现,非常缓慢地接收文件可以解决该问题,但这不适合我.现在我在想,将文件写入硬盘是问题所在,所以我确实尝试了一系列不同的技术将文件写入接收器的硬盘,但我找不到我的错误. br/>
提前一件事,我正在开发的应用程序是一种ICQ-Client,所以不要被senderUIN的东西弄糊涂了.好的,这是我的接收文件并将其写入硬盘的源代码:



And the corresponding characters within my testfile are missing...

I had a look at was is being sent with Wireshark and figured out, that the Content of the testfile is being sent in the right way. There are no strange Bytes or things like that. Same thing with the packets being received. Then I had a look at my source and tried to debug it. While doing that, I figured out, very very slow receiving of the file fixes the problem, but that isn''t suitable for me. Right now I am thinking, that writing the file to my hdd is the problem, so I really tried a hole bunch of different techniques for writing the file to the hdd of the receiver, but I just can''t find my mistake.

Just one thing in advance, the application I''m currently developing is a kind of ICQ-Client, so don''t be confused by the senderUIN stuff. Ok, here is my sourcecode for receiving the file and writing it to the hdd:

private bool recvFile(Socket tmpSocket, int TotalSize, string filename, string senderUIN, object signal)
        {
            Socket p2pSocket = tmpSocket;
            int totalSize = TotalSize;
            int totalReceived = 0;
            int received = 0;
            byte[] buffer = new byte[1000];
            BinaryWriter writer;

/* Create Directory if it isn''t existing */
            if (!System.IO.Directory.Exists(@"./Received Files"))
                System.IO.Directory.CreateDirectory(@"./Received Files");
            /* userspecific directory */
            string user_dir = "./Received Files/" + uin;
            if (!System.IO.Directory.Exists(@user_dir))<
                System.IO.Directory.CreateDirectory(@user_dir);
            writer = new BinaryWriter(File.Open(user_dir + "/" + filename, FileMode.Create));
            while (totalReceived < totalSize)
            {
                if (totalSize - totalReceived >= 1000)
                {
                    buffer = connection.recv_data(1000, p2pSocket);
                    received = 1000;
                    totalReceived += received;
                    writer.Write(buffer, 0, received);
                    Thread.Sleep(1);
                }
                else
                {
                    buffer = connection.recv_data(totalSize - totalReceived, p2pSocket);
                    received = totalSize - totalReceived;
                    totalReceived += received;
                    writer.Write(buffer, 0, received);
                    Thread.Sleep(1);
                }
                if (!Monitor.TryEnter(signal, 0))
                {
                    writer.Close();
                    try
                    {
                        File.Delete(user_dir + "/" + filename);
                    }
                    catch
                    {
                        MessageBox.Show("The file: " + filename + " could not be deleted.\r\nPlease delete it yourself.");
                    }
                    return false;
                }
                Monitor.Exit(signal);
                sic_.updateFileTransferWindow(senderUIN, (((float)totalReceived / (float)totalSize) * 100));
            }
            writer.Close();
            return true;
        }


谢谢您的事先帮助...


Thank you for your help in advance...


您似乎正在假设您在recv_data()呼叫中收到的字符数,但是您无法检查您是否确实收到了该字符字节数.因此,您写到磁盘的内容可能只是已发送消息的一部分.您需要准确检查每个调用接收到多少个字节,然后只将其写入磁盘.您也可以进行一些错误检查.
You seem to be making assumptions about the number of characters received in your recv_data() calls but nowhere do you check that you do actually receive that number of bytes. Thus what you write to disk may be only part of the message that was sent. You need to check exactly how many bytes you receive on each call, and only write that amount to disk. You could also do with some error checking.


这篇关于使用C#将文件写入HDD会导致奇怪的结果...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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