Socket.Poll(Int32, SelectMode) Socket.Poll(Int32, SelectMode) Socket.Poll(Int32, SelectMode) Socket.Poll(Int32, SelectMode) Method

定义

确定 Socket 的状态。Determines the status of the Socket.

public:
 bool Poll(int microSeconds, System::Net::Sockets::SelectMode mode);
public bool Poll (int microSeconds, System.Net.Sockets.SelectMode mode);
member this.Poll : int * System.Net.Sockets.SelectMode -> bool
Public Function Poll (microSeconds As Integer, mode As SelectMode) As Boolean

参数

microSeconds
Int32 Int32 Int32 Int32

等待响应的时间(以微秒为单位)。The time to wait for a response, in microseconds.

mode
SelectMode SelectMode SelectMode SelectMode

SelectMode 值之一。One of the SelectMode values.

返回

基于 mode 参数中传递的轮询模式值的 Socket 的状态。The status of the Socket based on the polling mode value passed in the mode parameter.

模式Mode 返回值Return Value
SelectRead如果已调用 Listen(Int32) 并且有挂起的连接,则为 truetrue if Listen(Int32) has been called and a connection is pending; -or- 如果有数据可供读取,则为 truetrue if data is available for reading; - 或 --or- 如果连接已关闭、重置或终止,则返回 truetrue if the connection has been closed, reset, or terminated; 否则,返回 falseotherwise, returns false.
SelectWrite如果正在处理 Connect(EndPoint) 并且连接已成功,则为 truetrue, if processing a Connect(EndPoint), and the connection has succeeded; -or- 如果可以发送数据,则返回 truetrue if data can be sent; 否则,返回 falseotherwise, returns false.
SelectError如果正在处理不阻止的 Connect(EndPoint),并且连接已失败,则为 truetrue if processing a Connect(EndPoint) that does not block, and the connection has failed; -or- 如果 OutOfBandInline 未设置,并且带外数据可用,则为 truetrue if OutOfBandInline is not set and out-of-band data is available; 否则,返回 falseotherwise, returns false.

异常

mode 参数不是一个 SelectMode 值。The mode parameter is not one of the SelectMode values.

尝试访问套接字时出错。An error occurred when attempting to access the socket. 请参见下面的备注。See remarks below.

示例

下面的代码示例创建套接字,连接到服务器,并使用Poll检查套接字的状态。The following code example creates a socket, connects to a server, and uses Poll to check the status of the socket.

//Creates the Socket for sending data over TCP.
Socket^ s = gcnew Socket( AddressFamily::InterNetwork, SocketType::Stream,
   ProtocolType::Tcp );

// Connects to host using IPEndPoint.
s->Connect( EPhost );
if ( !s->Connected )
{
   strRetPage = "Unable to connect to host";
}
// Use the SelectWrite enumeration to obtain Socket status.
if ( s->Poll( -1, SelectMode::SelectWrite ) )
{
   Console::WriteLine( "This Socket is writable." );
}
else if ( s->Poll(  -1, SelectMode::SelectRead ) )
{
   Console::WriteLine( "This Socket is readable." );
}
else if ( s->Poll(  -1, SelectMode::SelectError ) )
{
   Console::WriteLine( "This Socket has an error." );
}
   //Creates the Socket for sending data over TCP.
   Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
      ProtocolType.Tcp );

   // Connects to host using IPEndPoint.
   s.Connect(EPhost);
   if (!s.Connected)
   {
      strRetPage = "Unable to connect to host";
   }
   // Use the SelectWrite enumeration to obtain Socket status.
    if(s.Poll(-1, SelectMode.SelectWrite)){
         Console.WriteLine("This Socket is writable.");
    }
    else if (s.Poll(-1, SelectMode.SelectRead)){
          Console.WriteLine("This Socket is readable." );
    }
    else if (s.Poll(-1, SelectMode.SelectError)){
         Console.WriteLine("This Socket has an error.");
    }

'Creates the Socket for sending data over TCP.
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

' Connects to host using IPEndPoint.
s.Connect(EPhost)
If Not s.Connected Then
   strRetPage = "Unable to connect to host"
End If
' Use the SelectWrite enumeration to obtain Socket status.
If s.Poll(- 1, SelectMode.SelectWrite) Then
   Console.WriteLine("This Socket is writable.")
Else
   If s.Poll(- 1, SelectMode.SelectRead) Then
      Console.WriteLine(("This Socket is readable. "))
   Else
      If s.Poll(- 1, SelectMode.SelectError) Then
         Console.WriteLine("This Socket has an error.")
      End If
   End If 
End If 

注解

方法将检查的状态SocketPollThe Poll method will check the state of the Socket. SelectMode.SelectReadselectMode 参数指定以确定Socket是否可读。Specify SelectMode.SelectRead for the selectMode parameter to determine if the Socket is readable. 指定SelectMode.SelectWrite以确定Socket是否可写。Specify SelectMode.SelectWrite to determine if the Socket is writable. 使用SelectMode.SelectError检测错误条件。Use SelectMode.SelectError to detect an error condition. Poll将阻止执行到指定的时间段(以为microseconds单位)。Poll will block execution until the specified time period, measured in microseconds, elapses. 如果要无限期等待响应,请将参数设置为负整数。microSecondsSet the microSeconds parameter to a negative integer if you would like to wait indefinitely for a response. 如果要检查多个套接字的状态,可能更倾向于使用Select方法。If you want to check the status of multiple sockets, you might prefer to use the Select method.

备注

如果收到SocketException,请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码文档,以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

此方法无法检测某些类型的连接问题,例如网络电缆损坏或远程主机已关闭意外。This method cannot detect certain kinds of connection problems, such as a broken network cable, or that the remote host was shut down ungracefully. 您必须尝试发送或接收数据,以检测这些类型的错误。You must attempt to send or receive data to detect these kinds of errors.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

适用于

另请参阅