通过 TCP 连接发送大文件

2023-11-26

我需要通过互联网将一些大文件发送到计算机。结果我打开了路由器上打算使用的端口并转发相应的IP地址。无论如何,让我向您展示我为了实现这一目标而一直在努力的课程。这些类对于小文件工作得很好,但有时对于大文件则失败。

这是服务器的代码:(它是一个控制台应用程序)

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

namespace ConsoleApplication20
{

    //server
    class Program
    {
        static Server s;
        public static void mm()
        {
            s = new Server("192.168.0.196");
            s.startServer();
            Console.Read();
        }

        static void Main(string[] args)
        {
           // Thread t = new Thread(new ThreadStart(mm));
           // t.Start();
            mm();
            Console.Read();
            s.disconnect();
        }


    }



    class MyTCP
    {
        protected const int MaxChunkSize = 4096;

        protected Int32 port { get; set; }
        protected string serverIP { get; set; }
        protected TcpClient client { get; set; }
        protected static NetworkStream stream { get; set; }

        protected void sendData(NetworkStream stream, Byte[] data)
        {
            // Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length);
        }

        protected String receiveData(NetworkStream stream)
        {
            // Buffer to store the response bytes.
            Byte[] data = new Byte[MaxChunkSize];

            // String to store the response ASCII representation.
            String responseData = String.Empty;

            // Read the first batch of the TcpServer response bytes.
            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            Console.WriteLine("Received: {0}", responseData);

            return responseData;
        }

        protected static Byte[] textToBytes(string text)
        {
            return System.Text.Encoding.ASCII.GetBytes(text);
        }

        public virtual void disconnect() { }

        public bool isServerConected { get { return client.Connected; } }
    }

    [Serializable]
    public class FileProperties 
    {
        public string FileName { get; set; }
        public string DestPath { get; set; }
        public double FileSize { get; set; }

        public FileAttributes fileAttributes { get; set; }
        public System.Security.AccessControl.FileSecurity FileSecurity { get; set; }
        public DateTime creationTime { get; set; }
        public DateTime lastAccessTime { get; set; }
        public DateTime lastWriteTime { get; set; }   
    }

    class Server: MyTCP
    {
        private System.IO.FileStream _FileStream;
        private static TcpListener server;
        private static bool disconect;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="localAddr">The ip address of the server</param>
        /// <param name="port">on what port the server going to be listening to?</param>
        /// <param name="autoStartServer">start listening for connections now? you may call the startserver() method latter...</param>
        public Server(string localAddr, Int32 port = 13000, bool autoStartServer = false)
        {
            this.port = port;
            this.serverIP = localAddr;

            if (autoStartServer)
                start();
        }

        /// <summary>
        /// Start listening for connections
        /// </summary>
        public void startServer()
        {
            start();
        }

        public override void disconnect()
        {
            // Close everything.
            stream.Close();
            client.Close();
            server.Stop();
            disconect = true;
        }


