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 namespace MonoTests.System.Net.Sockets
19 /// Tests System.Net.Sockets.TcpClient
22 public class TcpClientTest
26 /// Tests the TcpClient object
27 /// (from System.Net.Sockets)
30 public void TcpClient()
32 // set up a listening Socket
33 Socket lSock = new Socket(AddressFamily.InterNetwork,
34 SocketType.Stream, ProtocolType.Tcp);
36 lSock.Bind(new IPEndPoint(IPAddress.Any, 8765));
40 // connect to it with a TcpClient
41 TcpClient outClient = new TcpClient("localhost", 8765);
42 Socket inSock = lSock.Accept();
45 // now try exchanging data
46 NetworkStream stream = outClient.GetStream();
49 byte[] outBuf = new Byte[len];
50 for (int i=0; i<len; i++)
52 outBuf[i] = (byte)(i % 256);
56 stream.Write(outBuf,0,len);
58 // and see if it comes back
59 byte[] inBuf = new Byte[len];
60 int ret = inSock.Receive(inBuf, 0, len, 0);
61 Assert.IsTrue (ret != 0);
63 for (int i=0; i<len; i++)
65 Assert.IsTrue (inBuf[i] == outBuf[i]);
76 public void CloseTest ()
78 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8765);
79 using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
82 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
83 NetworkStream ns = tcpClient.GetStream ();
84 Assert.IsNotNull (ns, "#A1");
85 Assert.AreEqual (0, tcpClient.Available, "#A2");
86 Assert.IsTrue (tcpClient.Connected, "#A3");
87 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
89 Assert.IsNotNull (tcpClient.Client, "#A5");
91 int available = tcpClient.Available;
92 Assert.Fail ("#A6: " + available);
93 } catch (ObjectDisposedException) {
95 Assert.IsFalse (tcpClient.Connected, "#A7");
96 // not supported on linux
99 bool exclusive = tcpClient.ExclusiveAddressUse;
100 Assert.Fail ("#A8: " + exclusive);
101 } catch (ObjectDisposedException) {
106 using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
109 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
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");