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 lSock.Bind(new IPEndPoint(IPAddress.Any, 8765));
42 // connect to it with a TcpClient
43 TcpClient outClient = new TcpClient("localhost", 8765);
44 Socket inSock = lSock.Accept();
47 // now try exchanging data
48 NetworkStream stream = outClient.GetStream();
51 byte[] outBuf = new Byte[len];
52 for (int i=0; i<len; i++)
54 outBuf[i] = (byte)(i % 256);
58 stream.Write(outBuf,0,len);
60 // and see if it comes back
61 byte[] inBuf = new Byte[len];
62 int ret = inSock.Receive(inBuf, 0, len, 0);
63 Assert.IsTrue (ret != 0);
65 for (int i=0; i<len; i++)
67 Assert.IsTrue (inBuf[i] == outBuf[i]);
78 public void CloseTest ()
80 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8765);
81 using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
84 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
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, new SocketRequestHandler (CloseRequestHandler))) {
111 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
112 Assert.AreEqual (0, tcpClient.Available, "#B1");
113 Assert.IsTrue (tcpClient.Connected, "#B2");
114 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
116 Assert.IsNull (tcpClient.Client, "#B4");
118 int available = tcpClient.Available;
119 Assert.Fail ("#B5: " + available);
120 } catch (NullReferenceException) {
123 bool connected = tcpClient.Connected;
124 Assert.Fail ("#B6: " + connected);
125 } catch (NullReferenceException) {
127 // not supported on linux
130 bool exclusive = tcpClient.ExclusiveAddressUse;
131 Assert.Fail ("#B7: " + exclusive);
132 } catch (NullReferenceException) {
138 byte [] CloseRequestHandler (Socket socket)
144 [ExpectedException (typeof(ArgumentNullException))]
145 public void ConnectMultiNull ()
147 TcpClient client = new TcpClient ();
148 IPAddress[] ipAddresses = null;
150 client.Connect (ipAddresses, 1234);
154 public void ConnectMultiAny ()
156 TcpClient client = new TcpClient ();
157 IPAddress[] ipAddresses = new IPAddress[1];
159 ipAddresses[0] = IPAddress.Any;
162 client.Connect (ipAddresses, 1234);
163 Assert.Fail ("ConnectMultiAny #1");
164 } catch (SocketException ex) {
165 Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
167 Assert.Fail ("ConnectMultiAny #3");
172 public void ConnectMultiRefused ()
174 TcpClient client = new TcpClient ();
175 IPAddress[] ipAddresses = new IPAddress[1];
177 ipAddresses[0] = IPAddress.Loopback;
180 client.Connect (ipAddresses, 1234);
181 Assert.Fail ("ConnectMultiRefused #1");
182 } catch (SocketException ex) {
183 Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
185 Assert.Fail ("ConnectMultiRefused #3");