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



multicastloopback多播 环回


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


multicastloopback多播 环回
发表于:2007-06-06 15:52:50 楼主
兄弟我想在win   4.2   或者5.0下实现多播。要求socket属性设定自己发的消息自己不收的目的。于是起用了socket.setsocketoption(socketoptionlevel.ip,   socketoptionname.multicastloopback,   0)这句话。在ce下运行会报参数无效或不支持。且如果在xp下我能实现我的目的(自己发的自己不收)。但在ce下加上这句话就报错。如果我屏蔽掉这句话。用textbox3.text   =   socket.getsocketoption(socketoptionlevel.ip,   socketoptionname.multicastloopback)的话发现textbox3.text   显示为0。但在xp下显示为1   (同样没有socket.setsocketoption(socketoptionlevel.ip,   socketoptionname.multicastloopback,   0)这句话)
请教高手。我该怎么样实现在ce下自己发的消息自己不收呀。谢谢。

急!
发表于:2007-06-06 16:52:311楼 得分:0
http://topic.csdn.net/t/20040201/13/2692395.html

不懂,找到这个不知道对您有没有一点帮助。
发表于:2007-06-06 20:27:292楼 得分:0
有点乱,没看明白,用udpclient,另外用封装好的udpclient不可以吗?ce程序没开发过,不知道怎么写,多播(组播)是要路由器或3层交换机支持的,有局限性,广播服务端然后写c/s模式的感觉好些
发表于:2007-06-06 20:30:193楼 得分:0
imports   system.runtime.interopservices

namespace   my

        partial   class   mycomputer

                public   function   structtobytes(byval   structobj   as   object)   as   byte()
                        dim   size   as   integer   =   marshal.sizeof(structobj)
                        dim   buffer   as   intptr   =   marshal.allochglobal(size)
                        try
                                marshal.structuretoptr(structobj,   buffer,   false)
                                dim   bytes   as   byte()   =   new   byte(size   -   1)   {}
                                marshal.copy(buffer,   bytes,   0,   size)
                                return   bytes
                        finally
                                marshal.freehglobal(buffer)
                        end   try
                end   function

                public   function   bytestostruct(byval   bytes   as   byte(),   byval   strcuttype   as   type)   as   object
                        dim   size   as   integer   =   marshal.sizeof(strcuttype)
                        dim   buffer   as   intptr   =   marshal.allochglobal(size)
                        try
                                marshal.copy(bytes,   0,   buffer,   size)
                                return   marshal.ptrtostructure(buffer,   strcuttype)
                        finally
                                marshal.freehglobal(buffer)
                        end   try
                end   function

        end   class
end   namespace


imports   system.text
imports   system.net
imports   system.net.sockets
imports   system.threading

public   class   server
        private   sendbuf   as   byte()
        private   receivebuf   as   byte()
        private   iserver   as   udpclient
        private   tserver   as   thread
        private   tserverjoin   as   thread
        private   groupep   as   ipendpoint
        private   remoteep   as   ipendpoint
        private   sendinfo   as   sendstruc

        public   event   receivemessage   as   system.eventhandler

        public   sub   broadcast(byval   sendtype   as   string,   byval   imessage   as   string)
                sendinfo.command   =   sendtype
                sendinfo.value   =   imessage
                sendbuf   =   my.computer.structtobytes(sendinfo)
                iserver.send(sendbuf,   sendbuf.length,   groupep)
        end   sub

        public   sub   new()
                groupep   =   new   ipendpoint(ipaddress.parse( "239.1.1.3 "),   10000)
                iserver   =   new   udpclient(groupep.port)
                iserver.joinmulticastgroup(groupep.address)
                tserver   =   new   thread(addressof   serverlistener)
                tserver.start()
                tserverjoin   =   new   thread(addressof   waitingforlistener)
                tserverjoin.start()
        end   sub

        private   sub   serverlistener()
                dim   receiveinfo   as   new   sendstruc(true)
                dim   receivemessage   as   string   =   " "
                do
                        try
                                receivebuf   =   iserver.receive(remoteep)
                                receiveinfo   =   my.computer.bytestostruct(receivebuf,   receiveinfo.gettype)
                                if   receiveinfo.value   =   " "   then   receiveinfo.value   =   "(空消息) "
                                if   receiveinfo.command   =   "joinserver "   then
                                        receivemessage   =   receiveinfo.value
                                        tserverjoin.abort()
                                elseif   receiveinfo.command   =   "send "   then
                                        receivemessage   =   "收到来自 "   &   remoteep.tostring   &   "的信息: "   &   receiveinfo.value
                                end   if
                                raiseevent   receivemessage(receivemessage,   new   system.eventargs)
                        catch   ex   as   exception

                        end   try
                loop

        end   sub

        private   sub   waitingforlistener()
                thread.sleep(100)
                broadcast( "joinserver ",   getlocalip()   &   ": "   &   groupep.port   &   "加入组播! ")
        end   sub

        public   sub   close()

                broadcast( "joinserver ",   getlocalip()   &   ": "   &   groupep.port   &   "退出组播! ")
                iserver.dropmulticastgroup(groupep.address)
                iserver.close()
                tserver.abort()
        end   sub

        private   function   getlocalip()   as   string
                dim   addr   as   system.net.ipaddress
                addr   =   system.net.dns.gethostaddresses(system.net.dns.gethostname())(0)
                return   addr.tostring
        end   function

        private   structure   sendstruc

                public   sub   new(byval   initme   as   boolean)
                        if   initme   then
                                command   =   " "
                                value   =   " "
                        end   if
                end   sub

                public   command   as   string
                public   value   as   string
        end   structure
end   class


public   class   form1

        private   withevents   myserver   as   new   server
        private   delegate   sub   invokedelegate()
        private   receivemessage   as   string

        private   sub   form1_formclosing(byval   sender   as   object,   byval   e   as   system.windows.forms.formclosingeventargs)   handles   me.formclosing
                myserver.close()
        end   sub

        private   sub   button1_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   button1.click
                myserver.broadcast( "send ",   textbox1.text)
        end   sub

        private   sub   receive(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   myserver.receivemessage
                receivemessage   =   sender.tostring
                textbox2.begininvoke(new   invokedelegate(addressof   receiveinvoke))
        end   sub

        private   sub   receiveinvoke()
                textbox2.appendtext(receivemessage   &   vbcrlf)
        end   sub


end   class


快速检索

最新资讯
热门点击