1 // System.Net.Sockets.UdpClientTest.cs
4 // Chris Bacon <chris.bacon@docobo.co.uk>
10 using System.Net.Sockets;
11 using System.Threading;
12 using NUnit.Framework;
14 namespace MonoTests.System.Net.Sockets {
16 [Ignore("UdpClient is not supported - since UDP sockets are not supported")]
19 public class UdpClientTest {
21 public void UdpClientBroadcastTest ()
23 bool exThrown = false;
24 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
25 byte[] bytes = new byte[] {10, 11, 12, 13};
28 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
29 } catch (SocketException) {
32 Assert.IsFalse(exThrown, "UdpClient Broadcast #1");
39 public void JoinMulticastGroup ()
41 UdpClient client = new UdpClient ();
42 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
43 IPAddress local_addr = Dns.GetHostEntry ("").AddressList[0];
44 bool exThrown = false;
46 /* So much for the documented note "You cannot
47 * call JoinMulticastGroup on a UdpClient
48 * constructed without a specific local port
49 * (that is, using the UdpClient or
50 * UdpClient(AddressFamily) constructor).
53 client.JoinMulticastGroup (mcast_addr,
61 Assert.IsFalse (exThrown,
62 "UdpClient JoinMulticastGroup #1");
65 // Test for bug 324033
67 public void JoinMulticastGroupWithLocal ()
69 UdpClient client = new UdpClient (9001);
70 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
71 IPAddress local_addr = IPAddress.Any;
73 bool exThrown = false;
76 client.JoinMulticastGroup (mcast_addr,
84 Assert.IsFalse (exThrown, "UdpClient JoinMulticastGroupWithLocal #1");
88 [ExpectedException (typeof(ArgumentNullException))]
89 public void BeginSendNull ()
91 UdpClient client = new UdpClient ();
93 client.BeginSend (null, 0, null, null);
98 static bool BSSent = false;
100 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
102 private static void BSCallback (IAsyncResult asyncResult)
104 UdpClient client = (UdpClient)asyncResult.AsyncState;
106 BSBytes = client.EndSend (asyncResult);
113 public void BeginSend ()
115 UdpClient client = new UdpClient ();
116 byte[] bytes = new byte[] {10, 11, 12, 13};
119 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
120 Assert.Fail ("BeginSend #1");
121 } catch (SocketException ex) {
122 Assert.AreEqual (10057, ex.ErrorCode,
127 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
128 Assert.Fail ("BeginSend #3");
129 } catch (SocketException ex) {
130 Assert.AreEqual (10057, ex.ErrorCode,
134 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1236);
136 BSCalledBack.Reset ();
138 client.BeginSend (bytes, bytes.Length, ep,
139 new AsyncCallback (BSCallback),
141 if (BSCalledBack.WaitOne (2000, false) == false) {
142 Assert.Fail ("BeginSend wait timed out");
145 Assertion.AssertEquals ("BeginSend #5", true, BSSent);
146 Assertion.AssertEquals ("BeginSend #6", 4, BSBytes);
151 static bool BRReceived = false;
152 static byte[] BRBytes;
153 static IPEndPoint BRFrom;
154 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
156 private static void BRCallback (IAsyncResult asyncResult)
158 UdpClient client = (UdpClient)asyncResult.AsyncState;
160 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
167 public void BeginReceive ()
169 UdpClient client = new UdpClient (1237);
171 BRCalledBack.Reset ();
173 client.BeginReceive (BRCallback, client);
175 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1237);
176 byte[] send_bytes = new byte[] {10, 11, 12, 13};
177 client.Send (send_bytes, send_bytes.Length, ep);
179 if (BRCalledBack.WaitOne (2000, false) == false) {
180 Assert.Fail ("BeginReceive wait timed out");
183 Assertion.AssertEquals ("BeginReceive #1", true,
185 Assertion.AssertEquals ("BeginReceive #2", 4,
187 Assertion.AssertEquals ("BeginReceive #3", ep. Port,
189 Assertion.AssertEquals ("BeginReceive #4", ep.Address,
196 public void Available ()
198 UdpClient client = new UdpClient (1238);
199 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1238);
200 byte[] bytes = new byte[] {10, 11, 12, 13};
202 client.Send (bytes, bytes.Length, ep);
203 int avail = client.Available;
205 Assertion.AssertEquals ("Available #1", bytes.Length,
212 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
213 public void DontFragmentDefault ()
215 UdpClient client = new UdpClient ();
217 /* Ignore the docs, testing shows the default
218 * here is in fact false
220 Assertion.AssertEquals ("DontFragmentDefault", false,
221 client.DontFragment);
227 public void EnableBroadcastDefault ()
229 UdpClient client = new UdpClient ();
231 Assertion.AssertEquals ("EnableBroadcastDefault",
232 false, client.EnableBroadcast);
237 /* Can't test the default for ExclusiveAddressUse as
238 * it's different on different versions and service
242 [Category ("NotWorking")] // Not supported on Linux
243 public void ExclusiveAddressUseUnbound ()
245 UdpClient client = new UdpClient ();
247 client.ExclusiveAddressUse = true;
249 Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
251 client.ExclusiveAddressUse);
257 [ExpectedException (typeof(InvalidOperationException))]
258 [Category ("NotWorking")] // Not supported on Linux
259 public void ExclusiveAddressUseBound ()
261 UdpClient client = new UdpClient (1239);
263 client.ExclusiveAddressUse = true;
269 public void MulticastLoopbackDefault ()
271 UdpClient client = new UdpClient ();
273 Assertion.AssertEquals ("MulticastLoopbackDefault",
275 client.MulticastLoopback);
280 /* No test for Ttl default as it is platform dependent */