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



谁能给我个vb.net异步通信的例子(服务端与客户端)万分感谢


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


谁能给我个vb.net异步通信的例子(服务端与客户端)万分感谢
发表于:2007-02-15 09:57:27 楼主
谢谢!
在线等
发表于:2007-02-15 13:13:401楼 得分:0
是这个吗?
http://www.codeproject.com/vb/net/tinyudp.asp
http://www.codeproject.com/vb/net/vbnetsendreceivetcp.asp
有关udp,tcp/ip的连接类。
发表于:2007-02-16 16:09:062楼 得分:0
imports   system
imports   system.net
imports   system.net.sockets
imports   system.text
imports   system.threading
imports   microsoft.visualbasic

'   state   object   for   reading   client   data   asynchronously

public   class   stateobject
        '   client     socket.
        public   worksocket   as   socket   =   nothing
        '   size   of   receive   buffer.
        public   const   buffersize   as   integer   =   1024
        '   receive   buffer.
        public   buffer(buffersize)   as   byte
        '   received   data   string.
        public   sb   as   new   stringbuilder
end   class   'stateobject


public   class   asynchronoussocketlistener
        '   thread   signal.
        public   shared   alldone   as   new   manualresetevent(false)

        '   this   server   waits   for   a   connection   and   then   uses     asychronous   operations   to
        '   accept   the   connection,   get   data   from   the   connected   client,  
        '   echo   that   data   back   to   the   connected   client.
        '   it   then   disconnects   from   the   client   and   waits   for   another   client.  
        public   shared   sub   main()
                '   data   buffer   for   incoming   data.
                dim   bytes()   as   byte   =   new   [byte](1023)   {}

                '   establish   the   local   endpoint   for   the   socket.
                dim   iphostinfo   as   iphostentry   =   dns.resolve(dns.gethostname())
                dim   ipaddress   as   ipaddress   =   iphostinfo.addresslist(0)
                dim   localendpoint   as   new   ipendpoint(ipaddress,   11000)

                '   create   a   tcp/ip   socket.
                dim   listener   as   new   socket(addressfamily.internetwork,   sockettype.stream,   protocoltype.tcp)

                '   bind   the   socket   to   the   local   endpoint   and   listen   for   incoming   connections.
                listener.bind(localendpoint)
                listener.listen(100)

                while   true
                        '   set   the   event   to   nonsignaled   state.
                        alldone.reset()

                        '   start   an   asynchronous   socket   to   listen   for   connections.
                        console.writeline( "waiting   for   a   connection... ")
                        listener.beginaccept(new   asynccallback(addressof   acceptcallback),   listener)

                        '   wait   until   a   connection   is   made   and   processed   before   continuing.
                        alldone.waitone()
                end   while
        end   sub   'main


        public   shared   sub   acceptcallback(byval   ar   as   iasyncresult)
                '   get   the   socket   that   handles   the   client   request.
                dim   listener   as   socket   =   ctype(ar.asyncstate,   socket)
                '   end   the   operation.
                dim   handler   as   socket   =   listener.endaccept(ar)

                '   create   the   state   object   for   the   async   receive.
                dim   state   as   new   stateobject
                state.worksocket   =   handler
                handler.beginreceive(state.buffer,   0,   stateobject.buffersize,   0,   new   asynccallback(addressof   readcallback),   state)
        end   sub   'acceptcallback


        public   shared   sub   readcallback(byval   ar   as   iasyncresult)
                dim   content   as   string   =   string.empty

                '   retrieve   the   state   object   and   the   handler   socket
                '   from   the   asynchronous   state   object.
                dim   state   as   stateobject   =   ctype(ar.asyncstate,   stateobject)
                dim   handler   as   socket   =   state.worksocket

                '   read   data   from   the   client   socket.  
                dim   bytesread   as   integer   =   handler.endreceive(ar)

                if   bytesread   >   0   then
                        '   there     might   be   more   data,   so   store   the   data   received   so   far.
                        state.sb.append(encoding.ascii.getstring(state.buffer,   0,   bytesread))

                        '   check   for   end-of-file   tag.   if   it   is   not   there,   read  
                        '   more   data.
                        content   =   state.sb.tostring()
                        if   content.indexof( " <eof> ")   >   -1   then
                                '   all   the   data   has   been   read   from   the  
                                '   client.   display   it   on   the   console.
                                console.writeline( "read   {0}   bytes   from   socket.   "   +   vblf   +   "   data   :   {1} ",   content.length,   content)
                                '   echo   the   data   back   to   the   client.
                                send(handler,   content)
                        else
                                '   not   all   data   received.   get   more.
                                handler.beginreceive(state.buffer,   0,   stateobject.buffersize,   0,   new   asynccallback(addressof   readcallback),   state)
                        end   if
                end   if
        end   sub   'readcallback

        private   shared   sub   send(byval   handler   as   socket,   byval   data   as   string)
                '   convert   the   string   data   to   byte   data   using   ascii   encoding.
                dim   bytedata   as   byte()   =   encoding.ascii.getbytes(data)

                '   begin   sending   the   data   to   the   remote   device.
                handler.beginsend(bytedata,   0,   bytedata.length,   0,   new   asynccallback(addressof   sendcallback),   handler)
        end   sub   'send


        private   shared   sub   sendcallback(byval   ar   as   iasyncresult)
                '   retrieve   the   socket   from   the   state   object.
                dim   handler   as   socket   =   ctype(ar.asyncstate,   socket)

                '   complete   sending   the   data   to   the   remote   device.
                dim   bytessent   as   integer   =   handler.endsend(ar)
                console.writeline( "sent   {0}   bytes   to   client. ",   bytessent)

                handler.shutdown(socketshutdown.both)
                handler.close()
                '   signal   the   main   thread   to   continue.
                alldone.set()
        end   sub   'sendcallback
