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 // listen with a new listener (IPv4 is the default)
32 TcpListener inListener
= NetworkHelpers
.CreateAndStartTcpListener (out int port
);
35 // connect to it from a new socket
36 IPHostEntry hostent
= Dns
.GetHostByAddress (IPAddress
.Loopback
);
37 Socket outSock
= null;
39 foreach (IPAddress address
in hostent
.AddressList
) {
40 if (address
.AddressFamily
== AddressFamily
.InterNetwork
) {
41 /// Only keep IPv4 addresses, our Server is in IPv4 only mode.
42 outSock
= new Socket (address
.AddressFamily
, SocketType
.Stream
,
44 IPEndPoint remote
= new IPEndPoint (address
, port
);
45 outSock
.Connect (remote
);
50 // There is no guarantee that the connecting socket will be in the listener's
51 // accept queue yet (though it is highly likely on Linux). We wait up to one
52 // second for the connecting socket to enter the listener's accept queue.
53 Assert
.IsTrue (inListener
.Server
.Poll (1000, SelectMode
.SelectRead
));
54 Assert
.IsTrue (inListener
.Pending ());
55 Socket inSock
= inListener
.AcceptSocket ();
57 // now send some data and see if it comes out the other end
59 byte[] outBuf
= new Byte
[len
];
60 for (int i
=0; i
<len
; i
++)
61 outBuf
[i
] = (byte) (i
% 256);
63 outSock
.Send (outBuf
, 0, len
, 0);
65 byte[] inBuf
= new Byte
[len
];
66 int ret
= inSock
.Receive (inBuf
, 0, len
, 0);
69 // let's see if it arrived OK
70 Assert
.IsTrue (ret
!= 0);
71 for (int i
=0; i
<len
; i
++)
72 Assert
.IsTrue (inBuf
[i
] == outBuf
[i
]);
74 // tidy up after ourselves
81 #if FEATURE_NO_BSD_SOCKETS
82 [ExpectedException (typeof (PlatformNotSupportedException
))]
84 public void CtorInt1 ()
87 try { new TcpListener (-1); }
catch { nex++; }
89 new TcpListener (65535);
90 try { new TcpListener (65536); }
catch { nex++; }
91 try { new TcpListener (100000); }
catch { nex++; }
92 Assert
.IsTrue (nex
== 3);
96 #if FEATURE_NO_BSD_SOCKETS && !WASM
97 [ExpectedException (typeof (PlatformNotSupportedException
))]
99 [ExpectedException (typeof (ArgumentNullException
))]
101 public void CtorIPEndPoint ()
103 new TcpListener (null);
107 #if FEATURE_NO_BSD_SOCKETS && !WASM
108 [ExpectedException (typeof (PlatformNotSupportedException
))]
110 [ExpectedException (typeof (ArgumentNullException
))]
112 public void CtorIPAddressInt1 ()
114 new TcpListener (null, 100000);
118 #if FEATURE_NO_BSD_SOCKETS && !WASM
119 [ExpectedException (typeof (PlatformNotSupportedException
))]
121 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
123 public void CtorIPAddressInt2 ()
125 new TcpListener (IPAddress
.Any
, 100000);
128 class MyListener
: TcpListener
131 : base (IPAddress
.Loopback
, 0)
135 public Socket
GetSocket ()
140 public bool IsActive
{
141 get { return Active; }
146 #if FEATURE_NO_BSD_SOCKETS
147 [ExpectedException (typeof (PlatformNotSupportedException
))]
149 public void PreStartStatus ()
151 MyListener listener
= new MyListener ();
152 Assert
.AreEqual (false, listener
.IsActive
, "#01");
153 Assert
.IsTrue (null != listener
.GetSocket (), "#02");
155 listener
.AcceptSocket ();
156 Assert
.Fail ("Exception not thrown");
157 } catch (InvalidOperationException
) {
161 listener
.AcceptTcpClient ();
162 Assert
.Fail ("Exception not thrown");
163 } catch (InvalidOperationException
) {
168 Assert
.Fail ("Exception not thrown");
169 } catch (InvalidOperationException
) {
176 #if FEATURE_NO_BSD_SOCKETS
177 [ExpectedException (typeof (PlatformNotSupportedException
))]
179 public void PostStartStatus ()
181 MyListener listener
= new MyListener ();
183 Assert
.AreEqual (true, listener
.IsActive
, "#01");
184 Assert
.IsTrue (null != listener
.GetSocket (), "#02");
186 Socket sock
= listener
.GetSocket ();
187 listener
.Start (); // Start called twice
188 Assert
.AreEqual (true, listener
.IsActive
, "#03");
189 Assert
.IsTrue (null != listener
.GetSocket (), "#04");
191 Assert
.AreEqual (false, listener
.Pending (), "#05");
194 Assert
.AreEqual (false, listener
.IsActive
, "#06");
195 Assert
.IsTrue (null != listener
.GetSocket (), "#07");
196 Assert
.IsTrue (sock
!= listener
.GetSocket (), "#08");
200 #if FEATURE_NO_BSD_SOCKETS
201 [ExpectedException (typeof (PlatformNotSupportedException
))]
203 public void StartListenMoreThan5 ()
205 TcpListener listen
= new TcpListener (IPAddress
.Loopback
, 0);
216 listen
.Start (32768);
219 listen
.Start (65536);
224 #if FEATURE_NO_BSD_SOCKETS
225 [ExpectedException (typeof (PlatformNotSupportedException
))]
227 public void EndAcceptTcpClient ()
229 var listenerSocket
= NetworkHelpers
.CreateAndStartTcpListener (IPAddress
.Any
, out int port
);
230 listenerSocket
.BeginAcceptTcpClient (new AsyncCallback (l
=> {
231 listenerSocket
.EndAcceptTcpClient (l
);
235 using (var outClient
= new TcpClient ("localhost", port
)) {
236 using (var stream
= outClient
.GetStream ()) {
237 stream
.WriteByte (3);