diff --git a/samples/SampleClient/MyClient.cs b/samples/SampleClient/MyClient.cs index df0edb6..5f80400 100644 --- a/samples/SampleClient/MyClient.cs +++ b/samples/SampleClient/MyClient.cs @@ -13,11 +13,12 @@ internal sealed class MyClient : NetClient /// /// /// - public MyClient(string host, int port, int bufferSize) + public MyClient(string host, int port, int bufferSize, int timeOut) { this.Configuration.Host = host; this.Configuration.Port = port; this.Configuration.BufferSize = bufferSize; + this.Configuration.TimeOut = timeOut; } /// diff --git a/samples/SampleClient/Program.cs b/samples/SampleClient/Program.cs index 554f336..91dbbe7 100644 --- a/samples/SampleClient/Program.cs +++ b/samples/SampleClient/Program.cs @@ -9,9 +9,16 @@ internal class Program { private static void Main() { - var client = new MyClient("127.0.0.1", 4444, 512); + var client = new MyClient("127.0.0.1", 4444, 512, 5000); client.Connect(); + if (!client.IsConnected) + { + Console.WriteLine("Can't connect to server!"); + Console.ReadLine(); + return; + } + Console.WriteLine("Enter a message and press enter..."); int i = 0; var random = new Random(); diff --git a/src/Ether.Network/Client/NetClient.cs b/src/Ether.Network/Client/NetClient.cs index 97f8bce..344fcac 100644 --- a/src/Ether.Network/Client/NetClient.cs +++ b/src/Ether.Network/Client/NetClient.cs @@ -84,12 +84,15 @@ public void Connect() connectSocket.RemoteEndPoint = NetUtils.CreateIpEndPoint(this.Configuration.Host, this.Configuration.Port); if (this.Socket.ConnectAsync(connectSocket)) - this._autoConnectEvent.WaitOne(); + this._autoConnectEvent.WaitOne(Configuration.TimeOut); SocketError errorCode = connectSocket.SocketError; - if (errorCode != SocketError.Success) - throw new SocketException((int) errorCode); + if (!IsConnected) + { + this.OnSocketError(errorCode); + return; + } this._sendingQueueWorker.Start(); this._receivingQueueWorker.Start(); diff --git a/src/Ether.Network/Client/NetClientConfiguration.cs b/src/Ether.Network/Client/NetClientConfiguration.cs index f786fbe..7c132d2 100644 --- a/src/Ether.Network/Client/NetClientConfiguration.cs +++ b/src/Ether.Network/Client/NetClientConfiguration.cs @@ -13,6 +13,7 @@ public sealed class NetClientConfiguration private int _port; private int _bufferSize; private string _host; + private int _timeOut; /// /// Gets or sets the port. @@ -41,6 +42,15 @@ public string Host set => this.SetValue(ref this._host, value); } + /// + /// Gets or sets the connecting time out. + /// + public int TimeOut + { + get => this._timeOut; + set => this.SetValue(ref this._timeOut, value); + } + /// /// Gets the listening address. /// @@ -56,6 +66,7 @@ internal NetClientConfiguration(INetClient client) this._bufferSize = 1024; this._port = 0; this._host = null; + this._timeOut = 5000; } ///