本文整理汇总了C#中Socket.SetKeepAlive方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.SetKeepAlive方法的具体用法?C# Socket.SetKeepAlive怎么用?C# Socket.SetKeepAlive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.SetKeepAlive方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateSocket
/// <summary>
/// 创建一个 <see cref="Socket"/>。
/// </summary>
/// <returns><see cref="Socket"/>。</returns>
public Socket CreateSocket()
{
var socket = new Socket(this._EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.NoDelay = true;
//socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, false);
if(this._KeepAliveMilliseconds > 0U)
{
socket.SetKeepAlive(this._KeepAliveMilliseconds);
}
return socket;
}
示例2: Connect
public virtual void Connect()
{
try
{
IPAddress = GetIPAddress(_dataSource, AddressFamily.InterNetwork);
IPEndPoint endPoint = new IPEndPoint(IPAddress, _portNumber);
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Set Receive Buffer size.
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, _packetSize);
// Set Send Buffer size.
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, _packetSize);
// Disables the Nagle algorithm for send coalescing.
_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
// Start sending keepalive packets every 30min after 30min of idle connection
_socket.SetKeepAlive(KeepAliveTime, KeepAliveInterval);
// Make the socket to connect to the Server
_socket.Connect(endPoint);
_networkStream = new NetworkStream(_socket, true);
}
catch (SocketException ex)
{
throw IscException.ForTypeErrorCodeStrParam(IscCodes.isc_arg_gds, IscCodes.isc_network_error, _dataSource, ex);
}
}