end   class   'asynchronoussocketlistener
发表于:2007-02-16 16:09:213楼 得分:0
imports   system
imports   system.net
imports   system.net.sockets
imports   system.threading
imports   system.text


'   state   object   for   receiving   data   from   remote   device.

public   class   stateobject
        '   client   socket.
        public   worksocket   as   socket   =   nothing
        '   size   of   receive   buffer.
        public   const   buffersize   as   integer   =   256
        '   receive   buffer.
        public   buffer(buffersize)   as   byte
        '   received   data   string.
        public   sb   as   new   stringbuilder
end   class   'stateobject


public   class   asynchronousclient
        '   the   port   number   for   the   remote   device.
        private   const   port   as   integer   =   11000

        '   manualresetevent   instances   signal   completion.
        private   shared   connectdone   as   new   manualresetevent(false)
        private   shared   senddone   as   new   manualresetevent(false)
        private   shared   receivedone   as   new   manualresetevent(false)

        '   the   response   from   the   remote   device.
        private   shared   response   as   string   =   string.empty


        public   shared   sub   main()
                '   establish   the   remote   endpoint   for   the   socket.
                '   for   this   example   use   local   machine.
                dim   iphostinfo   as   iphostentry   =   dns.resolve(dns.gethostname())
                dim   ipaddress   as   ipaddress   =   iphostinfo.addresslist(0)
                dim   remoteep   as   new   ipendpoint(ipaddress,   port)

                '   create   a   tcp/ip   socket.
                dim   client   as   new   socket(addressfamily.internetwork,   sockettype.stream,   protocoltype.tcp)

                '   connect   to   the   remote   endpoint.
                client.beginconnect(remoteep,   new   asynccallback(addressof   connectcallback),   client)

                '   wait   for   connect.
                connectdone.waitone()

                '   send   test   data   to   the   remote   device.
                send(client,   "this   is   a   test <eof> ")
                senddone.waitone()

                '   receive   the   response   from   the   remote   device.
                receive(client)
                receivedone.waitone()

                '   write   the   response   to   the   console.
                console.writeline( "response   received   :   {0} ",   response)

                '   release   the   socket.
                client.shutdown(socketshutdown.both)
                client.close()
        end   sub   'main


        private   shared   sub   connectcallback(byval   ar   as   iasyncresult)
                '   retrieve   the   socket   from   the   state   object.
                dim   client   as   socket   =   ctype(ar.asyncstate,   socket)

                '   complete   the   connection.
                client.endconnect(ar)

                console.writeline( "socket   connected   to   {0} ",   client.remoteendpoint.tostring())

                '   signal   that   the   connection   has   been   made.
                connectdone.set()
        end   sub   'connectcallback


        private   shared   sub   receive(byval   client   as   socket)

                '   create   the   state   object.
                dim   state   as   new   stateobject
                state.worksocket   =   client

                '   begin   receiving   the   data   from   the   remote   device.
                client.beginreceive(state.buffer,   0,   stateobject.buffersize,   0,   new   asynccallback(addressof   receivecallback),   state)
        end   sub   'receive


        private   shared   sub   receivecallback(byval   ar   as   iasyncresult)

                '   retrieve   the   state   object   and   the   client   socket  
                '   from   the   asynchronous   state   object.
                dim   state   as   stateobject   =   ctype(ar.asyncstate,   stateobject)
                dim   client   as   socket   =   state.worksocket

                '   read   data   from   the   remote   device.
                dim   bytesread   as   integer   =   client.endreceive(ar)

                if   bytesread   >   0   then
                        '   there   might   be   more   data,   so   store   the   data   received   so   far.
                        state.sb.append(encoding.ascii.getstring(state.buffer,   0,   bytesread))

                        '   get   the   rest   of   the   data.
                        client.beginreceive(state.buffer,   0,   stateobject.buffersize,   0,   new   asynccallback(addressof   receivecallback),   state)
                else
                        '   all   the   data   has   arrived;   put   it   in   response.
                        if   state.sb.length   >   1   then
                                response   =   state.sb.tostring()
                        end   if
                        '   signal   that   all   bytes   have   been   received.
                        receivedone.set()
                end   if
        end   sub   'receivecallback


        private   shared   sub   send(byval   client   as   socket,   byval   data   as   string)
                '   convert   the   string   data   to   byte   data   using   ascii   encoding.
                dim   bytedata   as   byte()   =   encoding.ascii.getbytes(data)

                '   begin   sending   the   data   to   the   remote   device.
                client.beginsend(bytedata,   0,   bytedata.length,   0,   new   asynccallback(addressof   sendcallback),   client)
        end   sub   'send


        private   shared   sub   sendcallback(byval   ar   as   iasyncresult)
                '   retrieve   the   socket   from   the   state   object.
                dim   client   as   socket   =   ctype(ar.asyncstate,   socket)

                '   complete   sending   the   data   to   the   remote   device.
                dim   bytessent   as   integer   =   client.endsend(ar)
                console.writeline( "sent   {0}   bytes   to   server. ",   bytessent)

                '   signal   that   all   bytes   have   been   sent.
                senddone.set()
        end   sub   'sendcallback
end   class   'asynchronousclient
发表于:2007-02-17 06:17:454楼 得分:0
该回复于2007-12-21 19:38:09被管理员或版主删除


快速检索

最新资讯
热门点击