本文整理汇总了C#中System.Net.Sockets.SocketAsyncEventArgs.StartOperationConnect方法的典型用法代码示例。如果您正苦于以下问题:C# SocketAsyncEventArgs.StartOperationConnect方法的具体用法?C# SocketAsyncEventArgs.StartOperationConnect怎么用?C# SocketAsyncEventArgs.StartOperationConnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.SocketAsyncEventArgs
的用法示例。
在下文中一共展示了SocketAsyncEventArgs.StartOperationConnect方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConnectAsync
/// <summary>
/// Begins an asynchronous request for a connection to a remote host.
/// </summary>
///
/// <returns>
/// Returns true if the I/O operation is pending. The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will be raised upon completion of the operation. Returns false if the I/O operation completed synchronously. In this case, The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will not be raised and the <paramref name="e"/> object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.
/// </returns>
/// <param name="e">The <see cref="T:System.Net.Sockets.SocketAsyncEventArgs"/> object to use for this asynchronous socket operation.</param><exception cref="T:System.ArgumentException">An argument is not valid. This exception occurs if multiple buffers are specified, the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.BufferList"/> property is not null. </exception><exception cref="T:System.ArgumentNullException">The <paramref name="e"/> parameter cannot be null and the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.RemoteEndPoint"/> cannot be null.</exception><exception cref="T:System.InvalidOperationException">The <see cref="T:System.Net.Sockets.Socket"/> is listening or a socket operation was already in progress using the <see cref="T:System.Net.Sockets.SocketAsyncEventArgs"/> object specified in the <paramref name="e"/> parameter.</exception><exception cref="T:System.Net.Sockets.SocketException">An error occurred when attempting to access the socket. See the Remarks section for more information.</exception><exception cref="T:System.NotSupportedException">Windows XP or later is required for this method. This exception also occurs if the local endpoint and the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.RemoteEndPoint"/> are not the same address family.</exception><exception cref="T:System.ObjectDisposedException">The <see cref="T:System.Net.Sockets.Socket"/> has been closed. </exception><exception cref="T:System.Security.SecurityException">A caller higher in the call stack does not have permission for the requested operation.</exception>
public bool ConnectAsync(SocketAsyncEventArgs e)
{
if (Socket.s_LoggingEnabled)
Logging.Enter(Logging.Sockets, (object) this, "ConnectAsync", "");
if (this.CleanedUp)
throw new ObjectDisposedException(this.GetType().FullName);
if (e.m_BufferList != null)
throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList");
if (e.RemoteEndPoint == null)
throw new ArgumentNullException("remoteEP");
if (this.isListening)
throw new InvalidOperationException(SR.GetString("net_sockets_mustnotlisten"));
EndPoint remoteEndPoint = e.RemoteEndPoint;
DnsEndPoint endPoint1 = remoteEndPoint as DnsEndPoint;
bool flag;
if (endPoint1 != null)
{
if (Socket.s_LoggingEnabled)
Logging.PrintInfo(Logging.Sockets, "Socket#" + ValidationHelper.HashString((object) this) + "::ConnectAsync " + SR.GetString("net_log_socket_connect_dnsendpoint"));
if (endPoint1.AddressFamily != AddressFamily.Unspecified && !this.CanTryAddressFamily(endPoint1.AddressFamily))
throw new NotSupportedException(SR.GetString("net_invalidversion"));
MultipleConnectAsync args = (MultipleConnectAsync) new SingleSocketMultipleConnectAsync(this, true);
e.StartOperationCommon(this);
e.StartOperationWrapperConnect(args);
flag = args.StartConnectAsync(e, endPoint1);
}
else
{
if (!this.CanTryAddressFamily(e.RemoteEndPoint.AddressFamily))
throw new NotSupportedException(SR.GetString("net_invalidversion"));
e.m_SocketAddress = this.CheckCacheRemote(ref remoteEndPoint, false);
if (this.m_RightEndPoint == null)
{
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork)
this.InternalBind((EndPoint) new IPEndPoint(IPAddress.Any, 0));
else
this.InternalBind((EndPoint) new IPEndPoint(IPAddress.IPv6Any, 0));
}
EndPoint endPoint2 = this.m_RightEndPoint;
if (this.m_RightEndPoint == null)
this.m_RightEndPoint = remoteEndPoint;
e.StartOperationCommon(this);
e.StartOperationConnect();
this.BindToCompletionPort();
SocketError socketError = SocketError.Success;
int bytesSent;
try
{
if (!this.ConnectEx(this.m_Handle, e.m_PtrSocketAddressBuffer, e.m_SocketAddress.m_Size, e.m_PtrSingleBuffer, e.Count, out bytesSent, (SafeHandle) e.m_PtrNativeOverlapped))
socketError = (SocketError) Marshal.GetLastWin32Error();
}
catch (Exception ex)
{
this.m_RightEndPoint = endPoint2;
e.Complete();
throw ex;
}
if (socketError != SocketError.Success && socketError != SocketError.IOPending)
{
e.FinishOperationSyncFailure(socketError, bytesSent, SocketFlags.None);
flag = false;
}
else
flag = true;
}
if (Socket.s_LoggingEnabled)
Logging.Exit(Logging.Sockets, (object) this, "ConnectAsync", (object) (bool) (flag ? 1 : 0));
return flag;
}
示例2: ConnectAsync
public bool ConnectAsync(SocketAsyncEventArgs e)
{
bool retval;
if (s_loggingEnabled)
{
Logging.Enter(Logging.Sockets, this, "ConnectAsync", "");
}
if (CleanedUp)
{
throw new ObjectDisposedException(GetType().FullName);
}
if (e == null)
{
throw new ArgumentNullException("e");
}
if (e._bufferList != null)
{
throw new ArgumentException(SR.net_multibuffernotsupported, "BufferList");
}
if (e.RemoteEndPoint == null)
{
throw new ArgumentNullException("remoteEP");
}
if (_isListening)
{
throw new InvalidOperationException(SR.net_sockets_mustnotlisten);
}
// Check permissions for connect and prepare SocketAddress.
EndPoint endPointSnapshot = e.RemoteEndPoint;
DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint;
if (dnsEP != null)
{
if (s_loggingEnabled)
{
Logging.PrintInfo(Logging.Sockets, "Socket#" + Logging.HashString(this) + "::ConnectAsync " + SR.net_log_socket_connect_dnsendpoint);
}
if (dnsEP.AddressFamily != AddressFamily.Unspecified && !CanTryAddressFamily(dnsEP.AddressFamily))
{
throw new NotSupportedException(SR.net_invalidversion);
}
MultipleConnectAsync multipleConnectAsync = new SingleSocketMultipleConnectAsync(this, true);
e.StartOperationCommon(this);
e.StartOperationWrapperConnect(multipleConnectAsync);
retval = multipleConnectAsync.StartConnectAsync(e, dnsEP);
}
else
{
// Throw if remote address family doesn't match socket.
if (!CanTryAddressFamily(e.RemoteEndPoint.AddressFamily))
{
throw new NotSupportedException(SR.net_invalidversion);
}
e._socketAddress = CheckCacheRemote(ref endPointSnapshot, false);
// Do wildcard bind if socket not bound.
if (_rightEndPoint == null)
{
if (endPointSnapshot.AddressFamily == AddressFamily.InterNetwork)
{
InternalBind(new IPEndPoint(IPAddress.Any, 0));
}
else
{
InternalBind(new IPEndPoint(IPAddress.IPv6Any, 0));
}
}
// Save the old RightEndPoint and prep new RightEndPoint.
EndPoint oldEndPoint = _rightEndPoint;
if (_rightEndPoint == null)
{
_rightEndPoint = endPointSnapshot;
}
// Prepare for the native call.
e.StartOperationCommon(this);
e.StartOperationConnect();
// Make the native call.
int bytesTransferred;
SocketError socketError = SocketError.Success;
try
{
socketError = e.DoOperationConnect(this, _handle, out bytesTransferred);
}
catch (Exception ex)
{
_rightEndPoint = oldEndPoint;
// Clear in-use flag on event args object.
//.........这里部分代码省略.........
示例3: ConnectAsync
public bool ConnectAsync(SocketAsyncEventArgs e)
{
bool flag;
if (s_LoggingEnabled)
{
Logging.Enter(Logging.Sockets, this, "ConnectAsync", "");
}
if (this.CleanedUp)
{
throw new ObjectDisposedException(base.GetType().FullName);
}
if (e.m_BufferList != null)
{
throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList");
}
if (e.RemoteEndPoint == null)
{
throw new ArgumentNullException("remoteEP");
}
if (this.isListening)
{
throw new InvalidOperationException(SR.GetString("net_sockets_mustnotlisten"));
}
EndPoint remoteEndPoint = e.RemoteEndPoint;
DnsEndPoint endPoint = remoteEndPoint as DnsEndPoint;
if (endPoint != null)
{
if (s_LoggingEnabled)
{
Logging.PrintInfo(Logging.Sockets, "Socket#" + ValidationHelper.HashString(this) + "::ConnectAsync Connecting to a DnsEndPoint");
}
if ((endPoint.AddressFamily != System.Net.Sockets.AddressFamily.Unspecified) && (endPoint.AddressFamily != this.addressFamily))
{
throw new NotSupportedException(SR.GetString("net_invalidversion"));
}
MultipleConnectAsync args = new SingleSocketMultipleConnectAsync(this, true);
e.StartOperationCommon(this);
e.StartOperationWrapperConnect(args);
flag = args.StartConnectAsync(e, endPoint);
}
else
{
int num;
if (this.addressFamily != e.RemoteEndPoint.AddressFamily)
{
throw new NotSupportedException(SR.GetString("net_invalidversion"));
}
e.m_SocketAddress = this.CheckCacheRemote(ref remoteEndPoint, false);
if (this.m_RightEndPoint == null)
{
if (remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
this.InternalBind(new IPEndPoint(IPAddress.Any, 0));
}
else
{
this.InternalBind(new IPEndPoint(IPAddress.IPv6Any, 0));
}
}
EndPoint rightEndPoint = this.m_RightEndPoint;
if (this.m_RightEndPoint == null)
{
this.m_RightEndPoint = remoteEndPoint;
}
e.StartOperationCommon(this);
e.StartOperationConnect();
this.BindToCompletionPort();
SocketError success = SocketError.Success;
try
{
if (!this.ConnectEx(this.m_Handle, e.m_PtrSocketAddressBuffer, e.m_SocketAddress.m_Size, e.m_PtrSingleBuffer, e.Count, out num, e.m_PtrNativeOverlapped))
{
success = (SocketError) Marshal.GetLastWin32Error();
}
}
catch (Exception exception)
{
this.m_RightEndPoint = rightEndPoint;
e.Complete();
throw exception;
}
if ((success != SocketError.Success) && (success != SocketError.IOPending))
{
e.FinishOperationSyncFailure(success, num, SocketFlags.None);
flag = false;
}
else
{
flag = true;
}
}
if (s_LoggingEnabled)
{
Logging.Exit(Logging.Sockets, this, "ConnectAsync", flag);
}
return flag;
}