【问题标题】:How To Send A File By Sockets On TCP Protocol In C#?如何在 C# 中通过 TCP 协议上的套接字发送文件?
【发布时间】:2019-06-24 20:18:00
【问题描述】:

我对这段代码有一个严重的问题。这是它应该如何工作的: 客户端连接到服务器并选择磁盘上的文件。在该客户端以这种格式(“文件”(4字节)+ FileNameLength(4字节)+ FileDataLength(4字节))向服务器发送(字节[]缓冲区)之后。 在该服务器创建一个具有此大小(新字节[FileNameLength + FileDataLength])的(字节[]缓冲区)之后,客户端以这种格式(字节[]缓冲区=文件名+文件数据)向服务器发送数据。服务器得到一个文件。问题出在这里,我在服务器中有一个 MessageBox 可以在接收到 FileName 后查看 FileName,但 MessageBox 始终为空白,并且它运行时间和时间。 有什么解决办法?

服务器:

    private Socket SServer = null;
    private Socket SClient = null;
    private byte[] buffer = new byte[1024];
    private byte[] FileNameLength = null;
    private byte[] FileSize = null;

    private void Server_Load(object sender, EventArgs e)
    {
        SServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        SServer.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13000));
        SServer.Listen(1);
        new Thread(() =>
        {
            SClient = SServer.Accept();
            MessageBox.Show("Connected.");
            new Thread(() => Receiver()).Start();
        }).Start();
    }

    private void Receiver()
    {
        buffer = new byte[1024];
        while (true)
        {
            Int32 AllLength = SClient.Receive(buffer, 0, buffer.Length, SocketFlags.None);
            byte[] Devider = new byte[4];
            Array.Copy(buffer, 0, Devider, 0, 4);
            string Devide = Encoding.ASCII.GetString(Devider);
            if (AllLength > 0)
            {
                if (Devide == "File")
                {
                    FileNameLength = new byte[4];
                    Array.Copy(buffer, 4, FileNameLength, 0, 4);
                    FileSize = new byte[4];
                    Array.Copy(buffer, 8, FileSize, 0, 4);
                    buffer = null;
                    buffer = new byte[BitConverter.ToInt32(FileNameLength, 0) + BitConverter.ToInt32(FileSize, 0)];
                }
                else
                {
                    byte[] FileNameBytes = new byte[BitConverter.ToInt32(FileNameLength, 0)];
                    Array.Copy(buffer, 0, FileNameBytes, 0, BitConverter.ToInt32(FileNameLength, 0));
                    byte[] FileBytes = new byte[BitConverter.ToInt32(FileSize, 0)];
                    Array.Copy(buffer, BitConverter.ToInt32(FileNameLength, 0), FileBytes, 0, BitConverter.ToInt32(FileBytes, 0));
                    string FileName = Encoding.ASCII.GetString(FileNameBytes);
                    MessageBox.Show(FileName);
                    buffer = null;
                    buffer = new byte[1024];
                }
            }
        }
    }

客户:

    private Socket SClient = null;
    string Path, FileName;

    private void Client_Load(object sender, EventArgs e)
    {
        SClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        SClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13000));
    }

    private void BT_SendFile_Click(object sender, EventArgs e)
    {
        byte[] FileLengthBytes = BitConverter.GetBytes(FileName.Length);
        byte[] FileBytes = File.ReadAllBytes(Path + FileName);
        byte[] buffer = new byte[FileLengthBytes.Length + FileBytes.Length + 4];
        //buffer = Encoding.Unicode.GetBytes("File") + FileLengthBytes + FileBytes;
        Array.Copy(Encoding.ASCII.GetBytes("File"), 0, buffer, 0, 4);
        Array.Copy(FileLengthBytes, 0, buffer, 4, FileLengthBytes.Length);
        Array.Copy(BitConverter.GetBytes(FileBytes.Length), 0, buffer, 8, 4);
        SClient.Send(buffer, 0, buffer.Length, SocketFlags.None);
        byte[] FileNameBytes = Encoding.ASCII.GetBytes(FileName);
        buffer = null;
        buffer = new byte[FileNameBytes.Length + FileBytes.Length];
        Array.Copy(FileNameBytes, 0, buffer, 0, FileNameBytes.Length);
        Array.Copy(FileBytes, 0, buffer, FileNameBytes.Length, FileBytes.Length);
        SClient.Send(buffer, 0, buffer.Length, SocketFlags.None);
    }

    private void BT_Browse_Click(object sender, EventArgs e)
    {
        OpenFileDialog N = new OpenFileDialog();
        if (N.ShowDialog() == DialogResult.OK)
        {
            TB_Address.Text = N.FileName;
            string[] Seperate = N.FileName.Split('\\');
            FileName = Seperate[Seperate.Length - 1];
            Path = null;
            foreach (string str in Seperate)
            {
                if (str != Seperate[Seperate.Length - 1])
                    Path += str + "\\";
            }
        }
    }

【问题讨论】:

  • 你不会在一个块中获得整个接收消息 TCP 的最大数据报大小约为 1500 字节。您的文件可能要大得多。所以你只需要从初始块中删除文件名和大小。尝试自己调试并确保正确提取文件名和大小以及文件的第一块。这是您在 CS 101 Basket Weaving 中进行的非常基本的调试。
  • 既然有FTPFTPs, SFTPSCP、...,为什么还要重新发明轮子?

标签: c# sockets tcp protocols sendfile


【解决方案1】:

正如这位朋友 (Fildor) 所说,我尝试阅读有关协议的更多信息。 谢谢你们两个,但我想如果我将文件分开并逐个发送,我可以发送孔文件。 我知道这可能很愚蠢,但我认为它有效。

【讨论】:

    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 2015-01-30
    • 2018-02-05
    • 2011-12-03
    • 2021-09-15
    • 2020-04-28
    • 2017-03-14
    • 2018-06-26
    相关资源
    最近更新 更多