1 // System.Net.Sockets.TcpClientTest.cs
4 // Phillip Pearson (pp@myelin.co.nz)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)
8 // (C) Copyright 2003 Martin Willemoes Hansen
13 using System.Net.Sockets;
14 using NUnit.Framework;
16 using MonoTests.Helpers;
18 namespace MonoTests.System.Net.Sockets
21 /// Tests System.Net.Sockets.TcpClient
24 public class TcpClientTest
28 /// Tests the TcpClient object
29 /// (from System.Net.Sockets)
32 #if FEATURE_NO_BSD_SOCKETS
33 [ExpectedException (typeof (PlatformNotSupportedException))]
35 public void TcpClient()
37 // set up a listening Socket
38 Socket lSock = new Socket(AddressFamily.InterNetwork,
39 SocketType.Stream, ProtocolType.Tcp);
41 var port = NetworkHelpers.FindFreePort ();
42 lSock.Bind(new IPEndPoint(IPAddress.Any, port));
46 // connect to it with a TcpClient
47 TcpClient outClient = new TcpClient("localhost", port);
48 Socket inSock = lSock.Accept();
51 // now try exchanging data
52 NetworkStream stream = outClient.GetStream();
55 byte[] outBuf = new Byte[len];
56 for (int i=0; i<len; i++)
58 outBuf[i] = (byte)(i % 256);
62 stream.Write(outBuf,0,len);
64 // and see if it comes back
65 byte[] inBuf = new Byte[len];
66 int ret = inSock.Receive(inBuf, 0, len, 0);
67 Assert.IsTrue (ret != 0);
69 for (int i=0; i<len; i++)
71 Assert.IsTrue (inBuf[i] == outBuf[i]);
82 #if FEATURE_NO_BSD_SOCKETS
83 [ExpectedException (typeof (PlatformNotSupportedException))]
85 public void CloseTest ()
87 var port = NetworkHelpers.FindFreePort ();
88 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, port);
89 using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
90 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
91 NetworkStream ns = tcpClient.GetStream ();
92 Assert.IsNotNull (ns, "#A1");
93 Assert.AreEqual (0, tcpClient.Available, "#A2");
94 Assert.IsTrue (tcpClient.Connected, "#A3");
95 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
97 Assert.IsNotNull (tcpClient.Client, "#A5");
99 int available = tcpClient.Available;
100 Assert.Fail ("#A6: " + available);
101 } catch (ObjectDisposedException) {
103 Assert.IsFalse (tcpClient.Connected, "#A7");
104 // not supported on linux
107 bool exclusive = tcpClient.ExclusiveAddressUse;
108 Assert.Fail ("#A8: " + exclusive);
109 } catch (ObjectDisposedException) {
114 using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
115 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
116 Assert.AreEqual (0, tcpClient.Available, "#B1");
117 Assert.IsTrue (tcpClient.Connected, "#B2");
118 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
120 Assert.IsNull (tcpClient.Client, "#B4");
122 int available = tcpClient.Available;
123 Assert.Fail ("#B5: " + available);
124 } catch (NullReferenceException) {
127 bool connected = tcpClient.Connected;
128 Assert.Fail ("#B6: " + connected);
129 } catch (NullReferenceException) {
131 // not supported on linux
134 bool exclusive = tcpClient.ExclusiveAddressUse;
135 Assert.Fail ("#B7: " + exclusive);
136 } catch (NullReferenceException) {
142 byte [] CloseRequestHandler (Socket socket)
148 #if FEATURE_NO_BSD_SOCKETS
149 [ExpectedException (typeof (PlatformNotSupportedException))]
151 [ExpectedException (typeof(ArgumentNullException))]
153 public void ConnectMultiNull ()
155 TcpClient client = new TcpClient ();
156 IPAddress[] ipAddresses = null;
158 client.Connect (ipAddresses, 1234);
162 #if FEATURE_NO_BSD_SOCKETS
163 [ExpectedException (typeof (PlatformNotSupportedException))]
165 public void ConnectMultiAny ()
167 TcpClient client = new TcpClient ();
168 IPAddress[] ipAddresses = new IPAddress[1];
170 ipAddresses[0] = IPAddress.Any;
173 client.Connect (ipAddresses, 1234);
174 Assert.Fail ("ConnectMultiAny #1");
175 } catch (SocketException ex) {
176 Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
178 Assert.Fail ("ConnectMultiAny #3");
183 #if FEATURE_NO_BSD_SOCKETS
184 [ExpectedException (typeof (PlatformNotSupportedException))]
186 public void ConnectMultiRefused ()
188 TcpClient client = new TcpClient ();
189 IPAddress[] ipAddresses = new IPAddress[1];
191 ipAddresses[0] = IPAddress.Loopback;
194 client.Connect (ipAddresses, 1234);
195 Assert.Fail ("ConnectMultiRefused #1");
196 } catch (SocketException ex) {
197 Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
199 Assert.Fail ("ConnectMultiRefused #3");