diff --git a/src/Ether.Network/Client/NetClient.cs b/src/Ether.Network/Client/NetClient.cs
index cf25db5..97f8bce 100644
--- a/src/Ether.Network/Client/NetClient.cs
+++ b/src/Ether.Network/Client/NetClient.cs
@@ -9,6 +9,7 @@
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
+using Ether.Network.Exceptions;
namespace Ether.Network.Client
{
@@ -73,6 +74,8 @@ public void Connect()
if (this.IsConnected)
throw new InvalidOperationException("Client is already connected to remote.");
+ this.CheckConfiguration();
+
this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this._socketSendArgs = NetUtils.CreateSocketAsync(this.Socket, this.IO_Completed);
this._socketReceiveArgs = NetUtils.CreateSocketAsync(this, this.IO_Completed, this.Configuration.BufferSize);
@@ -238,6 +241,21 @@ private void ProcessReceive(SocketAsyncEventArgs e)
}
}
+ ///
+ /// Checks the configuration.
+ ///
+ private void CheckConfiguration()
+ {
+ if (this.Configuration.Port <= 0)
+ throw new EtherConfigurationException($"{this.Configuration.Port} is not a valid port.");
+
+ if (this.Configuration.Address == null)
+ throw new EtherConfigurationException($"Invalid host : {this.Configuration.Host}.");
+
+ if (this.Configuration.BufferSize <= 0)
+ throw new EtherConfigurationException("BufferSize cannot less or equal to 0.");
+ }
+
///
/// Triggered when a async operation is completed.
///
diff --git a/src/Ether.Network/Server/NetServer.cs b/src/Ether.Network/Server/NetServer.cs
index ff2adcf..92259b2 100644
--- a/src/Ether.Network/Server/NetServer.cs
+++ b/src/Ether.Network/Server/NetServer.cs
@@ -74,12 +74,7 @@ public void Start()
if (this.IsRunning)
throw new InvalidOperationException("Server is already running.");
- if (this.Configuration.Port <= 0)
- throw new EtherConfigurationException($"{this.Configuration.Port} is not a valid port.");
-
- IPAddress address = this.Configuration.Host == AllInterfaces ? IPAddress.Any : this.Configuration.Address;
- if (address == null)
- throw new EtherConfigurationException($"Invalid host : {this.Configuration.Host}");
+ this.CheckConfiguration();
for (var i = 0; i < this.Configuration.MaximumNumberOfConnections; i++)
{
@@ -90,7 +85,7 @@ public void Start()
this.Initialize();
this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
- this.Socket.Bind(new IPEndPoint(address, this.Configuration.Port));
+ this.Socket.Bind(NetUtils.CreateIpEndPoint(this.Configuration.Host, this.Configuration.Port));
this.Socket.Listen(this.Configuration.Backlog);
this.IsRunning = true;
this.StartAccept(NetUtils.CreateSocketAsync(null, this.IO_Completed));
@@ -358,6 +353,25 @@ private void ClearResources()
this._messageQueue.Clear();
}
+ ///
+ /// Checks the configuration.
+ ///
+ private void CheckConfiguration()
+ {
+ if (this.Configuration.Port <= 0)
+ throw new EtherConfigurationException($"{this.Configuration.Port} is not a valid port.");
+
+ IPAddress address = this.Configuration.Host == AllInterfaces ? IPAddress.Any : this.Configuration.Address;
+ if (address == null)
+ throw new EtherConfigurationException($"Invalid host : {this.Configuration.Host}.");
+
+ if (this.Configuration.BufferSize <= 0)
+ throw new EtherConfigurationException("BufferSize cannot less or equal to 0.");
+
+ if (this.Configuration.Backlog <= 0)
+ throw new EtherConfigurationException("Backlog cannot be less or equal to 0.");
+ }
+
///
/// Triggered when a async operation is completed.
///
diff --git a/test/Ether.Network.Tests/Contexts/NetConfig/ConfigClient.cs b/test/Ether.Network.Tests/Contexts/NetConfig/ConfigClient.cs
new file mode 100644
index 0000000..62dfaee
--- /dev/null
+++ b/test/Ether.Network.Tests/Contexts/NetConfig/ConfigClient.cs
@@ -0,0 +1,27 @@
+using System.Net.Sockets;
+using Ether.Network.Client;
+
+namespace Ether.Network.Tests.Contexts.NetConfig
+{
+ public class ConfigClient : NetClient
+ {
+ protected override void OnConnected()
+ {
+ }
+
+ protected override void OnDisconnected()
+ {
+ }
+
+ protected override void OnSocketError(SocketError socketError)
+ {
+ }
+
+ public void SetupConfiguration()
+ {
+ this.Configuration.Host = "127.0.0.1";
+ this.Configuration.Port = 4445;
+ this.Configuration.BufferSize = 512;
+ }
+ }
+}
diff --git a/test/Ether.Network.Tests/Contexts/NetConfig/ConfigServer.cs b/test/Ether.Network.Tests/Contexts/NetConfig/ConfigServer.cs
index dab52ea..e7e929a 100644
--- a/test/Ether.Network.Tests/Contexts/NetConfig/ConfigServer.cs
+++ b/test/Ether.Network.Tests/Contexts/NetConfig/ConfigServer.cs
@@ -1,6 +1,4 @@
using System;
-using System.Collections.Generic;
-using System.Text;
using Ether.Network.Common;
using Ether.Network.Server;
diff --git a/test/Ether.Network.Tests/NetClientConfigurationTest.cs b/test/Ether.Network.Tests/NetClientConfigurationTest.cs
new file mode 100644
index 0000000..63e3374
--- /dev/null
+++ b/test/Ether.Network.Tests/NetClientConfigurationTest.cs
@@ -0,0 +1,66 @@
+using System;
+using Ether.Network.Exceptions;
+using Ether.Network.Tests.Contexts.NetConfig;
+using Xunit;
+
+namespace Ether.Network.Tests
+{
+ public class NetClientConfigurationTest
+ {
+ [Fact]
+ public void StartClientWihtoutConfiguration()
+ {
+ using (var server = new ConfigServer())
+ {
+ server.SetupConfiguration();
+ server.Start();
+
+ using (var client = new ConfigClient())
+ {
+ Exception ex = Assert.Throws(() => client.Connect());
+ Assert.IsType(ex);
+
+ client.Disconnect();
+ }
+ }
+ }
+
+ [Fact]
+ public void StartClientWithConfiguration()
+ {
+ using (var server = new ConfigServer())
+ {
+ server.SetupConfiguration();
+ server.Start();
+
+ using (var client = new ConfigClient())
+ {
+ client.SetupConfiguration();
+ client.Connect();
+ client.Disconnect();
+ }
+ }
+ }
+
+ [Fact]
+ public void SetupClientConfigurationAfterConnected()
+ {
+ using (var server = new ConfigServer())
+ {
+ server.SetupConfiguration();
+ server.Start();
+
+ using (var client = new ConfigClient())
+ {
+ client.SetupConfiguration();
+ client.Connect();
+
+ Exception ex = Assert.Throws(() => client.SetupConfiguration());
+ Assert.IsType(ex);
+
+ client.Disconnect();
+ }
+ }
+ }
+ }
+}
\ No newline at end of file