标题:求助C#文件传输,请高手赐教……
只看楼主
mishiw
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-3-30
结帖率:0
已结贴  问题点数:20 回复次数:2 
求助C#文件传输,请高手赐教……
欲实现一个类似QQ传文件一个网络文件传输模块,以下是实现文件传输模块功能的代码,但还存在一个亟待解决的问题——在接受端是一次性写入的,这就导致了当传输大文件时内存溢出的现象,请各位高手赐教,如何实现像QQ或飞秋、飞鸽那样传文件的功能,不胜感激,谢谢

文件发送端代码:
/// <summary>
        /// 发送
        /// </summary>
        private void SendFile()
        {
            if(this.FileName.Text.Trim() == "" )
            {
                this.labTestTrans.Text = "请选择需要传输的文件!";
                return;
            }
            else
                if(this.hostName.Text.Trim() == "" )
            {
                this.labTestTrans.Text = "请输入远程主机名!";
                return;
            }
            else
                if(this.portCode.Text.Trim() == "")
            {
                this.labTestTrans.Text = "请输入远程主机端口号";
                return;
            }
            this.labTestTrans.Text = "正在与远程主机建立连接…";
            try
            {
                string SendFileName=this.FileName.Text.Trim(); //取得预发送的文件名
                string RemoteHostName=this.hostName.Text.Trim(); //远程主机
                int RemotePort=Int32.Parse(this.portCode.Text.Trim());//远程端口  
                IPHostEntry ipInfo=Dns.GetHostByName(RemoteHostName); //得到主机信息
                IPAddress RemoteIp=ipInfo.AddressList[0]; //取得IPAddress[]/得到远程接收IP
                TcpClient client = new TcpClient();
                client.Connect(RemoteIp, RemotePort);
                this.labTestTrans.Text = "已与远程主机:"+RemoteHostName+":"+RemotePort+" 建立连接!";
                NetworkStream clientStream = client.GetStream();//创建接收用的Stream
                //读取源文件至字节数组
                FileStream fs = new FileStream(SendFileName, FileMode.Open, FileAccess.Read);
                BinaryReader fileReader = new BinaryReader(fs,Encoding.UTF8);
                long FileLength = fs.Length;//文件长度
                int hasRead = 0;
            do
                {    arrbyte = new byte[4096];
                    hasRead = fileReader.Read(arrbyte,0,arrbyte.Length);
                    clientStream.Write(arrbyte, 0, hasRead);
                }while(hasRead > 0);
                clientStream.Close();
                client.Close();
            }
                catch ( Exception se)
            {
                MessageBox.Show ( se.Message , "错误" ) ;
            }
        
        }


文件接收端代码:
private void Receive()
        {
            try
            {
                //获取本机主机名
                IPHostEntry ieh=Dns.GetHostByName(Dns.GetHostName());
                IPAddress LocalIP = ieh.AddressList[0];
                int LocalPort=Int32.Parse(this.LocalPort.Text);
                //新建监听
                listener = new TcpListener(LocalIP, LocalPort);
                listener.Stop();//关闭先前监听计划
                listener.Start();//开始监听
                this.ReceiveLabel.Text = "程序正在监听..." ;
                while (true)
                {
                    const int bufferSize = 4096;
                    TcpClient client = listener.AcceptTcpClient();

                    //接收客户端传过来的数据
                    NetworkStream clientStream = client.GetStream();
                    byte[] responseBuffer = new byte[bufferSize];
                    MemoryStream memStream = new MemoryStream();
                    string SaveName = this.ReceiveName.Text;
                    
                    StreamReader reader = new StreamReader(memStream);
                    int bytesRead = 0;
                    FileStream fs ;
                    do
                    {
                        bytesRead = clientStream.Read(responseBuffer, 0, bufferSize);
                        memStream.Write(responseBuffer, 0, bytesRead);
                    }while (bytesRead > 0);

                    fs = new FileStream(SaveName, FileMode.OpenOrCreate);
                    BinaryWriter w = new BinaryWriter(fs);
                    w.Write(memStream.ToArray());
                    fs.Close();
                    this.RecMessage.Text = "文件接收成功!\n\r完成时间:"+System.DateTime.Now.ToString()+"\n\r文件名:"+SaveName;
                    

                }
            }
            catch ( System.Security.SecurityException )
            {
                MessageBox.Show ( "侦听失败!" , "错误" ) ;
            }
        }

搜索更多相关主题的帖子: 文件 传输 
2010-03-30 14:57
mishiw
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-3-30
得分:0 
现在的问题就是接受过来的文件流都存放在内存中,待接受完毕后才一次性写入,这就导致了过多地消耗内存,诚请各位大虾协助出招解决,谢谢
2010-03-30 15:00
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
得分:20 
内存的话与你的responseBuffer有关,如果文件大的话,可以一次写入1M缓存,文件小就 把缓存设小一点

站在春哥的肩膀上
2010-03-31 11:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-301023-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.355449 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved