本文整理汇总了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();
}
示例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();
}