        void start()
        {
            server = null;

            try
            {
                // TcpListener server = new TcpListener(port);
                server = new TcpListener(IPAddress.Parse(serverIP), port);

                // Start listening for client requests.
                server.Start();

                // Buffer for reading data
                Byte[] bytes = new Byte[MaxChunkSize];
                String data = null;

                // Enter the listening loop.
                while (disconect==false)
                {
                    Console.Write("Waiting for a connection... ");

                    // Perform a blocking call to accept requests.
                    // You could also user server.AcceptSocket() here.
                    client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");




                    // Get a stream object for reading and writing
                    stream = client.GetStream();



                    int i;
                    try
                    {
                        // Loop to receive all the data sent by the client.
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {


                            // Translate data bytes to a ASCII string.
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                            Console.WriteLine("Received: {0}", data);

                            if (data.ToUpper().Contains("<sendFile>".ToUpper()))
                            {
                                receiveFile(bytes);
                            }



                            continue;


                        }
                    }
                    catch { }

                    // Shutdown and end connection
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }


            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }


        void receiveFile(Byte[] bytes)
        {
            // send 1
            sendData(stream, textToBytes("<1>"));

            // receive 2
            int length = stream.Read(bytes, 0, bytes.Length);
            byte[] tempA = new byte[length];
            for (int k = 0; k < length; k++)
                tempA[k] = bytes[k];

            Stream ms = new MemoryStream(tempA);
            FileProperties p = new FileProperties();
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());

            try
            {
                p = (FileProperties)x.Deserialize(ms);

                if (Directory.Exists(p.DestPath))
                {
                    //send 3
                    sendData(stream, textToBytes("<3>"));
                }
                else
                {
                    //send 3
                    sendData(stream, textToBytes("<no>"));
                    return;
                }
            }
            catch
            {
                //send 3
                sendData(stream, textToBytes("<no>"));
                return;
            }



            int i;

            string temp = Path.Combine(new string[]{ p.DestPath, p.FileName + ".temp"});

            _FileStream = new System.IO.FileStream(temp, System.IO.FileMode.Create, System.IO.FileAccess.Write);

                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    if (i == 11 & System.Text.Encoding.ASCII.GetString(bytes, 0, i).ToUpper().Equals("</sendFile>".ToUpper()))
                    {
                        _FileStream.Close();

                        Console.WriteLine("D!");

                        File.SetAttributes(temp, p.fileAttributes);
                        File.SetAccessControl(temp, p.FileSecurity);
                        File.SetCreationTime(temp, p.creationTime);
                        File.SetLastAccessTime(temp, p.lastAccessTime);
                        File.SetLastWriteTime(temp, p.lastWriteTime);

                        if(File.Exists(temp.Substring(0, temp.Length - 4)))
                            File.Delete(temp.Substring(0, temp.Length - 4));

                        File.Move(temp, temp.Substring(0, temp.Length - 4));


                        //sendData(stream, textToBytes("<done>"));

                        Console.WriteLine("Done!");

                        return;
                    }
                    _FileStream.Write(bytes, 0, i);

                }



            return;

        }
    }
}

我的客户的代码是:

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


namespace WpfApplication23sdfd
{

    [Serializable]
    public class FileProperties 
    {
        public string FileName { get; set; }
        public string DestPath { get; set; }
        public double FileSize { get; set; }

        public FileAttributes fileAttributes { get; set; }
        public System.Security.AccessControl.FileSecurity FileSecurity { get; set; }
        public DateTime creationTime { get; set; }
        public DateTime lastAccessTime { get; set; }
        public DateTime lastWriteTime { get; set; }
    }

    abstract class MyTCP
    {
        protected const int MaxChunkSize = 4096;

        protected Int32 port { get; set; }
        protected string serverIP { get; set; }
        protected TcpClient client { get; set; }
        protected static NetworkStream stream { get; set; }

        protected void sendData(NetworkStream stream, Byte[] data)
        {

            // Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length);

            // Receive the TcpServer.response.
        }

        protected String receiveData(NetworkStream stream)
        {
            // Buffer to store the response bytes.
            Byte[] data = new Byte[MaxChunkSize];

            // String to store the response ASCII representation.
            String responseData = String.Empty;

            // Read the first batch of the TcpServer response bytes.
            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            Console.WriteLine("Received: {0}", responseData);

            return responseData;
        }

        protected static Byte[] textToBytes(string text)
        {
            return System.Text.Encoding.ASCII.GetBytes(text);
        }

        public virtual void disconnect() { }

        public bool isServerConected { get { return client.Connected; } }
    }

    //client
    class Client: MyTCP
    {



        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="serverIP">the ip address of the server</param>
        /// <param name="port">through what port is the connection going to be established</param>
        public Client(string serverIP, Int32 port = 13000, bool autoConnect = false)
        {
            this.port = port;
            this.serverIP = serverIP;

            if (autoConnect)
                connect();
        }


        public bool connect()
        {
            Byte[] data = System.Text.Encoding.ASCII.GetBytes("connect");

            // Create a TcpClient.
            // Note, for this client to work you need to have a TcpServer 
            // connected to the same address as specified by the server, port
            // combination.
            try
            {
                client = new TcpClient(serverIP, port);

                // Get a client stream for reading and writing.
                //  Stream stream = client.GetStream();
                stream = client.GetStream();

                return true;


            }
            catch
            {
                return false;
            }

        }

        public override void disconnect()
        {
            // Close everything.
            stream.Close();
            client.Close();
        }

