欢迎光临
我们一直在努力

利用remoting实现文件传输-.NET教程,远程及网络应用

建站超值云服务器,限时71元/月

因为工作需要写了一个文件传输的东东

使用了system.runtime.remoting

客户端接口类:

namespace nail.net.remoting.trans

    public interface itransfile

        function sendfile(byval filename as string, byval bytes() as byte, byval clientcrc as integer) as integer
        function comparefile(byval filename as string, byval clientcrc as integer) as boolean
        function getfiledet(byval filename as string) as integer
        function getfileinfo(byval filename as string) as byte()
        function getfileinfo(byval filename as string, byval length as integer) as byte()
        function getfileinfo(byval filename as string, byval point as integer, byval length as integer) as byte()
        property servertranssavefilepath() as string
        property servertempfilepath() as string

    end interface

end namespace

客户端类:(文件传输的实现在此)(包含1个窗体 后面窗体代码我会贴出来)(请添加引用system.runtime.remoting .net默认是不引用的 服务器端类同样)

imports system.runtime
imports system.runtime.remoting
imports system.runtime.remoting.channels
imports system.runtime.remoting.channels.tcp
imports system.io
imports system.threading

namespace nail.net.remoting.client

    public class filetransclient
        private transtempsize as integer
        private transtype as trtype
        private transport as integer
        private transserverclassname as string
        private transserveripadr as string
        private transcrc32 as integer
        private tsc as nail.net.remoting.trans.itransfile
        private senconds as integer = 10
        private withevents timer1 as new system.windows.forms.timer
        private newtransform as frmtransfersui
        private transfilename as string

        文件全路径
        public property fullfilename() as string
            get
                return transfilename
            end get
            set(byval value as string)
                transfilename = value
            end set
        end property

        缓存属性
        public property tempsize() as integer
            get
                return tempsize
            end get
            set(byval value as integer)
                me.transtempsize = value
            end set
        end property

        端口属性
        public property port() as integer
            get
                return port
            end get
            set(byval value as integer)
                me.transport = value
            end set
        end property

        服务器端服务名属性
        public property serverclassname() as string
            get
                return serverclassname
            end get
            set(byval value as string)
                me.transserverclassname = value
            end set
        end property

        服务器端ip地址属性
        public property serveripadr() as string
            get
                return serveripadr()
            end get
            set(byval value as string)
                me.transserveripadr = value
            end set
        end property

        枚举传输类型
        enum trtype
            tcp = 1
            http = 2
        end enum

        传输类别属性
        public property type() as trtype
            get
                return transtype
            end get
            set(byval value as trtype)
                me.transtype = value
            end set
        end property

        连接服务器
        public sub connetserver()
            try
                select case transtype
                    case trtype.tcp
                        tsc = activator.getobject(gettype(nail.net.remoting.trans.itransfile), “tcp://” & transserveripadr & “:” & transport & “/” & transserverclassname)
                    case trtype.http
                        tsc = activator.getobject(gettype(nail.net.remoting.trans.itransfile), “http://” & transserveripadr & “:” & transport & “/” & transserverclassname)
                end select

            catch ex as exception
                msgbox(ex.tostring)
            end try
        end sub

        public function checkfileinfo(byval filename as string) as integer
            dim crc as new nail.filetools.clscrc32
            return crc.calcuatefile(filename)
        end function

        public sub sendfile()
            showui()
            timer1.interval = 10
            timer1.start()
            dim sendthead as new thread(addressof sendfiletoserver)
            sendthead.start()
        end sub

        发送文件方法
        private sub sendfiletoserver()
            dim crc as integer = checkfileinfo(transfilename)
            dim fif as new fileinfo(transfilename)
            dim filesm as new filestream(transfilename, filemode.open, fileaccess.read, fileshare.read)
            if filesm.length = 0 then
                msgbox(“这是个空文件 请选择一个有效文件!”)
                exit sub
            end if

            dim bty(transtempsize – 1) as byte
            dim star as integer = 0
            dim i as long

            newtransform.maxofprogress = filesm.length 进度条总量
            newtransform.filesize = fif.length / 1000 & “kbyte” 文件大小
            newtransform.transfersfilename = fif.name 文件名
            try
                if (fif.length / transtempsize).tostring.indexof(“.”) > 0 then
                    if (fif.length / transtempsize).tostring.substring(0, 1) = “0” then
                        redim bty(fif.length – 1)
                        filesm.read(bty, 0, bty.length)
                        tsc.sendfile(fif.name, bty, crc)
                        with newtransform
                            .rateofprogress = bty.length 进度条当前值
                            .percentofprogress = “100%”  已完成的百分比
                            .leavetime = “完成”          剩余时间
                            .transed = bty.length & “byte”  已传输文件量
                            .rateoftransfers = math.floor(bty.length / senconds) & “kbyte/秒”  传输速度
                        end with
                    else
                        for i = math.floor(fif.length / transtempsize) to 1 step -1
                            filesm.read(bty, 0, bty.length)
                            tsc.sendfile(fif.name, bty, crc)
                            with newtransform
                                .rateofprogress = star
                                .percentofprogress = math.floor(newtransform.pgbtransfers.value / newtransform.pgbtransfers.maximum * 100) & “%”
                                if star > 0 then
                                    .leavetime = timespan.fromseconds(math.floor((fif.length – star) / 1000 / math.floor(star / senconds))).tostring
                                end if
                                .transed = star / 1000 & “kbyte”
                                .rateoftransfers = math.floor(star / senconds) & “kbyte/秒”
                            end with
                            star += transtempsize
                            filesm.seek(star, seekorigin.begin)
                        next
                        redim bty((fif.length – (math.floor(fif.length / transtempsize)) * transtempsize) – 1)
                        filesm.read(bty, 0, bty.length)
                        tsc.sendfile(fif.name, bty, crc)
                        with newtransform
                            .rateofprogress = .pgbtransfers.maximum  进度条当前值
                            .percentofprogress = “100%”
                            .leavetime = “完成”
                            .transed = filesm.length / 1000 & “kbyte”
                            .rateoftransfers = math.floor(star / senconds) & “kbyte/秒”  传输速度
                        end with

                    end if

                else

                    for i = math.floor(fif.length / transtempsize) to 1 step -1
                        filesm.read(bty, 0, bty.length)
                        tsc.sendfile(fif.name, bty, crc)
                        with newtransform
                            .rateofprogress = star
                            .percentofprogress = math.floor(newtransform.pgbtransfers.value / newtransform.pgbtransfers.maximum * 100) & “%”
                            .transed = star / 1000 & “kbyte”
                            if star > 0 then
                                .leavetime = timespan.fromseconds(math.floor((fif.length – star) / 1000 / math.floor(star / senconds))).tostring
                            end if
                            .rateoftransfers = math.floor(star / senconds) & “kbyte/秒”  传输速度
                        end with
                        star += transtempsize
                        filesm.seek(star, seekorigin.begin)

                    next
                    newtransform.leavetime = “完成”
                   
                end if
                newtransform.btncancle.text = “确  定”
                me.timer1.stop()
                senconds = 10
                newtransform.close()
                msgbox(“传输完成”)
            catch ex as exception
                msgbox(ex.tostring)
            end try

            filesm.close()
        end sub

        接受文件方法
        public function getfile(byval _filename as string) as integer

        end function

        记时
        private sub timer1_tick(byval sender as object, byval e as system.eventargs) handles timer1.tick
            senconds += 10
        end sub

        public function showui() as integer
            newtransform = new frmtransfersui
            newtransform.show()
            return 0
        end function
    end class

end namespace

客户端窗体(显示文件传输的进度 时间 大小 。。。。。)

public class frmtransfersui
    inherits system.windows.forms.form

#region ” windows 窗体设计器生成的代码 “

    public sub new()
        mybase.new()

        该调用是 windows 窗体设计器所必需的。
        initializecomponent()

        在 initializecomponent() 调用之后添加任何初始化

    end sub

    窗体重写 dispose 以清理组件列表。
    protected overloads overrides sub dispose(byval disposing as boolean)
        if disposing then
            if not (components is nothing) then
                components.dispose()
            end if
        end if
        mybase.dispose(disposing)
    end sub

    windows 窗体设计器所必需的
    private components as system.componentmodel.icontainer

    注意: 以下过程是 windows 窗体设计器所必需的
    可以使用 windows 窗体设计器修改此过程。
    不要使用代码编辑器修改它。
    friend withevents button1 as system.windows.forms.button
    friend withevents pgbtransfers as system.windows.forms.progressbar
    friend withevents button2 as system.windows.forms.button
    friend withevents lblrateoftransfers as system.windows.forms.label
    friend withevents lbltransfersfilename as system.windows.forms.label
    friend withevents btncancle as system.windows.forms.button
    friend withevents lblfilesize as system.windows.forms.label
    friend withevents lblleavetime as system.windows.forms.label
    friend withevents lblpercentofprogress as system.windows.forms.label
    friend withevents lbltransed as system.windows.forms.label
    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
        me.button1 = new system.windows.forms.button
        me.pgbtransfers = new system.windows.forms.progressbar
        me.button2 = new system.windows.forms.button
        me.lblrateoftransfers = new system.windows.forms.label
        me.lbltransed = new system.windows.forms.label
        me.lbltransfersfilename = new system.windows.forms.label
        me.btncancle = new system.windows.forms.button
        me.lblfilesize = new system.windows.forms.label
        me.lblleavetime = new system.windows.forms.label
        me.lblpercentofprogress = new system.windows.forms.label
        me.suspendlayout()
       
        button1
       
        me.button1.dock = system.windows.forms.dockstyle.fill
        me.button1.enabled = false
        me.button1.flatstyle = system.windows.forms.flatstyle.system
        me.button1.location = new system.drawing.point(0, 0)
        me.button1.name = “button1”
        me.button1.size = new system.drawing.size(448, 136)
        me.button1.tabindex = 0
       
        pgbtransfers
       
        me.pgbtransfers.location = new system.drawing.point(10, 16)
        me.pgbtransfers.name = “pgbtransfers”
        me.pgbtransfers.size = new system.drawing.size(430, 12)
        me.pgbtransfers.tabindex = 1
       
        button2
       
        me.button2.enabled = false
        me.button2.flatstyle = system.windows.forms.flatstyle.system
        me.button2.location = new system.drawing.point(4, 4)
        me.button2.name = “button2”
        me.button2.size = new system.drawing.size(442, 130)
        me.button2.tabindex = 2
       
        lblrateoftransfers
       
        me.lblrateoftransfers.flatstyle = system.windows.forms.flatstyle.system
        me.lblrateoftransfers.location = new system.drawing.point(10, 36)
        me.lblrateoftransfers.name = “lblrateoftransfers”
        me.lblrateoftransfers.size = new system.drawing.size(220, 14)
        me.lblrateoftransfers.tabindex = 3
        me.lblrateoftransfers.text = “传输速度:”
       
        lbltransed
       
        me.lbltransed.flatstyle = system.windows.forms.flatstyle.system
        me.lbltransed.location = new system.drawing.point(10, 58)
        me.lbltransed.name = “lbltransed”
        me.lbltransed.size = new system.drawing.size(220, 14)
        me.lbltransed.tabindex = 4
        me.lbltransed.text = “已 传 送:”
       
        lbltransfersfilename
       
        me.lbltransfersfilename.flatstyle = system.windows.forms.flatstyle.system
        me.lbltransfersfilename.location = new system.drawing.point(10, 80)
        me.lbltransfersfilename.name = “lbltransfersfilename”
        me.lbltransfersfilename.size = new system.drawing.size(220, 14)
        me.lbltransfersfilename.tabindex = 5
        me.lbltransfersfilename.text = “传输文件:”
       
        btncancle
       
        me.btncancle.flatstyle = system.windows.forms.flatstyle.system
        me.btncancle.location = new system.drawing.point(192, 104)
        me.btncancle.name = “btncancle”
        me.btncancle.tabindex = 6
        me.btncancle.text = “取  消”
       
        lblfilesize
       
        me.lblfilesize.flatstyle = system.windows.forms.flatstyle.system
        me.lblfilesize.location = new system.drawing.point(246, 80)
        me.lblfilesize.name = “lblfilesize”
        me.lblfilesize.size = new system.drawing.size(192, 14)
        me.lblfilesize.tabindex = 9
        me.lblfilesize.text = “文件大小:”
       
        lblleavetime
       
        me.lblleavetime.flatstyle = system.windows.forms.flatstyle.system
        me.lblleavetime.location = new system.drawing.point(246, 58)
        me.lblleavetime.name = “lblleavetime”
        me.lblleavetime.size = new system.drawing.size(192, 14)
        me.lblleavetime.tabindex = 8
        me.lblleavetime.text = “剩余时间:”
       
        lblpercentofprogress
       
        me.lblpercentofprogress.flatstyle = system.windows.forms.flatstyle.system
        me.lblpercentofprogress.location = new system.drawing.point(246, 36)
        me.lblpercentofprogress.name = “lblpercentofprogress”
        me.lblpercentofprogress.size = new system.drawing.size(192, 14)
        me.lblpercentofprogress.tabindex = 7
        me.lblpercentofprogress.text = “传输进度:”
       
        frmtransfersui
       
        me.autoscalebasesize = new system.drawing.size(6, 14)
        me.clientsize = new system.drawing.size(448, 136)
        me.controls.add(me.lblfilesize)
        me.controls.add(me.lblleavetime)
        me.controls.add(me.lblpercentofprogress)
        me.controls.add(me.btncancle)
        me.controls.add(me.lbltransfersfilename)
        me.controls.add(me.lbltransed)
        me.controls.add(me.lblrateoftransfers)
        me.controls.add(me.pgbtransfers)
        me.controls.add(me.button2)
        me.controls.add(me.button1)
        me.formborderstyle = system.windows.forms.formborderstyle.none
        me.name = “frmtransfersui”
        me.showintaskbar = false
        me.startposition = system.windows.forms.formstartposition.centerscreen
        me.text = “frmtransfersui”
        me.topmost = true
        me.resumelayout(false)

    end sub

#end region

    public event usercancle()

    public writeonly property maxofprogress()
        set(byval value)
            me.pgbtransfers.maximum = value
        end set
    end property

    public writeonly property rateofprogress()
        set(byval value)
            me.pgbtransfers.value = value
        end set
    end property

    public writeonly property rateoftransfers() as string
        set(byval value as string)
            me.lblrateoftransfers.text = “传输速度:” & value
        end set
    end property

    public writeonly property transed() as string
        set(byval value as string)
            me.lbltransed.text = “已 传 送:” & value
        end set
    end property

    public writeonly property transfersfilename() as string
        set(byval value as string)
            me.lbltransfersfilename.text = “传输文件:” & value
        end set
    end property

    public writeonly property percentofprogress() as string
        set(byval value as string)
            me.lblpercentofprogress.text = “传输进度:” & value
        end set
    end property

    public writeonly property leavetime() as string
        set(byval value as string)
            me.lblleavetime.text = “剩余时间:” & value
        end set
    end property

    public writeonly property filesize() as string
        set(byval value as string)
            me.lblfilesize.text = “文件大小:” & value
        end set
    end property

    private sub btncancle_click(byval sender as system.object, byval e as system.eventargs) handles btncancle.click

        if me.btncancle.text = “确  定” then
            me.close()
        else
            raiseevent usercancle()
        end if

    end sub
end class

服务器端类   下面还有个模块

imports system.runtime
imports system.runtime.remoting
imports system.runtime.remoting.channels
imports system.runtime.remoting.channels.tcp
imports system.io

namespace nail.net.remoting.server
    public class filetransserver

        private transserverclassname as string
        private transtype as trtype
        private transport as integer
        private transcrc32 as integer

        enum trtype
            tcp = 1
            http = 2
        end enum

        public property type() as trtype
            get
                return transtype
            end get
            set(byval value as trtype)
                me.transtype = value
            end set
        end property

        public property servertranssavefilepath() as string
            get
                return transsavefilepath
            end get
            set(byval value as string)
                transsavefilepath = value
            end set
        end property

        public property servertempfilepath() as string
            get
                return tempfilepath
            end get
            set(byval value as string)
                tempfilepath = value
            end set
        end property

        public property serverclassname() as string
            get
                return serverclassname
            end get
            set(byval value as string)
                me.transserverclassname = value
            end set
        end property

        public property port() as integer
            get
                return port
            end get
            set(byval value as integer)
                me.transport = value
            end set
        end property

        public sub starserver()
            select case transtype
                case trtype.tcp
                    dim a as new system.runtime.remoting.channels.tcp.tcpchannel(transport)
                    channelservices.registerchannel(a)
                case trtype.http
                    dim a as new system.runtime.remoting.channels.http.httpchannel(transport)
                    channelservices.registerchannel(a)
            end select
            system.runtime.remoting.remotingconfiguration.applicationname = transserverclassname
            remotingconfiguration.registerwellknownservicetype(gettype(transfile), transserverclassname, wellknownobjectmode.singlecall)

        end sub

    end class

    public class transfile
        inherits marshalbyrefobject
        implements nail.net.remoting.trans.itransfile

        public function comparefile(byval filename as string, byval clientcrc as integer) as boolean implements nail.net.remoting.trans.itransfile.comparefile
            dim crc as new nail.filetools.clscrc32
            if crc.calcuatefile(filename) = clientcrc then
                return true
            else
                return false
            end if
        end function

        public function getfiledet(byval filename as string) as integer implements nail.net.remoting.trans.itransfile.getfiledet
            dim c as new fileinfo(filename)
            return c.length
        end function

        public function getfileinfo(byval filename as string) as byte() implements nail.net.remoting.trans.itransfile.getfileinfo
            dim fullname as string = filename
            dim c as new fileinfo(fullname)
            return getfileinfo(filename, 0, c.length)
        end function

        public function getfileinfo(byval filename as string, byval length as integer) as byte() implements nail.net.remoting.trans.itransfile.getfileinfo
            return getfileinfo(filename, 0, length)
        end function

        public function getfileinfo(byval filename as string, byval point as integer, byval length as integer) as byte() implements nail.net.remoting.trans.itransfile.getfileinfo
            dim fullname as string = filename
            dim c as new fileinfo(fullname)
            dim x as new filestream(fullname, filemode.open, fileaccess.read, fileshare.read)
            dim bty(length – 1) as byte
            x.seek(point, seekorigin.begin)
            x.read(bty, 0, length)
            return bty
        end function

        public function sendfile(byval filename as string, byval bytes() as byte, byval clientcrc as integer) as integer implements nail.net.remoting.trans.itransfile.sendfile
            try
                dim a as new filestream(tempfilepath & filename, filemode.openorcreate)
                a.seek(a.length, seekorigin.begin)
                a.write(bytes, 0, bytes.length)
                a.close()
                return 0
            catch ex as exception
                msgbox(ex.tostring)
                return 1
            finally
                dim finf as new fileinfo(tempfilepath & filename)
                if comparefile(tempfilepath & filename, clientcrc) = true then
                    拷贝临时文件()
                    file.copy(tempfilepath & filename, transsavefilepath & filename, true)
                else
                    删除临时文件()
                    finf.delete()
                end if
            end try
           
        end function

        public property servertempfilepath() as string implements nail.net.remoting.trans.itransfile.servertempfilepath
            get
                return tempfilepath
            end get
            set(byval value as string)
                tempfilepath = value
            end set
        end property

        public property servertranssavefilepath() as string implements nail.net.remoting.trans.itransfile.servertranssavefilepath
            get
                return transsavefilepath
            end get
            set(byval value as string)
                transsavefilepath = value
            end set
        end property
    end class

end namespace

模块:

module server
    public transsavefilepath as string
    public tempfilepath as string
end module

使用方法:

服务器端

示例:

dim server as new nail.net.remoting.server.filetransserver
        server.serverclassname = “aaa”
        server.port = 10001
        server.type = nail.net.remoting.server.filetransserver.trtype.http
        server.servertempfilepath = “f:\”
        server.servertranssavefilepath = “e:\”
        server.starserver()

客户端

 cl.port = 10001
        cl.serverclassname = “aaa”
        cl.tempsize = 102400
        cl.serveripadr = “localhost”
        cl.fullfilename = “c:\111.rar”
        cl.type = nail.net.remoting.client.filetransclient.trtype.http
        cl.connetserver()

发送文件:

 cl.sendfile()

最后忘记贴文件校对了 照书上搞的crc32

imports system.io

namespace nail.filetools

    public class clscrc32
        private const tablesize as integer = 256
        private const defaultpolynomial as integer = &hedb88320
        private const defaultialvalue as integer = &hffffffff
        private lookup(tablesize – 1) as integer
        private crcpolynomial as integer = 0

        public sub new()
            me.new(defaultpolynomial)
        end sub

        public sub new(byval crcpolynomial as integer)
            me.crcpolynomial = crcpolynomial
            initlookuptable()
        end sub

        public property polynomial() as integer
            get
                return crcpolynomial
            end get
            set(byval value as integer)
                me.crcpolynomial = value
                initlookuptable()
            end set
        end property

        public overloads function calculateblock(byval bytes() as byte) as integer
            return calculateblock(bytes, 0, bytes.length)
        end function

        public overloads function calculateblock(byval bytes() as byte, byval index as integer, byval length as integer) as integer
            return calculateblock(bytes, index, length, defaultialvalue)
        end function

        public overloads function calculateblock(byval bytes() as byte, byval index as integer, byval length as integer, byval initialvalue as integer) as integer
            if bytes is nothing then

            elseif index < 0 or length <= 0 or index + length > bytes.length then

            end if
            return not internalcalculateblock(bytes, index, length, initialvalue)
        end function

        private function internalcalculateblock(byval bytes() as byte, byval index as integer, byval length as integer, byval initialvalue as integer) as integer
            dim crc as integer = initialvalue
            dim shiftcrc as integer

            dim position as integer
            for position = index to length – 1
                shiftcrc = crc and &hffffff00
                shiftcrc = shiftcrc / &h100
                shiftcrc = shiftcrc and &hffffff
                crc = shiftcrc xor lookup(bytes(position)) xor (crc and &hff)
            next
            return crc
        end function

        public overloads function calcuatefile(byval path as string) as integer
            return calcuatefile(path, defaultialvalue)

        end function

        public overloads function calcuatefile(byval path as string, byval initialvalue as integer) as integer
            if path is nothing then

            elseif path.length = 0 then

            end if

            return not internalcalculatefile(path, initialvalue)
        end function

        private function internalcalculatefile(byval path as string, byval initialvalue as integer) as integer

            const blocksize as integer = 4096
            dim count as integer
            dim instream as system.io.filestream
            dim bytes(blocksize – 1) as byte
            dim crc as integer = initialvalue
            try
                instream = system.io.file.open(path, filemode.open, fileaccess.read)
                while instream.position < instream.length
                    count = instream.read(bytes, 0, blocksize)
                    crc = internalcalculateblock(bytes, 0, count, crc)
                end while
            finally
                if not instream is nothing then
                    instream.close()
                end if
            end try
            return crc
        end function

        private sub initlookuptable()
            dim bytecount, bitcount as integer
            dim crc, shiftcrc as integer
            for bytecount = 0 to tablesize – 1
                crc = bytecount
                for bitcount = 0 to 7
                    shiftcrc = crc and &hfffffffe
                    shiftcrc = shiftcrc \ &h2
                    shiftcrc = shiftcrc and &h7fffffff
                    if (crc and &h1) then
                        crc = shiftcrc xor crcpolynomial
                    else
                        crc = shiftcrc
                    end if
                next
                lookup(bytecount) = crc
            next
        end sub

    end class

end namespace

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 利用remoting实现文件传输-.NET教程,远程及网络应用
分享到: 更多 (0)