本文整理汇总了C#中Socket.?.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.?.Dispose方法的具体用法?C# Socket.?.Dispose怎么用?C# Socket.?.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.?.Dispose方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendTimeRequest
partial void SendTimeRequest()
{
try
{
byte[] buffer = new byte[48];
buffer[0] = 0x1B;
DnsEndPoint _endPoint = new DnsEndPoint(_ServerAddress, 123, AddressFamily.InterNetwork);
Socket socket = null;
var socketArgs = new SocketAsyncEventArgs() { RemoteEndPoint = _endPoint };
try
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
socketArgs.Completed += Socket_Completed_SendAgain;
//TFW - For some reason 'ConnectAsync' reports an error
//desktop .Net 4, but 'Connect' works fine. On WP
//only ConnectAsync is available, and it appears to work.
#if USE_CONNECTASYNC
socketArgs.SetBuffer(buffer, 0, buffer.Length);
if (!socket.ConnectAsync(socketArgs))
Socket_Completed_SendAgain(socket, socketArgs);
#else
socket.Connect(socketArgs.RemoteEndPoint);
socketArgs.SetBuffer(buffer, 0, buffer.Length);
if (!socket.SendAsync(socketArgs))
Socket_Completed_SendAgain(socket, socketArgs);
#endif
}
catch
{
socketArgs.Completed -= this.Socket_Completed_SendAgain;
throw;
}
}
catch
{
socket?.Dispose();
throw;
}
}
catch (SocketException se)
{
OnErrorOccurred(new NtpNetworkException(se.Message, (int)se.SocketErrorCode, se));
}
catch (Exception ex)
{
OnErrorOccurred(new NtpNetworkException(ex.Message, -1, ex));
}
}
示例2: ValidateProxy
public static bool ValidateProxy(HttpHost p)
{
if (_localAddr == null)
{
//Logger.Error("cannot get local ip");
return false;
}
bool isReachable = false;
Socket socket = null;
try
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4);
socket.Bind(new IPEndPoint(_localAddr, 0));
IPAddress address = IPAddress.Parse(p.Host);
socket.SendTimeout = 3000;
socket.ReceiveTimeout = 3000;
socket.Connect(address, p.Port);
//Logger.Info("SUCCESS - connection established! Local: " + _localAddr + " remote: " + p);
isReachable = true;
}
catch (IOException)
{
//Logger.Warn("FAILRE - CAN not connect! Local: " + _localAddr + " remote: " + p);
}
finally
{
try
{
#if !NET_CORE
socket?.Close();
#else
socket?.Dispose();
#endif
}
catch (IOException e)
{
//Logger.Warn("Error occurred while closing socket of validating proxy", e);
}
}
return isReachable;
}
示例3: OnErrorOccurredAndDisposeSocket
private void OnErrorOccurredAndDisposeSocket(Exception exception, Socket socket)
{
try
{
OnErrorOccurred(exception);
}
finally
{
socket?.Dispose();
}
}
示例4: ConnectAsync
/// <summary>
/// Asynchronously connects to Rserve identified by an IP address.
/// </summary>
/// <param name="addr">
/// Address of the Rserve server, or nothing for localhost
/// </param>
/// <param name="port">
/// Port on which the server listens
/// </param>
/// <param name="credentials">
/// Credentials for authentication or <c>null</c> for anonymous
/// </param>
public static async Task<RConnection> ConnectAsync(IPAddress addr = null, int port = 6311, NetworkCredential credentials = null)
{
var ipe = new IPEndPoint(addr ?? new IPAddress(new byte[] { 127, 0, 0, 1 }), port);
Socket socket = null;
try
{
socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
// Connect with a one-second timeout
// From http://stackoverflow.com/questions/1062035/how-to-config-socket-connect-timeout-in-c-sharp
var connectTask = socket.ConnectAsync(ipe);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(1));
var completedTask = await Task.WhenAny(connectTask, timeoutTask)
.ContinueContextFree();
if (completedTask == timeoutTask)
{
connectTask.IgnoreFault();
await timeoutTask.ContinueContextFree();
throw new SocketException((int)SocketError.TimedOut);
}
await connectTask.ContinueContextFree();
if (!socket.Connected)
return null;
var connection = new RConnection(ref socket);
await connection.InitAsync(credentials?.UserName, credentials?.Password).ContinueContextFree();
return connection;
}
finally
{
socket?.Dispose();
}
}