        static void ConnectOld(String server, Byte[] data)
        {
            try
            {
                // Create a TcpClient.
                // Note, for this client to work you need to have a TcpServer 
                // connected to the same address as specified by the server, port
                // combination.
                Int32 port = 13000;
                TcpClient client = new TcpClient(server, port);



                // Get a client stream for reading and writing.
                //  Stream stream = client.GetStream();

                NetworkStream stream = client.GetStream();

                // Send the message to the connected TcpServer. 
                stream.Write(data, 0, data.Length);



                // Receive the TcpServer.response.

                // Buffer to store the response bytes.
                data = new Byte[256];

                // String to store the response ASCII representation.
                String responseData = String.Empty;

                // Read the first batch of the TcpServer response bytes.
                Int32 bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);

                // Close everything.
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }

            Console.WriteLine("\n Press Enter to continue...");
            Console.Read();
        }

        public void sendFile(string file, string destPath = "c:\\")
        {

            //let server know what you are going to be doing...
            sendData(stream, textToBytes("<sendFile>"));

            FileProperties p = new FileProperties { 
                creationTime = File.GetCreationTime(file), 
                fileAttributes = File.GetAttributes(file), 
                FileSecurity = File.GetAccessControl(file), 
                lastAccessTime = File.GetLastAccessTime(file), 
                lastWriteTime = File.GetLastWriteTime(file),
                 DestPath = destPath,
                 FileName = Path.GetFileName(file)
            };


            // receive 1
            if (!receiveData(stream).ToUpper().Contains("<1>".ToUpper()))
            {
                MessageBox.Show("Error comunicating with server");
                return;
            }

            // send object p to server
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
            x.Serialize(stream, p); // send 2

            //recieve 3
            if (!receiveData(stream).ToUpper().Contains("<3>".ToUpper()))
            {
                MessageBox.Show("Error incorrect parameters sent to server");
                return;
            }


            System.IO.FileStream streamFile = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);



            while (true)
            {
                byte[] chunk = new byte[MaxChunkSize];

                int index = 0;
                // There are various different ways of structuring this bit of code.
                // Fundamentally we're trying to keep reading in to our chunk until
                // either we reach the end of the stream, or we've read everything we need.
                while (index < chunk.Length)
                {
                    int bytesRead = streamFile.Read(chunk, index, chunk.Length - index);


                    if (bytesRead == 0)
                    {
                        break;
                    }
                    if (bytesRead < MaxChunkSize)
                    {
                        byte[] temp = new byte[bytesRead];

                        for (var i = 0; i < bytesRead; i++)
                            temp[i] = chunk[i];

                        chunk = temp;
                    }


                    index += bytesRead;
                }
                if (index != 0) // Our previous chunk may have been the last one
                {
                    sendData(stream,chunk); // index is the number of bytes in the chunk
                }
                if (index != chunk.Length) // We didn't read a full chunk: we're done
                {

                    sendData(stream, textToBytes("</sendFile>".ToUpper()));

                    //receiveData(stream);//wait recall missing to check results

                    return;
                }
            }

        }

    }
}

我实例化客户端类的方法是提供服务器的 IP 地址,如下所示:

Client c = new Client("192.168.0.196");
c.sendFile(@"A:\Users\Tono\Desktop\a.mp4");

在执行该代码之前,服务器必须先运行。

我不知道为什么使用套接字通过互联网发送文件如此复杂。我不知道 WCF,这就是为什么我花了很多时间创建这个类。也许已经有一些内置的类可以让我通过互联网将文件发送到另一台计算机。我只知道网络的基础知识,如果我能通过一个简单的课程来完成它,那就太好了。我不明白为什么我的课程总是不起作用?如果我增加缓冲区大小,我的课程会更有效率吗?在发送更多字节之前我是否必须等待或暂停我的程序?如果有人能告诉我这门课有什么问题,那就太好了。它们对于小文件工作得很好,但对于大文件有时不起作用......


这是我发送大文件的代码。 一些技巧:

  • 检查你的缓冲区大小。如果太大就会失败。
  • 套接字标志。部分标志效果最好。
  • 由于传输时间的原因,需要套接字超时。

Client :

