【问题标题】:c#: sending files, mixed succeded files and corrupted filesc#:发送文件,混合成功文件和损坏文件
【发布时间】:2015-03-30 19:27:57
【问题描述】:

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

服务器

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

客户

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

【问题讨论】:

    标签: c# visual-studio file tcp ip


    【解决方案1】:

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

    您可以增加该缓冲区大小以匹配您分配的 10MB clientData 缓冲区的大小,例如,在 .Accept() 之后和 Receive() 之前的某个位置..

    clientSock.ReceiveBufferSize = 1024 * 1024 * 10;
    

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

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

    【讨论】:

    • 这并没有解决问题,还有其他建议吗?我相信客户端有问题,因为有时它可以发送整个文件而没有任何中断。但是,我还不太确定,也找不到问题..
    • 前。我发送了大约 8MB 大小的 mp3。第一次只发送了前500KB。当我关闭并重新打开客户端时,我再次尝试发送文件,成功为 8MB。
    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 2020-12-29
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2023-03-07
    • 2013-10-11
    • 2022-10-18
    相关资源
    最近更新 更多