dotgnu-pnet
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pnet-developers] UDP Broadcasting


From: Doru Budai
Subject: [Pnet-developers] UDP Broadcasting
Date: Sun, 20 Feb 2005 22:32:11 +0200

hi,

I'm curently working on a UDP chat application for LAN and I've
encounterd a Socket problem when running the application on Linux, on
Windows it works.

The problem apears to be tied to using UDP Broadcasting.

I've started with UdpClient, and in windows it worked but on linux
I've got an AccesViolation exception. From what I know you have to set
Broadcast option on a Socket under linux to be able to Broadcast
messages so I've tried that:

  SetSocketOption(SocketOptionLevel.Socket, SocketOptio
nName.Broadcast, 1);

but now I'm getting this exception:

System.Security.SecurityException: Invalid socket option
        at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel,
SocketOptionName, Int32) in ./Net/Sockets/Socket.cs:1527
        at repchat.MyUdpClient.enableBroadcast()
...

still didn't work, so I've started looking at Socket.cs and I've found this:

        public void SetSocketOption(SocketOptionLevel optionLevel,
                                                               
SocketOptionName optionName,
                                                                int optionValue)
                        {
                                // Validate the option information to
ensure that the
                                // caller is not trying to set
something that is insecure.
                                if(optionLevel == SocketOptionLevel.Socket)
                                {
                                        if(optionName ==
SocketOptionName.KeepAlive ||
                                           optionName ==
SocketOptionName.ReceiveBuffer ||
                                           optionName ==
SocketOptionName.SendBuffer ||
                                           optionName ==
SocketOptionName.ReuseAddress)
                                        {
                                               
SetSocketOptionRaw(optionLevel, optionName,
                                                                      
            optionValue);
                                                return;
                                        }
                                        if(optionName ==
SocketOptionName.DontLinger)
                                        {
                                                // Convert
"DontLinger" into a "Linger" call.
                                                LingerOption linger =
                                                        new
LingerOption(optionValue != 0, 0);
                                                SetSocketOption(optionLevel,
                                                                      
         SocketOptionName.Linger,
                                                                      
         linger);
                                                return;
                                        }
                                        if(optionName ==
SocketOptionName.ExclusiveAddressUse)
                                        {
                                                // Flip the option and
turn it into "ReuseAddress".
                                                SetSocketOptionRaw(optionLevel,
                                                                      
            SocketOptionName.ReuseAddress,
                                                                      
            (optionValue == 0) ? 1 : 0);
                                                return;
                                        }
                                        if(optionName ==
SocketOptionName.ReceiveTimeout ||
                                           optionName ==
SocketOptionName.SendTimeout)
                                        {
                                                // These options are
fixed by the Internet RFC's,
                                                // are should never be
changed by applications.
                                                // But there is
existing C# code that thinks they
                                                // can be set to
different values.  Quietly ignore.
                                                return;
                                        }
                                }
...

so when you want to set an option for a socket it tries to validate
the combination used and if I'm not wrong if you want to set
"SocketOptio
nName.Broadcast" it fails the test and throws an execption.

so is there annother way to make broadcasting work ?

thanks


reply via email to

[Prev in Thread] Current Thread [Next in Thread]