string IPAddress = "";
        int Port = 500;

        string Filename = @"C:\Users\Ben\Desktop\TT.zip";


        int bufferSize = 1024;
        byte[] buffer = null;
        byte[] header = null;


        FileStream fs = new FileStream(Filename, FileMode.Open);
        bool read = true;

        int bufferCount = Convert.ToInt32(Math.Ceiling((double)fs.Length / (double)bufferSize));



        TcpClient tcpClient = new TcpClient(IPAddress, Port);
        tcpClient.SendTimeout = 600000;
        tcpClient.ReceiveTimeout = 600000;

        string headerStr = "Content-length:" + fs.Length.ToString() + "\r\nFilename:" + @"C:\Users\Administrator\Desktop\" + "test.zip\r\n";
        header = new byte[bufferSize];
        Array.Copy(Encoding.ASCII.GetBytes(headerStr), header, Encoding.ASCII.GetBytes(headerStr).Length);

        tcpClient.Client.Send(header);

        for (int i = 0; i < bufferCount; i++)
        {
            buffer = new byte[bufferSize];
            int size = fs.Read(buffer, 0, bufferSize);

            tcpClient.Client.Send(buffer,size,SocketFlags.Partial);

        }

        tcpClient.Client.Close();

        fs.Close();

服务器 :

int Port = 500;

        TcpListener listener = new TcpListener(IPAddress.Any, Port);
        listener.Start();


        Socket socket = listener.AcceptSocket();

        int bufferSize = 1024;
        byte[] buffer = null;
        byte[] header = null;
        string headerStr = "";
        string filename = "";
        int filesize = 0;


        header = new byte[bufferSize];

        socket.Receive(header);

        headerStr = Encoding.ASCII.GetString(header);


        string[] splitted = headerStr.Split(new string[] { "\r\n" }, StringSplitOptions.None);
        Dictionary<string, string> headers = new Dictionary<string, string>();
        foreach (string s in splitted)
        {
            if (s.Contains(":"))
            {
                headers.Add(s.Substring(0,s.IndexOf(":")), s.Substring(s.IndexOf(":") + 1));
            }

        }
        //Get filesize from header
        filesize = Convert.ToInt32(headers["Content-length"]);
        //Get filename from header
        filename = headers["Filename"];

        int bufferCount = Convert.ToInt32(Math.Ceiling((double)filesize / (double)bufferSize));


        FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);

        while(filesize > 0)
        {
            buffer = new byte[bufferSize];

            int size = socket.Receive(buffer,SocketFlags.Partial);

            fs.Write(buffer,0,size);

            filesize -= size;
        }


        fs.Close();

希望这会对某人有所帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 TCP 连接发送大文件 的相关文章

