当前位置: 首页>>代码示例>>C#>>正文


C# TcpClient.SetSocketKeepAliveValues方法代码示例

本文整理汇总了C#中System.Net.Sockets.TcpClient.SetSocketKeepAliveValues方法的典型用法代码示例。如果您正苦于以下问题:C# TcpClient.SetSocketKeepAliveValues方法的具体用法?C# TcpClient.SetSocketKeepAliveValues怎么用?C# TcpClient.SetSocketKeepAliveValues使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.TcpClient的用法示例。


在下文中一共展示了TcpClient.SetSocketKeepAliveValues方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: connect

        void connect()
        {
            client = new TcpClient();

            //Notify we are connecting
            var eoc = this.OnConnecting;
            if (eoc != null)
                eoc(this.appleSettings.Host, this.appleSettings.Port);

            try
            {
                var connectDone = new AutoResetEvent(false);

                //Connect async so we can utilize a connection timeout
                connectAsyncResult = client.BeginConnect(
                    appleSettings.Host, appleSettings.Port,
                    new AsyncCallback(
                        delegate(IAsyncResult ar)
                        {
                            if (connectAsyncResult != ar)
                                return;

                            try
                            {
                                client.EndConnect(ar);

                                //Set keep alive on the socket may help maintain our APNS connection
                                //client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

                                //Really not sure if this will work on MONO....
                                try { client.SetSocketKeepAliveValues(20 * 60 * 1000, 30 * 1000); }
                                catch { }

                                Interlocked.Increment(ref reconnects);

                                //Trigger the reset event so we can continue execution below
                                connectDone.Set();
                            }
                            catch (Exception ex)
                            {
                                Log.Error("APNS Connect Callback Failed: " + ex);
                            }
                        }
                    ), client
                );

                if (!connectDone.WaitOne(appleSettings.ConnectionTimeout))
                {
                    throw new TimeoutException("Connection to Host Timed Out!");
                }
            }
            catch (Exception ex)
            {
                throw new ConnectionFailureException("Connection to Host Failed", ex);
            }

            if (appleSettings.SkipSsl)
            {
                networkStream = client.GetStream();
            }
            else
            {
                RemoteCertificateValidationCallback userCertificateValidation;

                if (appleSettings != null && appleSettings.ValidateServerCertificate)
                {
                    userCertificateValidation = ValidateRemoteCertificate;
                }
                else
                {
                    userCertificateValidation = (sender, cert, chain, sslPolicyErrors) => true; //Don't validate remote cert
                }

                stream = new SslStream(client.GetStream(), false,
                    userCertificateValidation,
                    (sender, targetHost, localCerts, remoteCert, acceptableIssuers) => certificate);

                try
                {
                    stream.AuthenticateAsClient(this.appleSettings.Host, this.certificates, System.Security.Authentication.SslProtocols.Ssl3, false);
                    //stream.AuthenticateAsClient(this.appleSettings.Host);
                }
                catch (System.Security.Authentication.AuthenticationException ex)
                {
                    throw new ConnectionFailureException("SSL Stream Failed to Authenticate as Client", ex);
                }

                if (!stream.IsMutuallyAuthenticated)
                    throw new ConnectionFailureException("SSL Stream Failed to Authenticate", null);

                if (!stream.CanWrite)
                    throw new ConnectionFailureException("SSL Stream is not Writable", null);

                networkStream = stream;
            }

            //Start reading from the stream asynchronously
            Reader();
        }
开发者ID:vniu,项目名称:PushSharp,代码行数:99,代码来源:ApplePushChannel.cs

示例2: Connect

		private void Connect()
		{
			if (_client != null)
			{
				Disconnect();
			}

			Log.Debug("Starting Connect with instance {0}", _channelInstanceId);
			_client = new TcpClient();

			var onConnecting = OnConnecting;
			if (onConnecting != null)
			{
				onConnecting(_appleSettings.Host, _appleSettings.Port);
			}

			try
			{
				var connectResult = _client.BeginConnect(_appleSettings.Host, _appleSettings.Port, null, null);
				if (!connectResult.AsyncWaitHandle.WaitOne(_appleSettings.ConnectionTimeout))
				{
					Disconnect();
					throw new TimeoutException("Timeout reached while attempting to connect.");
				}

				_client.EndConnect(connectResult);
				_client.SetSocketKeepAliveValues(
					(int)_appleSettings.KeepAlivePeriod.TotalMilliseconds,
					(int)_appleSettings.KeepAliveRetryPeriod.TotalMilliseconds);

				Interlocked.Increment(ref _reconnects);
			}
			catch (Exception exception)
			{
				throw new ConnectionFailureException("Connection to APNS host failed", exception);
			}

			SecureConnection();

			StartReader();
		}
开发者ID:greg84,项目名称:PushSharp,代码行数:41,代码来源:ApplePushChannel.cs


注:本文中的System.Net.Sockets.TcpClient.SetSocketKeepAliveValues方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。