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");
86 Assert.AreEqual (0, tcpClient.Available, "#A2");
87 Assert.IsTrue (tcpClient.Connected, "#A3");
88 // 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) {
110 using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
113 TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
115 Assert.AreEqual (0, tcpClient.Available, "#B1");
116 Assert.IsTrue (tcpClient.Connected, "#B2");
117 // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
121 Assert.IsNull (tcpClient.Client, "#B4");
123 int available = tcpClient.Available;
124 Assert.Fail ("#B5: " + available);
125 } catch (NullReferenceException) {
128 bool connected = tcpClient.Connected;
129 Assert.Fail ("#B6: " + connected);
130 } catch (NullReferenceException) {
132 // not supported on linux
135 bool exclusive = tcpClient.ExclusiveAddressUse;
136 Assert.Fail ("#B7: " + exclusive);
137 } catch (NullReferenceException) {
144 byte [] CloseRequestHandler (Socket socket)
151 [ExpectedException (typeof(ArgumentNullException))]
152 public void ConnectMultiNull ()
154 TcpClient client = new TcpClient ();
155 IPAddress[] ipAddresses = null;
157 client.Connect (ipAddresses, 1234);
161 public void ConnectMultiAny ()
163 TcpClient client = new TcpClient ();
164 IPAddress[] ipAddresses = new IPAddress[1];
166 ipAddresses[0] = IPAddress.Any;
169 client.Connect (ipAddresses, 1234);
170 Assert.Fail ("ConnectMultiAny #1");
171 } catch (SocketException ex) {
172 Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
174 Assert.Fail ("ConnectMultiAny #3");
179 public void ConnectMultiRefused ()
181 TcpClient client = new TcpClient ();
182 IPAddress[] ipAddresses = new IPAddress[1];
184 ipAddresses[0] = IPAddress.Loopback;
187 client.Connect (ipAddresses, 1234);
188 Assert.Fail ("ConnectMultiRefused #1");
189 } catch (SocketException ex) {
190 Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
192 Assert.Fail ("ConnectMultiRefused #3");