c#:发送文件,混合成功文件和损坏文件

标签 c# visual-studio file tcp ip

我在互联网上进行了一些研究后编写了该代码,以通过同一网络中的计算机发送文件。但是,有时它会发送损坏或质量低下的文件(如果文件是图片)。你能检查一下吗? 注意:我只想发送 10MB 大小的文件。

Server

class Server
{
    IPEndPoint end;
    Socket sock;

    public Server()
    {
        end = new IPEndPoint(IPAddress.Any, 5656);
        sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
        sock.Bind(end);
    }

    public static string path;
    public static string MsgCurrent = "Stopped";

    public void StartServer()
    {
        try
        {
            MsgCurrent = "Starting...";
            sock.Listen(100);
            MsgCurrent = "Works and looks for files";
            Socket clientSock = sock.Accept();
            byte[] clientData = new byte[1024 * 1024 * 10]; //count per byte
            int receivedByteLen = clientSock.Receive(clientData);
            MsgCurrent = "Receiving file ...";
            int fNameLen = BitConverter.ToInt32(clientData, 0);
            string fName = Encoding.ASCII.GetString(clientData, 4, fNameLen);
            BinaryWriter write = new BinaryWriter(File.Open(path + "/" + fName, FileMode.Append));
            write.Write(clientData, 4 + fNameLen, receivedByteLen - 4 - fNameLen);
            MsgCurrent = "Saving file....";
            write.Close();
            clientSock.Close();
            MsgCurrent = "The file was received";
        }
        catch
        {
            MsgCurrent = "Error, the file was not received";
        }
    }
}

Client

 class Client
{
    public static string ipsendf;//added
    public static string MsgCurrent = "Idle";
    public static void SendFile(string fName)
    {
        try
        {
            IPAddress ip = IPAddress.Parse(ipsendf); //127.0.0.1 in "" as string
            IPEndPoint end = new IPEndPoint(ip, 5656);
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

            string path = "";
            fName = fName.Replace("\\", "/");
            while (fName.IndexOf("/") > -1)
            {
                path += fName.Substring(0, fName.IndexOf("/") + 1);
                fName = fName.Substring(fName.IndexOf("/") + 1);
            }
            byte[] fNameByte = Encoding.ASCII.GetBytes(fName);
            if (fNameByte.Length > 10 * 1024 * 1024) //count per byte
            {
                //MsgCurrent = "File is greater than 850 kb";
                MsgCurrent = "File is greater than 10MB";
                return;
            }
            MsgCurrent = "Buffering...";
            byte[] fileData = File.ReadAllBytes(path + fName);
            byte[] clientData = new byte[4 + fNameByte.Length + fileData.Length];
            byte[] fNameLen = BitConverter.GetBytes(fNameByte.Length);
            fNameLen.CopyTo(clientData, 0);
            fNameByte.CopyTo(clientData, 4);
            fileData.CopyTo(clientData, 4 + fNameByte.Length);
            MsgCurrent = "Connecting to server ...";
            sock.Connect(end);
            MsgCurrent = "File sending ...";
            sock.Send(clientData);

            MsgCurrent = "Disconnecting...";
            sock.Close();
            MsgCurrent = "The file was sent ..";
        }
        catch (Exception ex)
        {
            //do nothing
        }
    }
}

最佳答案

套接字的默认接收大小为 8,192 字节 (for reference)。因此,从您的代码看来,您只是在服务器上读取消息的前 8,192 个字节。

您可以增加该缓冲区大小以匹配您分配的 10MB clientData 缓冲区的大小,例如,在 .Accept() 之后和 接收()..

clientSock.ReceiveBufferSize = 1024 * 1024 * 10;

或者,通过检查是否还有任何内容需要读取并继续填充本地缓冲区来分 block 接收。不过,您可能会在本地网络上处理潜在的大量消息,但一般来说,分 block 是正常的处理方式。

此外,您在服务器中写入文件时使用了 FileMode.Append,而我认为您需要 FileMode.Create。否则,如果您两次发送同一个文件,您将得到一个 20MB 的文件,而不是 10MB 的文件。

关于c#:发送文件,混合成功文件和损坏文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28250837/

相关文章:

c# - 尝试在 C# 中发送邮件时出现 SMTP 异常

c# - 发送大量邮件时 SmtpClient.SendMailAsync 方法挂起

.net - Entity Framework 错误 - "The EntityContainer name must be unique"

android - 在 Eclipse 中预览所有 Android XML 布局文件

c - 在 C 中,要在数据库(文件)中存储更多值,请输入 int 并调用特定行

c - 将char [128]数组写入C中的文件

c# - C# 中的线程

c# - 手动添加引用

c# - 如何在 Visual Studio 2010 的 C# 中使用 OpenFileDialog

c# - 从 C# 打开 Outlook 中的特定邮件项目