System.Net.Sockets.TcpClient.EndConnect C# (CSharp) Метод

EndConnect() публичный метод

public EndConnect ( IAsyncResult asyncResult ) : void
asyncResult IAsyncResult
Результат void
        public void EndConnect(IAsyncResult asyncResult)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, asyncResult);

            Socket s = Client;
            if (s == null)
            {
                // Dispose nulls out the client socket field.
                throw new ObjectDisposedException(GetType().Name);
            }

            EndConnectCore(s, asyncResult);

            _active = true;

            if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
        }

Usage Example

Пример #1
0
        public static bool Ping(string host, int port, TimeSpan timeout, out TimeSpan elapsed)
        {
            using (TcpClient tcp = new TcpClient())
            {
                DateTime start = DateTime.Now;
                IAsyncResult result = tcp.BeginConnect(host, port, null, null);
                WaitHandle wait = result.AsyncWaitHandle;
                bool ok = true;

                try
                {
                    if (!result.AsyncWaitHandle.WaitOne(timeout, false))
                    {
                        tcp.Close();
                        ok = false;
                    }

                    tcp.EndConnect(result);
                }
                catch
                {
                    ok = false;
                }
                finally
                {
                    wait.Close();
                }

                DateTime stop = DateTime.Now;
                elapsed = stop.Subtract(start);
                return ok;
            }
        }
All Usage Examples Of System.Net.Sockets.TcpClient::EndConnect