您的位置:程序门 -> .net技术 -> c#



用socket传输200-500m的文件解决方案,有代码


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


用socket传输200-500m的文件解决方案,有代码
发表于:2008-01-20 17:03:01 楼主
        ///   <summary>  
        ///   将文件写到socket  
        ///   </summary>  
        ///   <param   name="s"> 要发送文件的socket </param>  
        ///   <param   name="strfile"> 要发送的文件 </param>  
        ///   <returns> 是否成功 </returns>  
                    public   static   bool   writefiletosocket(socket   s,   string   strfile,onsend   onsendfile)
                    {

                            filestream   fs   =   new   filestream(strfile,   filemode.open,   fileaccess.read,   fileshare.read);
                            int   ilen   =   (int)fs.length;
                            writedynamiclentosocket(s,   ilen);
                            byte[]   buf   =   new   byte[ilen];
                            try
                            {
                                  fs.read(buf,   0,   ilen);
                                    return   writebuftosocket(s,   buf,   0,   ilen,   deallen,   onsendfile);
                            }
                            catch   (exception   err)
                            {

                                    messagebox.show("1发送文件失败!"   +   err.message);
                                    return   false;

                            }
                            finally
                            {
                                    fs.close();
                            }

                    }
                  ///   <summary>  
                    ///   将缓存的指定部分发送到socket  
                    ///   </summary>  
                    ///   <param   name="s"> 要发送缓存的socket </param>  
                    ///   <param   name="buf"> 要发送的缓存 </param>  
                    ///   <param   name="istart"> 要发送缓存的起始位置 </param>  
                    ///   <param   name="icount"> 要发送缓存的字节数 </param>  
                    ///   <param   name="iblock"> 每次发送的字节数 </param>  
                    ///   <param   name="sendsuccess"> 每次发送成功后的回调函数 </param>  
                    ///   <returns> 是否发送成功 </returns>  

                    public   static   bool   writebuftosocket(socket   s,   byte[]   buf,   int   istart,   int   icount,   int   iblock,   onsend   sendsuccess)
                    {

                            int   isended   =   0;

                            int   isending   =   0;

                            while   (isended   <   icount)
                            {

                                    if   (isended   +   iblock   <=   icount)

                                            isending   =   iblock;

                                    else

                                            isending   =   icount   -   isended;

                                    s.send(buf,   istart   +   isended,   isending,   socketflags.none);

                                    isended   +=   isending;

                                    if   (readresponsionfromsocket(s)   ==   "ok")

                                            if   (sendsuccess   !=   null)

                                                    sendsuccess(icount,   isended);

                                            else

                                                    return   false;

                            }

                            return   true;

                    }  
                    ///   <summary>
                        ///   大文件发送时使用,
                        ///   </summary>
                        ///   <param   name="s"> </param>
                        ///   <param   name="strfile"> </param>
                        ///   <param   name="onsendfile"> </param>
                        ///   <returns> </returns>
                        public   static   bool   writebigfiletosocket(socket   s,   string   strfile,onsend   onsendfile)
                        {

                                filestream   fs   =   new   filestream(strfile,   filemode.open,   fileaccess.read,   fileshare.read);

                                int   ilen   =   (int)fs.length;

                                writedynamiclentosocket(s,   ilen);

                                byte[]   buf   =   new   byte[fileblock];
                               
                               
                                int   len;

                                try
                                {
                                        while   ((len   =   fs.read(buf,   0,   fileblock))   !=   0)
                                        {
                                                s.send(buf,   0,   len,   socketflags.none);
                                        }
                                        fs.close();
                                    //     fs.read(buf,   0,   ilen);

                                    //     return   writebuftosocket(s,   buf,   0,   ilen,   deallen,   onsendfile);
                                        return   true;
                                }

                                catch   (exception   err)
                                {

                                        messagebox.show("发送大文件失败!"   +   err.message);

                                        return   false;

                                }

                                finally
                                {

                                        fs.close();
                                        buf   =   new   byte[0];
                                }

                        }
