using System.Net.Sockets;
using NUnit.Framework;
+using MonoTests.Helpers;
+
namespace MonoTests.System.Net.Sockets
{
/// <summary>
/// Tests System.Net.Sockets.TcpClient
/// </summary>
[TestFixture]
+ [Category ("RequiresBSDSockets")]
public class TcpClientTest
{
Socket lSock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
- lSock.Bind(new IPEndPoint(IPAddress.Any, 8765));
+ var port = NetworkHelpers.FindFreePort ();
+ lSock.Bind(new IPEndPoint(IPAddress.Any, port));
lSock.Listen(-1);
// connect to it with a TcpClient
- TcpClient outClient = new TcpClient("localhost", 8765);
+ TcpClient outClient = new TcpClient("localhost", port);
Socket inSock = lSock.Accept();
// and see if it comes back
byte[] inBuf = new Byte[len];
int ret = inSock.Receive(inBuf, 0, len, 0);
- Assertion.Assert(ret != 0);
+ Assert.IsTrue (ret != 0);
for (int i=0; i<len; i++)
{
- Assertion.Assert(inBuf[i] == outBuf[i]);
+ Assert.IsTrue (inBuf[i] == outBuf[i]);
}
// tidy up
[Test] // bug #81105
public void CloseTest ()
{
- IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8765);
- using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
- sr.Start ();
-
- TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
+ var port = NetworkHelpers.FindFreePort ();
+ IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, port);
+ using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
+ TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
NetworkStream ns = tcpClient.GetStream ();
Assert.IsNotNull (ns, "#A1");
-#if NET_2_0
Assert.AreEqual (0, tcpClient.Available, "#A2");
Assert.IsTrue (tcpClient.Connected, "#A3");
// Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
-#endif
tcpClient.Close ();
-#if NET_2_0
Assert.IsNotNull (tcpClient.Client, "#A5");
try {
int available = tcpClient.Available;
} catch (ObjectDisposedException) {
}
*/
-#endif
}
- using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
- sr.Start ();
-
- TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
-#if NET_2_0
+ using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
+ TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
Assert.AreEqual (0, tcpClient.Available, "#B1");
Assert.IsTrue (tcpClient.Connected, "#B2");
// Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
-#endif
tcpClient.Close ();
-#if NET_2_0
Assert.IsNull (tcpClient.Client, "#B4");
try {
int available = tcpClient.Available;
} catch (NullReferenceException) {
}
*/
-#endif
}
}
return new byte [0];
}
-#if NET_2_0
[Test]
[ExpectedException (typeof(ArgumentNullException))]
public void ConnectMultiNull ()
client.Connect (ipAddresses, 1234);
Assert.Fail ("ConnectMultiAny #1");
} catch (SocketException ex) {
- Assertion.AssertEquals ("ConnectMultiAny #2",
- 10049, ex.ErrorCode);
+ Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
} catch {
Assert.Fail ("ConnectMultiAny #3");
}
client.Connect (ipAddresses, 1234);
Assert.Fail ("ConnectMultiRefused #1");
} catch (SocketException ex) {
- Assertion.AssertEquals ("ConnectMultiRefused #2", 10061, ex.ErrorCode);
+ Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
} catch {
Assert.Fail ("ConnectMultiRefused #3");
}
}
-#endif
}
}