如何在C#中通过套接字发送文件?

我对这段代码有一个严重的问题。它应该是这样工作的:客户端连接到服务器,并选择磁盘上的一个文件。之后,客户端以这种格式(“byte[]”(4字节)+ FileNameLength (4字节)+ FileDataLength (4字节))向服务器发送(文件缓冲区)。在服务器创建了一个这样大小的(byte[] buffer) (新的byteFileNameLength +FileDataLength)之后,.So客户机以这种格式(byte[] buffer = FileName + FileData)向服务器发送数据。服务器就会得到一个文件。这里的问题是,我在服务器中有一个MessageBox,在收到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];
                }
            }
        }
    }

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

转载请注明出处:http://www.ommtl.com/article/20230526/2350783.html