以上是发送部分
                ///   <summary>  
                ///   读取文件形式的可变信息  
                ///   </summary>  
                ///   <param   name="s"> 要读取可变信息的socket </param>  
                ///   <param   name="strfile"> 读出后的文件保存位置 </param>  
                ///   <returns> 是否读取成功 </returns>  

                                public   static   bool   readdynamicfilefromsocket(socket   s,   string   strfile)
                                {

                                        int   ilen   =   readdynamiclenfromsocket(s);

                                        byte[]   buf   =   new   byte[ilen];

                                        filestream   fs   =   new   filestream(strfile,   filemode.create,   fileaccess.write);


                                        try
                                        {

                                                int   ireceiveded   =   0;

                                                int   ireceiveing   =   0;

                                                while   (ireceiveded   <   ilen)
                                                {

                                                        if   (ireceiveded   +   deallen   <=   ilen)

                                                                ireceiveing   =   deallen;

                                                        else

                                                                ireceiveing   =   ilen   -   ireceiveded;

                                                        s.receive(buf,   ireceiveded,   ireceiveing,   socketflags.none);

                                                        commonclass.writetexttosocket(s,   "ok");

                                                        ireceiveded   +=   ireceiveing;

                                                }

                                                fs.write(buf,   0,   ilen);

                                                return   true;

                                        }

                                        catch   (exception   err)
                                        {

                                                messagebox.show("接收文件失败"   +   err.message);
                                                return   false;

                                        }

                                        finally
                                        {

                                                fs.close();

                                        }

                                }  

                            ///   <summary>  
                            ///   读取文件形式的可变信息  
                            ///   </summary>  
                            ///   <param   name="s"> 要读取可变信息的socket </param>  
                            ///   <param   name="strfile"> 读出后的文件保存位置 </param>  
                            ///   <returns> 是否读取成功 </returns>  
                            public   static   bool   readbigfilefromsocket(socket   s,   string   strfile)
                            {

                                    int   ilen   =   readdynamiclenfromsocket(s);
                                    byte[]   buf   =   new   byte[fileblock];

                                    filestream   fs   =   new   filestream(strfile,   filemode.create,   fileaccess.write);
                                    try
                                    {
                                            int   ireceiveded   =   0;
                                            int   ireceiveing   =   0;

                                            while   (ireceiveded   <   ilen)
                                            {
                                                    if   (ireceiveded   +   fileblock   <=   ilen)

                                                            ireceiveing   =   fileblock;

                                                    else

                                                            ireceiveing   =   ilen   -   ireceiveded;

                                                    s.receive(buf,   0,   ireceiveing,   socketflags.none);
                                                    //分块写入到文件
                                                    fs.write(buf,   0,   ireceiveing);
                                                  //   commonclass.writetexttosocket(s,   "ok");
                                                    ireceiveded   +=   ireceiveing;

                                            }
                                            //fs.write(buf,   0,   ilen);
                                            return   true;
                                    }
                                    catch   (exception   err)
                                    {
                                            messagebox.show("接收文件失败"   +   err.message);
                                            return   false;
                                    }
                                    finally
                                    {
                                            fs.close();
                                    }
                            }  
private   static   readonly   int   deallen   =   4096   ;
这是接收部分,
这个用来接收十几m的文件没有问题,但是接收三十m以上的不会出错,高手分析一下是什么原因
如有demo希望能发一份给我:huangjinyin66@163.com
发表于:2008-01-20 17:04:411楼 得分:0
但是接收三十m以上的会出错
发表于:2008-01-20 22:28:542楼 得分:0
upup
发表于:2008-01-20 22:43:513楼 得分:0
up
发表于:2008-01-20 22:53:054楼 得分:0

沒有分段(分包)發送?夠嗆了。


发表于:2008-01-21 08:16:395楼 得分:0
下面还有个函数是分段分包发送的
发表于:2008-01-21 08:40:246楼 得分:0
up


快速检索

最新资讯
热门点击