简体   繁体   English

如何在C#中通过TCP协议上的套接字发送文件?

[英]How To Send A File By Sockets On TCP Protocol In C#?

I have a serious problem with this code. 我有这个代码的严重问题。 This is how it should work: Client connects to server and choose a file on disk. 它应该这样工作:客户端连接到服务器并选择磁盘上的文件。 after that client sends a (byte[] buffer) to the server by this format("File" (4 bytes) + FileNameLength (4 bytes) + FileDataLength (4 bytes)). 之后,该客户端以这种格式(“文件”(4个字节)+ FileNameLength(4个字节)+ FileDataLength(4个字节))向服务器发送一个(byte []缓冲区)。 After that server creates a (byte[] buffer) with this size (new byte[FileNameLength + FileDataLength]).So client sends a data to the server by this format(byte[] buffer = FileName + FileData). 之后,该服务器创建一个具有该大小(新的字节[FileNameLength + FileDataLength])的(字节[]缓冲区)。因此,客户端以这种格式(字节[]缓冲区= FileName + FileData)向服务器发送数据。 And the server gets a file. 然后服务器获取一个文件。 The problem is here that i have a MessageBox in the Server to see the FileName after receiving that but MessageBox is always blank and it runs times and times and times. 问题出在这里,我在服务器中有一个MessageBox,在接收到该文件后可以看到FileName,但是MessageBox始终为空,并且运行时间和时间。 what's the solution? 有什么解决办法?

The Server: 服务器:

    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];
                }
            }
        }
    }

TheClient: 客户端:

    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 + "\\";
            }
        }
    }

as this friend ( Fildor ) said, i try to read more about the protocols. 正如这位朋友( Fildor )所说,我尝试阅读有关协议的更多信息。 thank you two guys but i think if i separate the file and send it pieces by pieces, i can send the hole file. 谢谢两个家伙,但我想,如果我分开文件并逐个发送,我可以发送孔文件。 I know maybe this is stupid but i think it works. 我知道这可能很愚蠢,但我认为它可以工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM