C#文件传输

时间:2014-04-13 04:55:50

标签: c# file sockets

我有一个关于以C#语言发送文件和接收文件的方式的问题。我创建了一个简单的文件传输窗体应用程序,但它只支持.txt文件格式。如果我尝试发送图像文件或ms字文档文件,则可以在接收方完全接收。但是,收到的文件不可读,无法打开。我已经完成了类似的应用程序Java,但它适用于任何文件格式,我使用相同的逻辑在C#应用程序中实现。有人可以给我一些建议或意见吗?

SENDING SIDE:
// establish connection
                int port = Convert.ToInt32(txtLocalPort.Text) - 5;
                TcpListener listener = new TcpListener(IPAddress.Parse(txtLocalIP.Text), port);
                listener.Start();

                // get file size
                byte[] data = File.ReadAllBytes(downFile);
                int fSize = data.Length;
                // calculate block numbers, 1024 bytes each block
                int block = fSize / 1024;
                // leftover file bytes, less than 1024 bytes
                int byteLeft = fSize % 1024;

                // convert String to byte
                String cmd = "SEND_FILE" + fSize.ToString();
                buff = new byte[1024];
                buff = Encoding.ASCII.GetBytes(cmd);

                // send message in byte
                msgSocket.Send(buff);

                // accept connection
                TcpClient client = listener.AcceptTcpClient();

                // remote host connected
                if (client.Connected == true)
                {
                    // medium to read file bytes from file chosen
                    BinaryReader readByte = new BinaryReader(File.Open(downFile, FileMode.Open));
                    // medium to send file bytes
                    NetworkStream dataOUT = client.GetStream();

                    // send file bytes based on calculated block numbers
                    for (int i = 0; i < block; i++)
                    {
                        // buffer size
                        buff = new byte[1024];
                        // read file bytes from file chosen
                        readByte.Read(buff, 0, buff.Length);
                        // send file bytes
                        dataOUT.Write(buff, 0, buff.Length);
                        dataOUT.Flush();
                    }

                    // leftover file bytes
                    // buffer size
                    buffLeft = new byte[byteLeft];
                    // read leftover file bytes from file chosen
                    readByte.Read(buffLeft, 0, buffLeft.Length);
                    // send leftover file bytes
                    dataOUT.Write(buff, 0, buffLeft.Length);
                    dataOUT.Flush();

                    MessageBox.Show("File sent successfully.", "File Share", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    // close the medium
                    readByte.Close();
                    dataOUT.Close();
                    client.Close();
                    listener.Stop();

接收方:

// establish connection
        int port = Convert.ToInt32(txtRemotePort.Text) - 5;
        TcpClient client = new TcpClient(Dns.GetHostEntry(IPAddress.Parse(txtRemoteIP.Text)).HostName.ToString(), port);

        // connected to remote host
        if (client.Connected == true)
        {
            // get invalid character
            String invalid = new String(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

            // remove invalid character
            foreach (char c in invalid)
            {
                fileName = fileName.Replace(c.ToString(), "");
            }

            String downFile = Path.Combine(@"D:\", fileName);

            // file size
            int fSize = fileSize;
            // calculate block numbers, 1024 bytes each block
            int block = fSize / 1024;
            // leftover file bytes, less than 1024 bytes
            int byteLeft = fSize % 1024;

            // medium to receive file bytes
            NetworkStream dataIN = client.GetStream();
            // medium to write file bytes to new file
            BinaryWriter writeByte = new BinaryWriter(File.Open(downFile, FileMode.Create));

            // receive file bytes based on calculated block numbers
            for (int i = 0; i < block; i++)
            {
                // buffer size
                buff = new byte[1024];
                // receive file bytes
                dataIN.Read(buff, 0, buff.Length);
                dataIN.Flush();
                // write file bytes to new file
                writeByte.Write(buff, 0, buff.Length);
                writeByte.Flush();
            }

            // receive leftover file bytes
            // buffer size
            buffLeft = new byte[byteLeft];
            // receive file bytes
            dataIN.Read(buffLeft, 0, buffLeft.Length);
            dataIN.Flush();
            // write file bytes to new file
            writeByte.Write(buffLeft, 0, buffLeft.Length);
            writeByte.Flush();

            MessageBox.Show("File downloaded successfully.", "File Share", MessageBoxButtons.OK, MessageBoxIcon.Information);

            // close the medium
            writeByte.Close();
            dataIN.Close();
            client.Close();

1 个答案:

答案 0 :(得分:1)

我想我发现你做错了什么: 而不是 BinaryWriter writeByte = new BinaryWriter(File.Open(downFile, FileMode.Create));

使用 BinaryWriter writeByte = new BinaryWriter(File.Open(downFile, FileMode.Append));