| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250 |
- // System.Net.Sockets.UdpClientTest.cs
- //
- // Authors:
- // Chris Bacon <[email protected]>
- // Gert Driesen <[email protected]>
- //
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using System.Threading.Tasks;
- using NUnit.Framework;
- namespace MonoTests.System.Net.Sockets {
- [TestFixture]
- public class UdpClientTest {
- [Test] // .ctor ()
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor1 ()
- {
- MyUdpClient client;
- Socket s;
- client = new MyUdpClient ();
- s = client.Client;
- Assert.IsNotNull (s, "Client");
- Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
- Assert.IsFalse (s.Connected, "Client:Connected");
- Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
- Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
- Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
- Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
- Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
- Assert.IsFalse (client.Active, "Active");
- Assert.IsFalse (client.DontFragment, "DontFragment");
- Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
- //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
- Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
- //Assert.AreEqual (32, client.Ttl, "Ttl");
- client.Close ();
- }
- [Test] // .ctor (AddressFamily)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor2 ()
- {
- MyUdpClient client;
- Socket s;
- client = new MyUdpClient (AddressFamily.InterNetwork);
- s = client.Client;
- Assert.IsNotNull (s, "#A:Client");
- Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
- Assert.IsFalse (s.Connected, "#A:Client:Connected");
- Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
- Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
- Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
- Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
- Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
- Assert.IsFalse (client.Active, "#A:Active");
- //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
- Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
- //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
- Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
- //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- client = new MyUdpClient (AddressFamily.InterNetworkV6);
- s = client.Client;
- Assert.IsNotNull (s, "#B:Client");
- Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
- Assert.IsFalse (s.Connected, "#B:Client:Connected");
- Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
- Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
- Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
- Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
- Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
- Assert.IsFalse (client.Active, "#B:Active");
- //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
- Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
- //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
- Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
- //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
- client.Close ();
- }
- [Test] // .ctor (AddressFamily)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor2_Family_Invalid ()
- {
- try {
- new UdpClient (AddressFamily.NetBios);
- Assert.Fail ("#A1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- // 'UDP' Client can only accept InterNetwork or InterNetworkV6
- // addresses
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("family", ex.ParamName, "#A5");
- }
- try {
- new UdpClient ((AddressFamily) 666);
- Assert.Fail ("#B1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.AreEqual ("family", ex.ParamName, "#B5");
- }
- }
- [Test] // .ctor (Int32)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor3 ()
- {
- Socket s;
- IPEndPoint localEP;
- using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort))
- {
- s = client.Client;
- Assert.IsNotNull (s, "#A:Client");
- Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
- Assert.IsFalse (s.Connected, "#A:Client:Connected");
- Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
- Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
- Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
- Assert.IsFalse (client.Active, "#A:Active");
- Assert.IsFalse (client.DontFragment, "#A:DontFragment");
- Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
- //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
- Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
- //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
- localEP = s.LocalEndPoint as IPEndPoint;
- Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
- Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
- Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
- }
- using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
- {
- s = client.Client;
- Assert.IsNotNull (s, "#B:Client");
- Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
- Assert.IsFalse (s.Connected, "#B:Client:Connected");
- Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
- Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
- Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
- Assert.IsFalse (client.Active, "#B:Active");
- Assert.IsFalse (client.DontFragment, "#B:DontFragment");
- Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
- //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
- Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
- //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
- localEP = s.LocalEndPoint as IPEndPoint;
- Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
- Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
- Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
- Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
- }
- }
- [Test] // .ctor (Int32)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor3_Port_OutOfRange ()
- {
- try {
- new UdpClient (IPEndPoint.MaxPort + 1);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("port", ex.ParamName, "#A5");
- }
- try {
- new UdpClient (IPEndPoint.MinPort - 1);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("port", ex.ParamName, "#A5");
- }
- }
- [Test] // .ctor (IPEndPoint)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor4 ()
- {
- Socket s;
- IPEndPoint localEP;
- IPEndPoint clientEP;
- clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
- using (MyUdpClient client = new MyUdpClient (clientEP))
- {
- s = client.Client;
- Assert.IsNotNull (s, "#A:Client");
- Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
- Assert.IsFalse (s.Connected, "#A:Client:Connected");
- Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
- Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
- Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
- Assert.IsFalse (client.Active, "#A:Active");
- Assert.IsFalse (client.DontFragment, "#A:DontFragment");
- Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
- //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
- Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
- //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
- localEP = s.LocalEndPoint as IPEndPoint;
- Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
- Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
- Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
- Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
- Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
- }
- }
- [Test] // .ctor (IPEndPoint)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor4_LocalEP_Null ()
- {
- try {
- new UdpClient ((IPEndPoint) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("localEP", ex.ParamName, "#5");
- }
- }
- [Test] // .ctor (Int32, AddressFamily)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor5 ()
- {
- Socket s;
- IPEndPoint localEP;
- using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
- {
- s = client.Client;
- Assert.IsNotNull (s, "#A:Client");
- Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
- Assert.IsFalse (s.Connected, "#A:Client:Connected");
- Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
- Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
- Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
- Assert.IsFalse (client.Active, "#A:Active");
- //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
- Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
- //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
- Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
- //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
- localEP = s.LocalEndPoint as IPEndPoint;
- Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
- Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
- Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
- }
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
- {
- s = client.Client;
- Assert.IsNotNull (s, "#B:Client");
- Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
- Assert.IsFalse (s.Connected, "#B:Client:Connected");
- Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
- Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
- Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
- Assert.IsFalse (client.Active, "#B:Active");
- //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
- Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
- //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
- Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
- //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
- localEP = s.LocalEndPoint as IPEndPoint;
- Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
- Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
- Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
- Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
- }
- }
- [Test] // .ctor (Int32, AddressFamily)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor5_Family_Invalid ()
- {
- try {
- new UdpClient (80, AddressFamily.NetBios);
- Assert.Fail ("#A1");
- } catch (ArgumentException ex) {
- // family
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- // 'UDP' Client can only accept InterNetwork or InterNetworkV6
- // addresses
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("family", ex.ParamName, "#A5");
- }
- try {
- new UdpClient (80, (AddressFamily) 666);
- Assert.Fail ("#B1");
- } catch (ArgumentException ex) {
- // family
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- // 'UDP' Client can only accept InterNetwork or InterNetworkV6
- // addresses
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.AreEqual ("family", ex.ParamName, "#B5");
- }
- }
- [Test] // .ctor (Int32, AddressFamily)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor5_Port_OutOfRange ()
- {
- try {
- new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("port", ex.ParamName, "#A5");
- }
- try {
- new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("port", ex.ParamName, "#A5");
- }
- }
- [Test] // .ctor (String, Int32)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor6 ()
- {
- Socket s;
- IPEndPoint localEP;
- // Bug #5503
- // UDP port 0 doesn't seem to be valid.
- using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
- {
- s = client.Client;
- Assert.IsNotNull (s, "#A:Client");
- Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
- Assert.IsTrue (s.Connected, "#A:Client:Connected");
- Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
- Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
- Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
- Assert.IsTrue (client.Active, "#A:Active");
- Assert.IsFalse (client.DontFragment, "#A:DontFragment");
- Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
- //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
- //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
- //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
- localEP = s.LocalEndPoint as IPEndPoint;
- Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
- Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
- Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
- }
- using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
- {
- s = client.Client;
- Assert.IsNotNull (s, "#B:Client");
- Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
- Assert.IsTrue (s.Connected, "#B:Client:Connected");
- Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
- Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
- Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
- Assert.IsTrue (client.Active, "#B:Active");
- Assert.IsFalse (client.DontFragment, "#B:DontFragment");
- Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
- //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
- //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
- //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
- localEP = s.LocalEndPoint as IPEndPoint;
- Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
- Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
- Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
- }
- }
- [Test] // .ctor (String, Int32)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor6_HostName_Null ()
- {
- try {
- new UdpClient ((string) null, int.MaxValue);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("hostname", ex.ParamName, "#5");
- }
- }
- [Test] // .ctor (String, Int32)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Constructor6_Port_OutOfRange ()
- {
- try {
- new UdpClient ("local", IPEndPoint.MaxPort + 1);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("port", ex.ParamName, "#A5");
- }
- try {
- new UdpClient ("local", IPEndPoint.MinPort - 1);
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- // Specified argument was out of the range of valid values
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("port", ex.ParamName, "#A5");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void UdpClientBroadcastTest ()
- {
- UdpClient client = new UdpClient ();
- byte[] bytes = new byte[] {10, 11, 12, 13};
- try {
- client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
- } finally {
- client.Close ();
- }
- }
- [Test] // JoinMulticastGroup (IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup1_IPv4 ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
- client.JoinMulticastGroup (mcast_addr);
- }
- }
- [Test] // JoinMulticastGroup (IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup1_IPv6 ()
- {
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
- client.JoinMulticastGroup (mcast_addr);
- }
- }
- [Test] // JoinMulticastGroup (IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup1_MulticastAddr_Null ()
- {
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
- try {
- client.JoinMulticastGroup ((IPAddress) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
- }
- }
- }
- [Test] // JoinMulticastGroup (IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup1_Socket_Closed ()
- {
- IPAddress mcast_addr = null;
- UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
- client.Close ();
- try {
- client.JoinMulticastGroup (mcast_addr);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // JoinMulticastGroup (IPAddress)
- [Category ("NotWorking")]
- public void JoinMulticastGroup1_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
- UdpClient client = new UdpClient (AddressFamily.InterNetwork);
- try {
- client.JoinMulticastGroup (mcast_addr);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } finally {
- client.Close ();
- }
- }
- [Test] // JoinMulticastGroup (In32, IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup2_IPv4 ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
- try {
- client.JoinMulticastGroup (0, mcast_addr);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // The attempted operation is not supported for the type of
- // object referenced
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10045, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
- }
- }
- }
- [Test] // JoinMulticastGroup (In32, IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup2_IPv6 ()
- {
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
- client.JoinMulticastGroup (0, mcast_addr);
- }
- }
- [Test] // JoinMulticastGroup (Int32, IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup2_MulticastAddr_Null ()
- {
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
- try {
- client.JoinMulticastGroup (0, (IPAddress) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
- }
- }
- }
- [Test] // JoinMulticastGroup (Int32, IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup2_Socket_Closed ()
- {
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = null;
- UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
- client.Close ();
- try {
- client.JoinMulticastGroup (0, mcast_addr);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // JoinMulticastGroup (Int32, IPAddress)
- [Category ("NotWorking")]
- public void JoinMulticastGroup2_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
- try {
- client.JoinMulticastGroup (0, mcast_addr);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } finally {
- client.Close ();
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, Int32)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup3_IPv4 ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
- client.JoinMulticastGroup (mcast_addr, 0);
- }
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
- client.JoinMulticastGroup (mcast_addr, 255);
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, Int32)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup3_IPv6 ()
- {
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
- client.JoinMulticastGroup (mcast_addr, 0);
- }
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
- client.JoinMulticastGroup (mcast_addr, 255);
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, Int32)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup3_MulticastAddr_Null ()
- {
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
- try {
- client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
- }
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, Int32)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup3_Socket_Closed ()
- {
- IPAddress mcast_addr = null;
- UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
- client.Close ();
- try {
- client.JoinMulticastGroup (mcast_addr, 0);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, Int32)
- [Category ("NotWorking")]
- public void JoinMulticastGroup3_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
- UdpClient client = new UdpClient (AddressFamily.InterNetwork);
- try {
- client.JoinMulticastGroup (mcast_addr, 5);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } finally {
- client.Close ();
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup4_IPv4 ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
- IPAddress local_addr = IPAddress.Any;
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
- client.JoinMulticastGroup (mcast_addr, local_addr);
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup4_IPv6 ()
- {
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
- IPAddress local_addr = IPAddress.IPv6Any;
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
- try {
- client.JoinMulticastGroup (mcast_addr, local_addr);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // The attempted operation is not supported for the type of
- // object referenced
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10045, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
- }
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup4_LocalAddress_Null ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
- try {
- client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("mcint", ex.ParamName, "#5");
- }
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup4_MulticastAddr_Null ()
- {
- using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
- try {
- client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("group", ex.ParamName, "#5");
- }
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, IPAddress)
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroup4_Socket_Closed ()
- {
- IPAddress mcast_addr = null;
- IPAddress local_addr = null;
- UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
- client.Close ();
- try {
- client.JoinMulticastGroup (mcast_addr, local_addr);
- Assert.Fail ("#1");
- } catch (ObjectDisposedException ex) {
- // Cannot access a disposed object
- Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
- }
- }
- [Test] // JoinMulticastGroup (IPAddress, IPAddress)
- [Category ("NotWorking")]
- public void JoinMulticastGroup4_Socket_NotBound ()
- {
- IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
- IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
- UdpClient client = new UdpClient (AddressFamily.InterNetwork);
- try {
- client.JoinMulticastGroup (mcast_addr, local_addr);
- Assert.Fail ("#1");
- } catch (SocketException ex) {
- // An invalid argument was supplied
- Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
- Assert.AreEqual (10022, ex.ErrorCode, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
- Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
- } finally {
- client.Close ();
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void CloseInReceive ()
- {
- UdpClient client = null;
- var rnd = new Random ();
- for (int i = 0, max = 5; i < max; i++) {
- int port = rnd.Next (1025, 65534);
- try {
- client = new UdpClient (port);
- break;
- } catch (Exception) {
- if (i == max - 1)
- throw;
- }
- }
- ManualResetEvent ready = new ManualResetEvent (false);
- bool got_exc = false;
- Task receive_task = Task.Factory.StartNew (() => {
- IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
- try {
- ready.Set ();
- client.Receive(ref ep);
- } catch (SocketException) {
- got_exc = true;
- } finally {
- client.Close ();
- }
- });
- ready.WaitOne (2000);
- Thread.Sleep (20);
- client.Close();
- Assert.IsTrue (receive_task.Wait (1000));
- Assert.IsTrue (got_exc);
- }
- // Test for bug 324033
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void JoinMulticastGroupWithLocal ()
- {
- UdpClient client = new UdpClient (9001);
- IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
- IPAddress local_addr = IPAddress.Any;
- try {
- client.JoinMulticastGroup (mcast_addr, local_addr);
- } finally {
- client.Close ();
- }
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof(ArgumentNullException))]
- #endif
- public void BeginSendNull ()
- {
- UdpClient client = new UdpClient ();
- client.BeginSend (null, 0, null, null);
- client.Close ();
- }
-
- static bool BSSent = false;
- static int BSBytes;
- static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
-
- private static void BSCallback (IAsyncResult asyncResult)
- {
- UdpClient client = (UdpClient)asyncResult.AsyncState;
-
- BSBytes = client.EndSend (asyncResult);
-
- BSSent = true;
- BSCalledBack.Set ();
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginSend ()
- {
- UdpClient client = new UdpClient ();
- byte[] bytes = new byte[] {10, 11, 12, 13};
- try {
- client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
- Assert.Fail ("BeginSend #1");
- } catch (SocketException ex) {
- Assert.AreEqual (10057, ex.ErrorCode,
- "BeginSend #2");
- }
-
- try {
- client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
- Assert.Fail ("BeginSend #3");
- } catch (SocketException ex) {
- Assert.AreEqual (10057, ex.ErrorCode,
- "BeginSend #4");
- }
- IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
-
- BSCalledBack.Reset ();
-
- client.BeginSend (bytes, bytes.Length, ep,
- new AsyncCallback (BSCallback),
- client);
- if (BSCalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginSend wait timed out");
- }
-
- Assert.AreEqual (true, BSSent, "BeginSend #5");
- Assert.AreEqual (4, BSBytes, "BeginSend #6");
- client.Close ();
- }
-
- static bool BRReceived = false;
- static byte[] BRBytes;
- static IPEndPoint BRFrom;
- static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
-
- private static void BRCallback (IAsyncResult asyncResult)
- {
- UdpClient client = (UdpClient)asyncResult.AsyncState;
-
- BRBytes = client.EndReceive (asyncResult, ref BRFrom);
-
- BRReceived = true;
- BRCalledBack.Set ();
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void BeginReceive ()
- {
- UdpClient client = new UdpClient (1237);
-
- BRCalledBack.Reset ();
-
- client.BeginReceive (BRCallback, client);
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
- byte[] send_bytes = new byte[] {10, 11, 12, 13};
- client.Send (send_bytes, send_bytes.Length, ep);
- if (BRCalledBack.WaitOne (2000, false) == false) {
- Assert.Fail ("BeginReceive wait timed out");
- }
-
- Assert.AreEqual (true, BRReceived, "BeginReceive #1");
- Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
- Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
- Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
- client.Close ();
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Available ()
- {
- using (UdpClient client = new UdpClient (1238)) {
- IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
- byte[] bytes = new byte[] {10, 11, 12, 13};
-
- int res = client.Send (bytes, bytes.Length, ep);
- Assert.AreEqual (bytes.Length, res, "Send");
- // that might happen too quickly, data sent and not yet received/available
- Thread.Sleep (100);
- int avail = client.Available;
-
- Assert.AreEqual (bytes.Length, avail, "Available #1");
- client.Close ();
- }
- }
-
- [Test]
- [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
- public void DontFragmentDefault ()
- {
- UdpClient client = new UdpClient ();
-
- /* Ignore the docs, testing shows the default
- * here is in fact false
- */
- Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
- client.Close ();
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void EnableBroadcastDefault ()
- {
- UdpClient client = new UdpClient ();
-
- Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
- client.Close ();
- }
-
- /* Can't test the default for ExclusiveAddressUse as
- * it's different on different versions and service
- * packs of windows
- */
- [Test]
- [Category ("NotWorking")] // Not supported on Linux
- public void ExclusiveAddressUseUnbound ()
- {
- UdpClient client = new UdpClient ();
- client.ExclusiveAddressUse = true;
- Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
- client.Close ();
- }
-
- [Test]
- [ExpectedException (typeof(InvalidOperationException))]
- [Category ("NotWorking")] // Not supported on Linux
- public void ExclusiveAddressUseBound ()
- {
- UdpClient client = new UdpClient (1239);
- client.ExclusiveAddressUse = true;
- client.Close ();
- }
-
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void MulticastLoopbackDefault ()
- {
- UdpClient client = new UdpClient ();
-
- Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
- client.Close ();
- }
- [Test] // #6057
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void ReceiveIPv6 ()
- {
- if (!Socket.OSSupportsIPv6)
- Assert.Ignore ("IPv6 not enabled.");
- int PORT = 9997;
- using(var udpClient = new UdpClient (PORT, AddressFamily.InterNetworkV6))
- using(var udpClient2 = new UdpClient (PORT+1, AddressFamily.InterNetworkV6))
- {
- var dataSent = new byte [] {1,2,3};
- udpClient2.SendAsync (dataSent, dataSent.Length, "::1", PORT);
- IPEndPoint endPoint = new IPEndPoint (IPAddress.IPv6Any, 0);
- var data = udpClient.Receive (ref endPoint);
- Assert.AreEqual (dataSent.Length, data.Length);
- }
- }
-
- /* No test for Ttl default as it is platform dependent */
- class MyUdpClient : UdpClient
- {
- public MyUdpClient ()
- {
- }
- public MyUdpClient (AddressFamily family)
- : base (family)
- {
- }
- public MyUdpClient (Int32 port)
- : base (port)
- {
- }
- public MyUdpClient (IPEndPoint localEP)
- : base (localEP)
- {
- }
- public MyUdpClient (int port, AddressFamily family)
- : base (port, family)
- {
- }
- public MyUdpClient (string hostname, int port)
- : base (hostname, port)
- {
- }
- public new bool Active {
- get { return base.Active; }
- set { base.Active = value; }
- }
- }
- }
- }
|