简体   繁体   English

C# 如何使用 TCP 客户端发送 1GB 文件

[英]C# How to send a 1GB file using TCP client

public void SendFile(string remoteHostIP, int remoteHostPort, string longFileName, string shortFileName)
{
    byte[] fileNameByte = Encoding.ASCII.GetBytes(shortFileName);
    byte[] fileData = File.ReadAllBytes(longFileName);
    byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
    byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
    fileNameLen.CopyTo(clientData, 0);
    fileNameByte.CopyTo(clientData, 4);
    fileData.CopyTo(clientData, 4 + fileNameByte.Length);
    TcpClient clientSocket = new TcpClient(remoteHostIP, remoteHostPort);
    NetworkStream networkStream = clientSocket.GetStream();
    networkStream.Write(clientData, 0, clientData.GetLength(0));
    networkStream.Close();     
}

It is possible to use this function to send a 1GB file because the maximum file size i had been try to send now is only up to 400MB.可以使用此功能发送 1GB 文件,因为我现在尝试发送的最大文件大小仅为 400MB。 More than that is will cause an error of 'System.OutOfMemoryException'.不仅如此,还会导致“System.OutOfMemoryException”错误。 When i use another method to split the file into few parts but the server side can't receive the parts continuously and can only receive one of the part.当我使用另一种方法将文件分成几个部分但服务器端无法连续接收这些部分而只能接收一个部分时。

    private void splitBigFile(string FileInputPath, byte[] inputArray)
    {
        int port = 1113;

        double partSize = 104852000;
        int partSize2 = 104852000;

        string FolderOutputPath = "C:\\Users\\xx\\Desktop\\testing split";
        string currPartPath;
        string shortNameSplit;

        FileStream fileStream = new FileStream(FileInputPath, FileMode.Open);
        FileInfo fiSource = new FileInfo(txtFile.Text);
        double sourceLength = fiSource.Length;

        partNum = (int)Math.Ceiling((double)(sourceLength / partSize));

        for (int i = 0; i < partNum; i++)
        {
            if (i == (partNum - 1))
            {
                partSize2 = (int)fiSource.Length - (i * 104852000);
            }

            currPartPath = FolderOutputPath + "\\" + fiSource.Name + "." + String.Format(@"{0:D4}", i) + ".part";
            shortNameSplit = fiSource.Name + "." + String.Format(@"{0:D4}", i) + ".part";

            byte[] fileNameByte = Encoding.ASCII.GetBytes(shortNameSplit);

            byte[] readStream = new byte[partSize2];

            byte[] concateFile = new byte[5 + fileNameByte.Length];

            int ipSend = ((partNum - 1 - i) << 1);
            ipSend |= 0; // for differentiate ip or file
            byte[] byteSend = new byte[1];
            byteSend[0] = (byte)ipSend;

            fileStream.Read(readStream, 0, partSize2);
            byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

            byteSend.CopyTo(concateFile, 0);
            fileNameLen.CopyTo(concateFile, 1);
            fileNameByte.CopyTo(concateFile, 5);
            concateFile.CopyTo(inputArray, 0);
            readStream.CopyTo(inputArray, concateFile.Length);
            string ipAddress = "192.168.43.67";
            int sendport = 1113;
            //Task.Factory.StartNew(() => SendBigFileSize(ipAddress, sendport, inputArray[i]));
            Array.Clear(readStream, 0, readStream.Length);
            Array.Clear(fileNameLen, 0, fileNameLen.Length);
            Array.Clear(fileNameByte, 0, fileNameByte.Length);
            Array.Clear(byteSend, 0, byteSend.Length);
            Array.Clear(byteSend, 0, byteSend.Length);
        }
        fileStream.Close();
    }

Of course, just not all at once.当然,并不是一下子全部。 Send chunks at a time.一次发送块。

Besides, the maximum TCP packet size is 64k, and the MTU is 1500 bytes.此外,最大 TCP 数据包大小为 64k,MTU 为 1500 字节。 Your huge buffer is getting split up too, but you're using up a lot of memory doing it.您的巨大缓冲区也被拆分了,但是您正在使用大量内存。 Instead read 32MB (safe HDD I/O buffer size) at a time from your file and send it, then read the next bit.而是一次从文件中读取 32MB(安全 HDD I/O 缓冲区大小)并发送它,然后读取下一位。

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

相关问题 如何在c#中通过TCP / IP发送大文件(500MB-1GB)? - How to send large file ( 500MB - 1GB ) over TCP/IP in c#? 如何以高效的方式编写1GB文件C# - How to write 1GB file in efficient way C# 如何将大文件(12gb)分成多个1GB压缩(.gz)存档? C# - How to split big file(12gb) into multiple 1GB compressed(.gz) archives? C# 在C#中解析和上传&gt; 1GB的数据 - Parsing and uploading >1GB of data in C# 使用TCP c#将数据包从服务器发送到特定客户端 - Send packet from server to a specific client using TCP c# 使用tcp / ip将命令从客户端发送到C#中的服务器 - Send commands from client to server in C# using tcp/ip PHP - C# TCP 客户端/服务器如何发送字符串? - PHP - C# TCP Client / Server How to send Strings ? 如何通过 TCP 向带有节点和 C# 的客户端发送简单的 UInt - How to send a simple UInt over TCP to Client with Node and C# 如何通过 C# windows 应用程序发送数据并使用 TCP/IP 协议从 android 客户端接收数据 - How to send data by C# windows application and recieve it from android Client using TCP/IP protocal 如何快速创建具有“自然”内容的大型(&gt; 1gb)文本+二进制文件? (C#) - How can I quickly create large (>1gb) text+binary files with “natural” content? (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM