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 {
\r
16 [Ignore("UdpClient is not supported - since UDP sockets are not supported")]
\r
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");
66 [ExpectedException (typeof(ArgumentNullException))]
67 public void BeginSendNull ()
69 UdpClient client = new UdpClient ();
71 client.BeginSend (null, 0, null, null);
76 static bool BSSent = false;
78 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
80 private static void BSCallback (IAsyncResult asyncResult)
82 UdpClient client = (UdpClient)asyncResult.AsyncState;
84 BSBytes = client.EndSend (asyncResult);
91 public void BeginSend ()
93 UdpClient client = new UdpClient ();
94 byte[] bytes = new byte[] {10, 11, 12, 13};
97 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
98 Assert.Fail ("BeginSend #1");
99 } catch (SocketException ex) {
100 Assert.AreEqual (10057, ex.ErrorCode,
105 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
106 Assert.Fail ("BeginSend #3");
107 } catch (SocketException ex) {
108 Assert.AreEqual (10057, ex.ErrorCode,
112 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1236);
114 BSCalledBack.Reset ();
116 client.BeginSend (bytes, bytes.Length, ep,
117 new AsyncCallback (BSCallback),
119 if (BSCalledBack.WaitOne (2000, false) == false) {
120 Assert.Fail ("BeginSend wait timed out");
123 Assertion.AssertEquals ("BeginSend #5", true, BSSent);
124 Assertion.AssertEquals ("BeginSend #6", 4, BSBytes);
129 static bool BRReceived = false;
130 static byte[] BRBytes;
131 static IPEndPoint BRFrom;
132 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
134 private static void BRCallback (IAsyncResult asyncResult)
136 UdpClient client = (UdpClient)asyncResult.AsyncState;
138 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
145 public void BeginReceive ()
147 UdpClient client = new UdpClient (1237);
149 BRCalledBack.Reset ();
151 client.BeginReceive (BRCallback, client);
153 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1237);
154 byte[] send_bytes = new byte[] {10, 11, 12, 13};
155 client.Send (send_bytes, send_bytes.Length, ep);
157 if (BRCalledBack.WaitOne (2000, false) == false) {
158 Assert.Fail ("BeginReceive wait timed out");
161 Assertion.AssertEquals ("BeginReceive #1", true,
163 Assertion.AssertEquals ("BeginReceive #2", 4,
165 Assertion.AssertEquals ("BeginReceive #3", ep. Port,
167 Assertion.AssertEquals ("BeginReceive #4", ep.Address,
174 public void Available ()
176 UdpClient client = new UdpClient (1238);
177 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry ("").AddressList[0], 1238);
178 byte[] bytes = new byte[] {10, 11, 12, 13};
180 client.Send (bytes, bytes.Length, ep);
181 int avail = client.Available;
183 Assertion.AssertEquals ("Available #1", bytes.Length,
190 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
191 public void DontFragmentDefault ()
193 UdpClient client = new UdpClient ();
195 /* Ignore the docs, testing shows the default
196 * here is in fact false
198 Assertion.AssertEquals ("DontFragmentDefault", false,
199 client.DontFragment);
205 public void EnableBroadcastDefault ()
207 UdpClient client = new UdpClient ();
209 Assertion.AssertEquals ("EnableBroadcastDefault",
210 false, client.EnableBroadcast);
215 /* Can't test the default for ExclusiveAddressUse as
216 * it's different on different versions and service
220 [Category ("NotWorking")] // Not supported on Linux
221 public void ExclusiveAddressUseUnbound ()
223 UdpClient client = new UdpClient ();
225 client.ExclusiveAddressUse = true;
227 Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
229 client.ExclusiveAddressUse);
235 [ExpectedException (typeof(InvalidOperationException))]
236 [Category ("NotWorking")] // Not supported on Linux
237 public void ExclusiveAddressUseBound ()
239 UdpClient client = new UdpClient (1239);
241 client.ExclusiveAddressUse = true;
247 public void MulticastLoopbackDefault ()
249 UdpClient client = new UdpClient ();
251 Assertion.AssertEquals ("MulticastLoopbackDefault",
253 client.MulticastLoopback);
258 /* No test for Ttl default as it is platform dependent */