随机推荐

  • 如何为 List 实现 Parcelable

    我正在尝试在我的可打包中传递一个列表 public class MetaDados implements Parcelable private List
  • 哈希表与 C++ 中的 STL 映射

    我正在尝试学习 C 地图 只是想知道STL图的实现 我读到它采用了二叉搜索树 STL中有哈希表的实现吗 STL映射到底是如何存储键值对的 典型的 STL 实现基于红黑树 C TR1 提供了 std tr1 unordered map 它使用
  • 如何在 JMeter 中进行 OAuth 2.0 身份验证?

    我正在尝试对一些需要身份验证的 API OAuth 2 0 进行功能测试 并在 JMeter 中进行模拟 我正在尝试验证 Azure 云的 OAuth 服务 有没有人能够成功创建 JMeter HTTP 请求来针对 OAuth 2 0 进行
  • 在文本选择上显示自定义菜单

    您好 我希望能够在用户选择一些与媒体提供的文本非常相似的文本时显示自定义菜单 或上下文菜单 如何实现这样的事情 我知道本机 jquery 上下文菜单插件 但我如何知道用户何时选择文本 浏览器的onselect似乎仅在输入元素上受支持 这是一
  • PHP 的合并函数?

    许多编程语言都有合并函数 返回第一个非 NULL 值 example 遗憾的是 2009 年 PHP 还没有做到这一点 在 PHP 本身获得合并函数之前 在 PHP 中实现一个合并函数的好方法是什么 php 5 3 中有一个新的运算符可以执
  • 如何在 cmd start 中使用引号?

    这是我想做的事 start wait c Program Files NetDrive2 nd2cmd exe c m t ftp blabla If I do start wait c Program Files NetDrive2 nd
  • 如何在 Anaconda Env 中启动 Visual Studio Code

    我在 Windows 10 PC 上的 Visual Studio Code 中运行 Python 程序时遇到问题 当我从终端启动 python 会话并说import cv2我没有收到任何错误 当我通过命令提示符并启动时VSCode从环境中
  • 为什么 UI 元素必须始终从 UI 线程创建/更新?

    为什么 UI 元素必须始终从 UI 线程创建 更新 在 几乎 所有编程语言中 UI 元素只能从 UI 线程安全地访问 修改 我知道这是一个标准的并发访问和同步问题 但真的有必要吗 这种行为是由编程语言还是操作系统强加的 有没有什么编程语言可
  • Django - 仅包含日期部分的分组

    MyModel objects filter created at gte 2011 03 09 created at lte 2011 03 11 values created at status annotate status coun
  • 作为 Cowboy 客户端,您如何使用 Gun?

    我跟着入门说明对于 Cowboy 我让 Cowboy 在端口 8080 上运行并侦听 然后我得到了Hello Erlang 当我进入时的回应http localhost 8080在我的浏览器中 现在 如何使用 Gun 连接到 Cowboy
  • 在类中,“using Base::BaseOfBase;”应该做什么?

    考虑这段代码 include
  • GWT 中的配置参数存储在哪里?

    我可以在客户端哪里存储配置参数 我无法在 Servlet init 参数 web xml 中存储参数 因为我必须使用 PHP 那么我如何在客户端存储初始化应用程序参数 例如 PHP 脚本位置 一些密码等 为此 您有以下选择 将数据存储在客户
  • 对数组进行排序并反映另一个数组中的变化

    我有一个双精度数组 用 Java 编写 arr1我想要排序 最有可能的第一个选项是实用方法Arrays sort double 我的想法是我想要相同的更改 例如 值index i与值互换index j in arr1 反映在另一个整数数组中
  • 如何更改 Flutter 中 EditText 上气泡(光标下)的颜色

    如何更改在 Text 或 TextFormField 或 Flutter 中选择文本时出现的气泡的颜色 Here是同样的问题 但是对于本机代码 根据这个颤振文档 textSelectionHandleColor已弃用 你应该使用select
  • Scala 对现有变量的多重赋值

    我可以做类似的事情 def f Tuple2 String Long val a b f 如果变量已经存在怎么办 我在过滤器上运行相同的数据集 但我不想链接它们 长名称等 这是我尝试过的 但它抱怨期待 而不是最后一行的 var a init
  • 为什么原型函数不影响console.log?

    我制作了原型Function这样它就有一个 getBody 函数 Function prototype getBody function Get content between first and last var m this toStr
  • 列表视图有多个分割按钮吗?

    基于 JQuery Mobile 的示例拆分按钮列表我正在尝试在 Android 中生成一个列表视图组件 右侧有两个额外的按钮 一个紧挨着另一个 问题在于该代码仅生成一个按钮 第二个按钮作为当前项目的链接添加 这是我的代码 ul li a
  • 什么是控制字符的正则表达式?

    我试图匹配 c 形式的控制字符 其中 c 是控制字符的任何有效字符 我有这个正则表达式 但它当前不起作用 z 我认为问题在于插入符 是正则表达式解析引擎的一部分 匹配以下形式的 ASCII 文本字符串 X使用模式 仅此而已 匹配以下形式的
  • 如何将自己从事件处理程序中删除?

    我想做的基本上是从事件中删除函数 而不知道函数的名称 我有一个FileSystemWatcher 如果创建 重命名文件 它会检查其名称 如果匹配 则会将其移动到特定位置 但是 如果文件被锁定 它会生成一个附加到计时器的滴答事件的 lambd
  • 通过 TCP 连接发送大文件

    我需要通过互联网将一些大文件发送到计算机 结果我打开了路由器上打算使用的端口并转发相应的IP地址 无论如何 让我向您展示我为了实现这一目标而一直在努力的课程 这些类对于小文件工作得很好 但有时对于大文件则失败 这是服务器的代码 它是一个控制