1 // System.Net.Sockets.TcpListenerTest.cs
4 // Phillip Pearson (pp@myelin.co.nz)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)
9 // (C) Copyright 2003 Martin Willemoes Hansen (mwh@sysrq.dk)
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
15 using System.Net.Sockets;
16 using NUnit.Framework;
18 using MonoTests.Helpers;
20 namespace MonoTests.System.Net.Sockets
23 public class TcpListenerTest
26 #if FEATURE_NO_BSD_SOCKETS
27 [ExpectedException (typeof (PlatformNotSupportedException))]
29 public void TcpListener ()
31 var port = NetworkHelpers.FindFreePort ();
32 // listen with a new listener (IPv4 is the default)
33 TcpListener inListener = new TcpListener (port);
37 // connect to it from a new socket
38 IPHostEntry hostent = Dns.GetHostByAddress (IPAddress.Loopback);
39 Socket outSock = null;
41 foreach (IPAddress address in hostent.AddressList) {
42 if (address.AddressFamily == AddressFamily.InterNetwork) {
43 /// Only keep IPv4 addresses, our Server is in IPv4 only mode.
44 outSock = new Socket (address.AddressFamily, SocketType.Stream,
46 IPEndPoint remote = new IPEndPoint (address, port);
47 outSock.Connect (remote);
52 // make sure the connection arrives
53 Assert.IsTrue (inListener.Pending ());
54 Socket inSock = inListener.AcceptSocket ();
56 // now send some data and see if it comes out the other end
58 byte[] outBuf = new Byte [len];
59 for (int i=0; i<len; i++)
60 outBuf [i] = (byte) (i % 256);
62 outSock.Send (outBuf, 0, len, 0);
64 byte[] inBuf = new Byte[len];
65 int ret = inSock.Receive (inBuf, 0, len, 0);
68 // let's see if it arrived OK
69 Assert.IsTrue (ret != 0);
70 for (int i=0; i<len; i++)
71 Assert.IsTrue (inBuf[i] == outBuf [i]);
73 // tidy up after ourselves
80 #if FEATURE_NO_BSD_SOCKETS
81 [ExpectedException (typeof (PlatformNotSupportedException))]
83 public void CtorInt1 ()
86 try { new TcpListener (-1); } catch { nex++; }
88 new TcpListener (65535);
89 try { new TcpListener (65536); } catch { nex++; }
90 try { new TcpListener (100000); } catch { nex++; }
91 Assert.IsTrue (nex == 3);
95 #if FEATURE_NO_BSD_SOCKETS
96 [ExpectedException (typeof (PlatformNotSupportedException))]
98 [ExpectedException (typeof (ArgumentNullException))]
100 public void CtorIPEndPoint ()
102 new TcpListener (null);
106 #if FEATURE_NO_BSD_SOCKETS
107 [ExpectedException (typeof (PlatformNotSupportedException))]
109 [ExpectedException (typeof (ArgumentNullException))]
111 public void CtorIPAddressInt1 ()
113 new TcpListener (null, 100000);
117 #if FEATURE_NO_BSD_SOCKETS
118 [ExpectedException (typeof (PlatformNotSupportedException))]
120 [ExpectedException (typeof (ArgumentOutOfRangeException))]
122 public void CtorIPAddressInt2 ()
124 new TcpListener (IPAddress.Any, 100000);
127 class MyListener : TcpListener
130 : base (IPAddress.Loopback, NetworkHelpers.FindFreePort ())
134 public Socket GetSocket ()
139 public bool IsActive {
140 get { return Active; }
145 #if FEATURE_NO_BSD_SOCKETS
146 [ExpectedException (typeof (PlatformNotSupportedException))]
148 public void PreStartStatus ()
150 MyListener listener = new MyListener ();
151 Assert.AreEqual (false, listener.IsActive, "#01");
152 Assert.IsTrue (null != listener.GetSocket (), "#02");
154 listener.AcceptSocket ();
155 Assert.Fail ("Exception not thrown");
156 } catch (InvalidOperationException) {
160 listener.AcceptTcpClient ();
161 Assert.Fail ("Exception not thrown");
162 } catch (InvalidOperationException) {
167 Assert.Fail ("Exception not thrown");
168 } catch (InvalidOperationException) {
175 #if FEATURE_NO_BSD_SOCKETS
176 [ExpectedException (typeof (PlatformNotSupportedException))]
178 public void PostStartStatus ()
180 MyListener listener = new MyListener ();
182 Assert.AreEqual (true, listener.IsActive, "#01");
183 Assert.IsTrue (null != listener.GetSocket (), "#02");
185 Socket sock = listener.GetSocket ();
186 listener.Start (); // Start called twice
187 Assert.AreEqual (true, listener.IsActive, "#03");
188 Assert.IsTrue (null != listener.GetSocket (), "#04");
190 Assert.AreEqual (false, listener.Pending (), "#05");
193 Assert.AreEqual (false, listener.IsActive, "#06");
194 Assert.IsTrue (null != listener.GetSocket (), "#07");
195 Assert.IsTrue (sock != listener.GetSocket (), "#08");
199 #if FEATURE_NO_BSD_SOCKETS
200 [ExpectedException (typeof (PlatformNotSupportedException))]
202 public void StartListenMoreThan5 ()
204 var port = NetworkHelpers.FindFreePort ();
205 TcpListener listen = new TcpListener (IPAddress.Loopback, port);
216 listen.Start (32768);
219 listen.Start (65536);