2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System / System.Net.Sockets / UdpClient.cs
index 2a3aed47d989e65ba938639fe13fe340d8b0b843..de358efdaaf4861daf21acac018c94e91b5c403b 100644 (file)
@@ -6,6 +6,27 @@
 //\r
 // Copyright (C) Ximian, Inc. http://www.ximian.com\r
 //\r
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 \r
 using System;\r
 using System.Net;\r
@@ -24,6 +45,7 @@ namespace System.Net.Sockets
                {\r
                }\r
 \r
+#if NET_1_1
                public UdpClient(AddressFamily family)\r
                {\r
                        if(family != AddressFamily.InterNetwork && family != AddressFamily.InterNetwork)\r
@@ -32,6 +54,7 @@ namespace System.Net.Sockets
                        this.family = family;\r
                        InitSocket (null);\r
                }\r
+#endif
 \r
                public UdpClient (int port)\r
                {\r
@@ -53,7 +76,27 @@ namespace System.Net.Sockets
 \r
                        InitSocket (localEP);\r
                }\r
-\r
+
+#if NET_1_1
+               public UdpClient (int port, AddressFamily family)
+               {
+                       if (family != AddressFamily.InterNetwork &&
+                           family != AddressFamily.InterNetworkV6) {
+                               throw new ArgumentException ("Family must be InterNetwork or InterNetworkV6", "family");
+                       }
+                       
+                       if (port < IPEndPoint.MinPort ||
+                           port > IPEndPoint.MaxPort) {
+                               throw new ArgumentOutOfRangeException ("port");
+                       }
+                       
+                       this.family = family;
+
+                       IPEndPoint localEP = new IPEndPoint (IPAddress.Any, port);
+                       InitSocket (localEP);
+               }
+#endif
+               
                public UdpClient (string hostname, int port)\r
                {\r
                        if (hostname == null)\r
@@ -117,15 +160,10 @@ namespace System.Net.Sockets
                        IPAddress[] addresses = Dns.Resolve (hostname).AddressList;\r
                        for(int i=0; i<addresses.Length; i++) {\r
                                try {\r
-                                       Console.WriteLine("Trying: {0}, Family: {1}", addresses[i], addresses[i].AddressFamily);\r
-\r
                                        this.family = addresses[i].AddressFamily;\r
                                        Connect (new IPEndPoint (addresses[i], port));\r
-\r
-                                       Console.WriteLine("Connected: {0}, Family: {1}", addresses[i], family);\r
                                        break;\r
-                               }\r
-                               catch(Exception e) {\r
+                               } catch(Exception e) {\r
                                        if(i == addresses.Length - 1){\r
                                                if(socket != null) {\r
                                                        socket.Close();\r
@@ -137,7 +175,6 @@ namespace System.Net.Sockets
                                        }\r
                                }\r
                        }\r
-\r
                }\r
 #endregion\r
                #region Multicast methods\r
@@ -156,7 +193,31 @@ namespace System.Net.Sockets
                                        new IPv6MulticastOption (multicastAddr));\r
 #endif\r
                }\r
-\r
+
+#if NET_1_1
+               public void DropMulticastGroup (IPAddress multicastAddr,
+                                               int ifindex)
+               {
+                       CheckDisposed ();
+
+                       /* LAMESPEC: exceptions haven't been specified
+                        * for this overload.
+                        */
+                       if (multicastAddr == null) {
+                               throw new ArgumentNullException ("multicastAddr");
+                       }
+
+                       /* Does this overload only apply to IPv6?
+                        * Only the IPv6MulticastOption has an
+                        * ifindex-using constructor.  The MS docs
+                        * don't say.
+                        */
+                       if (family == AddressFamily.InterNetworkV6) {
+                               socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership, new IPv6MulticastOption (multicastAddr, ifindex));
+                       }
+               }
+#endif
+               
                public void JoinMulticastGroup (IPAddress multicastAddr)\r
                {\r
                        CheckDisposed ();\r
@@ -170,7 +231,24 @@ namespace System.Net.Sockets
                                        new IPv6MulticastOption (multicastAddr));\r
 #endif\r
                }\r
-\r
+
+#if NET_1_1
+               public void JoinMulticastGroup (int ifindex,
+                                               IPAddress multicastAddr)
+               {
+                       CheckDisposed ();
+
+                       /* Does this overload only apply to IPv6?
+                        * Only the IPv6MulticastOption has an
+                        * ifindex-using constructor.  The MS docs
+                        * don't say.
+                        */
+                       if (family == AddressFamily.InterNetworkV6) {
+                               socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption (multicastAddr, ifindex));
+                       }
+               }
+#endif
+               
                public void JoinMulticastGroup (IPAddress multicastAddr, int timeToLive)\r
                {\r
                        CheckDisposed ();\r
@@ -276,17 +354,16 @@ namespace System.Net.Sockets
 \r
                void Dispose (bool disposing)\r
                {\r
-                       if (disposed) \r
+                       if (disposed)\r
                                return;\r
                        disposed = true;\r
-                       if (disposing) {\r
-                               // release managed resources\r
-                       }                       \r
-                       // release unmanaged resources\r
-                       Socket s = socket;\r
-                       socket = null;\r
-                       if (s != null)\r
-                               s.Close ();\r
+\r
+                       if (disposing){\r
+                               if (socket != null)\r
+                                       socket.Close ();\r
+\r
+                               socket = null;\r
+                       }\r
                }\r
                \r
                ~UdpClient ()\r