91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

C#使用TCP/IP傳輸大文件方法

c#
小樊
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
吉水县| 军事| 淮北市| 海丰县| 西华县| 宁化县| 平遥县| 石阡县| 甘南县| 大余县| 宜兰市| 长岛县| 兰州市| 宿松县| 石门县| 阿勒泰市| 咸宁市| 葵青区| 东城区| 济阳县| 博罗县| 新郑市| 广宁县| 宿迁市| 盱眙县| 凉山| 英吉沙县| 南宫市| 遵化市| 新和县| 八宿县| 于都县| 尚志市| 永胜县| 宁安市| 广平县| 文登市| 金秀| 清流县| 武鸣县| 桐庐县|