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 [Category ("RequiresBSDSockets")]
25 public class TcpClientTest
29 /// Tests the TcpClient object
30 /// (from System.Net.Sockets)
33 public void TcpClient()
35 // set up a listening Socket
36 Socket lSock = new Socket(AddressFamily.InterNetwork,
37 SocketType.Stream, ProtocolType.Tcp);
39 var port = NetworkHelpers.FindFreePort ();
40 lSock.Bind(new IPEndPoint(IPAddress.Any, port));
44 // connect to it with a TcpClient
45 TcpClient outClient = new TcpClient("localhost", port);
46 Socket inSock = lSock.Accept();
49 // now try exchanging data
50 NetworkStream stream = outClient.GetStream();
53 byte[] outBuf = new Byte[len];
54 for (int i=0; i<len; i++)
56 outBuf[i] = (byte)(i % 256);
60 stream.Write(outBuf,0,len);
62 // and see if it comes back
63 byte[] inBuf = new Byte[len];
64 int ret = inSock.Receive(inBuf, 0, len, 0);
65 Assert.IsTrue (ret != 0);
67 for (int i=0; i<len; i++)
69 Assert.IsTrue (inBuf[i] == outBuf[i]);
80 public void CloseTest ()
82 var port = NetworkHelpers.FindFreePort ();
83 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, port);
84 using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
85 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
86 NetworkStream ns = tcpClient.GetStream ();
87 Assert.IsNotNull (ns, "#A1");
88 Assert.AreEqual (0, tcpClient.Available, "#A2");
89 Assert.IsTrue (tcpClient.Connected, "#A3");
90 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
92 Assert.IsNotNull (tcpClient.Client, "#A5");
94 int available = tcpClient.Available;
95 Assert.Fail ("#A6: " + available);
96 } catch (ObjectDisposedException) {
98 Assert.IsFalse (tcpClient.Connected, "#A7");
99 // not supported on linux
102 bool exclusive = tcpClient.ExclusiveAddressUse;
103 Assert.Fail ("#A8: " + exclusive);
104 } catch (ObjectDisposedException) {
109 using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
110 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
111 Assert.AreEqual (0, tcpClient.Available, "#B1");
112 Assert.IsTrue (tcpClient.Connected, "#B2");
113 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
115 Assert.IsNull (tcpClient.Client, "#B4");
117 int available = tcpClient.Available;
118 Assert.Fail ("#B5: " + available);
119 } catch (NullReferenceException) {
122 bool connected = tcpClient.Connected;
123 Assert.Fail ("#B6: " + connected);
124 } catch (NullReferenceException) {
126 // not supported on linux
129 bool exclusive = tcpClient.ExclusiveAddressUse;
130 Assert.Fail ("#B7: " + exclusive);
131 } catch (NullReferenceException) {
137 byte [] CloseRequestHandler (Socket socket)
143 [ExpectedException (typeof(ArgumentNullException))]
144 public void ConnectMultiNull ()
146 TcpClient client = new TcpClient ();
147 IPAddress[] ipAddresses = null;
149 client.Connect (ipAddresses, 1234);
153 public void ConnectMultiAny ()
155 TcpClient client = new TcpClient ();
156 IPAddress[] ipAddresses = new IPAddress[1];
158 ipAddresses[0] = IPAddress.Any;
161 client.Connect (ipAddresses, 1234);
162 Assert.Fail ("ConnectMultiAny #1");
163 } catch (SocketException ex) {
164 Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
166 Assert.Fail ("ConnectMultiAny #3");
171 public void ConnectMultiRefused ()
173 TcpClient client = new TcpClient ();
174 IPAddress[] ipAddresses = new IPAddress[1];
176 ipAddresses[0] = IPAddress.Loopback;
179 client.Connect (ipAddresses, 1234);
180 Assert.Fail ("ConnectMultiRefused #1");
181 } catch (SocketException ex) {
182 Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
184 Assert.Fail ("ConnectMultiRefused #3");