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 public void TcpClient()
34 // set up a listening Socket
35 Socket lSock = new Socket(AddressFamily.InterNetwork,
36 SocketType.Stream, ProtocolType.Tcp);
38 var port = NetworkHelpers.FindFreePort ();
39 lSock.Bind(new IPEndPoint(IPAddress.Any, port));
43 // connect to it with a TcpClient
44 TcpClient outClient = new TcpClient("localhost", port);
45 Socket inSock = lSock.Accept();
48 // now try exchanging data
49 NetworkStream stream = outClient.GetStream();
52 byte[] outBuf = new Byte[len];
53 for (int i=0; i<len; i++)
55 outBuf[i] = (byte)(i % 256);
59 stream.Write(outBuf,0,len);
61 // and see if it comes back
62 byte[] inBuf = new Byte[len];
63 int ret = inSock.Receive(inBuf, 0, len, 0);
64 Assert.IsTrue (ret != 0);
66 for (int i=0; i<len; i++)
68 Assert.IsTrue (inBuf[i] == outBuf[i]);
79 public void CloseTest ()
81 var port = NetworkHelpers.FindFreePort ();
82 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, port);
83 using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
84 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
85 NetworkStream ns = tcpClient.GetStream ();
86 Assert.IsNotNull (ns, "#A1");
87 Assert.AreEqual (0, tcpClient.Available, "#A2");
88 Assert.IsTrue (tcpClient.Connected, "#A3");
89 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
91 Assert.IsNotNull (tcpClient.Client, "#A5");
93 int available = tcpClient.Available;
94 Assert.Fail ("#A6: " + available);
95 } catch (ObjectDisposedException) {
97 Assert.IsFalse (tcpClient.Connected, "#A7");
98 // not supported on linux
101 bool exclusive = tcpClient.ExclusiveAddressUse;
102 Assert.Fail ("#A8: " + exclusive);
103 } catch (ObjectDisposedException) {
108 using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
109 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
110 Assert.AreEqual (0, tcpClient.Available, "#B1");
111 Assert.IsTrue (tcpClient.Connected, "#B2");
112 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
114 Assert.IsNull (tcpClient.Client, "#B4");
116 int available = tcpClient.Available;
117 Assert.Fail ("#B5: " + available);
118 } catch (NullReferenceException) {
121 bool connected = tcpClient.Connected;
122 Assert.Fail ("#B6: " + connected);
123 } catch (NullReferenceException) {
125 // not supported on linux
128 bool exclusive = tcpClient.ExclusiveAddressUse;
129 Assert.Fail ("#B7: " + exclusive);
130 } catch (NullReferenceException) {
136 byte [] CloseRequestHandler (Socket socket)
142 [ExpectedException (typeof(ArgumentNullException))]
143 public void ConnectMultiNull ()
145 TcpClient client = new TcpClient ();
146 IPAddress[] ipAddresses = null;
148 client.Connect (ipAddresses, 1234);
152 public void ConnectMultiAny ()
154 TcpClient client = new TcpClient ();
155 IPAddress[] ipAddresses = new IPAddress[1];
157 ipAddresses[0] = IPAddress.Any;
160 client.Connect (ipAddresses, 1234);
161 Assert.Fail ("ConnectMultiAny #1");
162 } catch (SocketException ex) {
163 Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
165 Assert.Fail ("ConnectMultiAny #3");
170 public void ConnectMultiRefused ()
172 TcpClient client = new TcpClient ();
173 IPAddress[] ipAddresses = new IPAddress[1];
175 ipAddresses[0] = IPAddress.Loopback;
178 client.Connect (ipAddresses, 1234);
179 Assert.Fail ("ConnectMultiRefused #1");
180 } catch (SocketException ex) {
181 Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
183 Assert.Fail ("ConnectMultiRefused #3");