c#

C#使用TCP/IP传输大文件方法

小樊
129
2024-09-14 23:28:44
栏目: 编程语言

在C#中,使用TCP/IP协议传输大文件可以通过分块读取和发送文件数据的方式实现。以下是一个简单的示例,展示了如何使用TCP/IP在C#中传输大文件:

首先,我们需要创建一个TCP服务器:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

class TcpServer
{
    public static void StartListening(int port)
    {
        TcpListener server = null;

        try
        {
            server = new TcpListener(IPAddress.Any, port);
            server.Start();

            Console.WriteLine("Server is listening on port " + port);

            while (true)
            {
                Console.WriteLine("Waiting for a client to connect...");
                TcpClient client = server.AcceptTcpClient();
                Console.WriteLine("Client connected.");

                NetworkStream stream = client.GetStream();

                // Read file and send it in chunks
                string fileName = "largefile.txt";
                using (FileStream fs = File.OpenRead(fileName))
                {
                    byte[] buffer = new byte[1024 * 1024]; // 1MB chunk size
                    int bytesRead;

                    while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        stream.Write(buffer, 0, bytesRead);
                    }
                }

                Console.WriteLine("File sent.");
                client.Close();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
        finally
        {
            if (server != null)
            {
                server.Stop();
            }
        }
    }
}

接下来,我们需要创建一个TCP客户端来接收文件:

using System;
using System.IO;
using System.Net.Sockets;

class TcpClient
{
    public static void Connect(string server, int port)
    {
        try
        {
            TcpClient client = new TcpClient(server, port);
            NetworkStream stream = client.GetStream();

            // Receive file in chunks and write it to disk
            string fileName = "received_largefile.txt";
            using (FileStream fs = File.Create(fileName))
            {
                byte[] buffer = new byte[1024 * 1024]; // 1MB chunk size
                int bytesRead;

                while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fs.Write(buffer, 0, bytesRead);
                }
            }

            Console.WriteLine("File received.");
            client.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
    }
}

最后,我们可以在主程序中启动服务器和客户端:

using System;

class Program
{
    static void Main(string[] args)
    {
        // Start server
        int port = 12345;
        TcpServer.StartListening(port);

        // Connect client
        string server = "127.0.0.1";
        TcpClient.Connect(server, port);
    }
}

这个示例中,服务器将大文件分成1MB大小的块,并通过网络流发送给客户端。客户端接收这些块并将它们写入到本地文件系统中。这种方法可以有效地传输大文件,而不会消耗大量内存。

0
看了该问题的人还看了