简体   繁体   English

C# 文件传输与 tcpclient 和服务器

[英]C# file transfer with tcpclient and server

When I send a file with the code below, some data (small amout) is missing.当我发送带有以下代码的文件时,缺少一些数据(少量)。 The file size doess not match on the receiver side.接收方的文件大小不匹配。 Sending a regular string is fine so theres no connection issue here.发送常规字符串很好,因此这里没有连接问题。 Im just looking for a minimal improvement to fix the issue, I will add error checking etc later.我只是在寻找解决问题的最小改进,稍后我将添加错误检查等。 Thanks.谢谢。 The code is mostly copied from some tutorial but i dont remember which though...该代码主要是从一些教程中复制的,但我不记得是哪个...

Client is the std.Net TcpClient class Client.Client is it's socket客户端是 std.Net TcpClient class Client.Client 是它的套接字

public void SendFile2(string fileName)
        {
            using (FileStream fs = File.OpenRead(fileName))
            {
                byte[] lenBytes = BitConverter.GetBytes((int)fs.Length);
                Client.Client.Send(lenBytes);

                byte[] buffer = new byte[1024];
                int bytesRead;
                fs.Position = 0;

                while ((bytesRead = fs.Read(buffer, 0, 1024)) > 0)
                    Client.Client.Send(buffer, bytesRead, SocketFlags.None);
            }
        }

        public bool ReceiveFile2(string fileName)
        {
            using (FileStream fs = File.Create(fileName))
            {
                byte[] lenBytes = new byte[4];

                if (Client.Client.Receive(lenBytes) < 4)
                    return false;

                long len = BitConverter.ToInt32(lenBytes, 0);
                byte[] buffer = new byte[1024];
                int bytesRead;

                while ((bytesRead = Client.Client.Receive(buffer)) > 0)
                    fs.Write(buffer, 0, bytesRead);

                return len == fs.Position;
            }
        }

SOLUTION:解决方案:

public void SendFile(string fileName)
    {
        using (FileStream fs = File.OpenRead(fileName))
        {
            byte[] lenBytes = BitConverter.GetBytes((int)fs.Length);
            Client.Client.Send(lenBytes);

            byte[] buffer = new byte[1024];
            int bytesRead;
            fs.Position = 0;

            while ((bytesRead = fs.Read(buffer, 0, 1024)) > 0)
                Client.Client.Send(buffer, bytesRead, SocketFlags.None);
        }
    }

    public bool ReceiveFile(string fileName)
    {
        using (FileStream fs = File.Create(fileName))
        {
            byte[] lenBytes = new byte[4];

            if (Client.Client.Receive(lenBytes) < 4)
                return false;

            long len = BitConverter.ToInt32(lenBytes, 0);
            byte[] buffer = new byte[1024];
            int bytesRead;

// Changed from here
            while (fs.Position < len)
            {
                bytesRead = Client.Client.Receive(buffer);
                fs.Write(buffer, 0, bytesRead);
            }
// To here

            return len == fs.Position;
        }
    }

I think this line can be a problem.我认为这条线可能是个问题。

if (Client.Client.Receive(lenBytes) < 4) and while ((bytesRead = Client.Client.Receive(buffer)) > 0) if (Client.Client.Receive(lenBytes) < 4)while ((bytesRead = Client.Client.Receive(buffer)) > 0)

You have two receives in your code.您的代码中有两个接收。

So you drop first bytes.所以你删除了第一个字节。 That can explain the differences you see in files sizes.这可以解释您在文件大小中看到的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM