Socket.Select(IList, IList, IList, Int32) Socket.Select(IList, IList, IList, Int32) Socket.Select(IList, IList, IList, Int32) Socket.Select(IList, IList, IList, Int32) Method

定义

确定一个或多个套接字的状态。Determines the status of one or more sockets.

public:
 static void Select(System::Collections::IList ^ checkRead, System::Collections::IList ^ checkWrite, System::Collections::IList ^ checkError, int microSeconds);
public static void Select (System.Collections.IList checkRead, System.Collections.IList checkWrite, System.Collections.IList checkError, int microSeconds);
static member Select : System.Collections.IList * System.Collections.IList * System.Collections.IList * int -> unit
Public Shared Sub Select (checkRead As IList, checkWrite As IList, checkError As IList, microSeconds As Integer)

参数

checkRead
IList IList IList IList

用于检查可读性的 Socket 实例的 IListAn IList of Socket instances to check for readability.

checkWrite
IList IList IList IList

用于检查可写性的 Socket 实例的 IListAn IList of Socket instances to check for writability.

checkError
IList IList IList IList

用于检查错误的 Socket 实例的 IListAn IList of Socket instances to check for errors.

microSeconds
Int32 Int32 Int32 Int32

超时值(以微秒为单位)。The time-out value, in microseconds. 值 -1 指示无限超时。A -1 value indicates an infinite time-out.

异常

checkRead 参数为 null 或为空。The checkRead parameter is null or empty.

-和--and- checkWrite 参数为 null 或为空The checkWrite parameter is null or empty -和--and- checkError 参数为 null 或为空。The checkError parameter is null or empty.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

示例

下面的代码示例使用Select来确定哪些侦听套接字已连接请求。The following code example uses Select to determine which listening sockets have a connection request.

      IPHostEntry^ lipa = Dns::Resolve( Dns::GetHostName() );
      
      //Gets three separate local endpoints.
      IPEndPoint^ lep1 = gcnew IPEndPoint( lipa->AddressList[ 0 ],11000 );
      IPEndPoint^ lep2 = gcnew IPEndPoint( lipa->AddressList[ 0 ],11001 );
      IPEndPoint^ lep3 = gcnew IPEndPoint( lipa->AddressList[ 0 ],11002 );
      
      //creates an array of endpoints.
      array<IPEndPoint^>^ipendpoints = gcnew array<IPEndPoint^>(3);
      ipendpoints[ 0 ] = lep1;
      ipendpoints[ 1 ] = lep2;
      ipendpoints[ 2 ] = lep3;
      
      //Creates three separate sockets.
      Socket^ s1 = gcnew Socket( lep1->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp );
      Socket^ s2 = gcnew Socket( lep2->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp );
      Socket^ s3 = gcnew Socket( lep3->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp );
      array<Socket^>^socketList = gcnew array<Socket^>(3);
      socketList[ 0 ] = s1;
      socketList[ 1 ] = s2;
      socketList[ 2 ] = s3;
      
      //Binds and Listens on all sockets in the array of sockets.
      for ( int i = 0; i < 3; i++ )
      {
         socketList[ i ]->Bind( ipendpoints[ i ] );
         socketList[ i ]->Listen( 1000 );

      }
      
      //Calls Select to determine which sockets are ready for reading.
      Socket::Select( safe_cast<IList^>(socketList), nullptr, nullptr, 1000 );
      
      //Reads on the sockets returned by Select.
      array<Byte>^buffer = gcnew array<Byte>(1024);
      String^ outString;
      for ( Int32 j = 0; j < (socketList->Length - 1); j++ )
      {
         socketList[ j ]->Receive( buffer );
         outString =  "Socket ";
         outString->Concat( j.ToString(),  " has the message", Encoding::ASCII->GetString( buffer ) );
         Console::WriteLine( outString );

      }
   }

};

int main()
{
   return 0;
}

IPHostEntry ipHostEntry = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostEntry.AddressList[0];

Socket socket0 = null;
Socket socket1 = null; 
Socket socket2 = null; 
Socket socket3 = null; 
Socket socket4 = null; 
Socket socket5 = null; 

ArrayList listenList = new ArrayList();
listenList.Add(socket0);
listenList.Add(socket1);
listenList.Add(socket2);

ArrayList acceptList = new ArrayList();
acceptList.Add(socket3);
acceptList.Add(socket4);
acceptList.Add(socket5);

for( int i = 0; i < 3; i++ )
{
  listenList[i] = new Socket(AddressFamily.InterNetwork,
                             SocketType.Stream,
                             ProtocolType.Tcp);
  ((Socket)listenList[i]).Bind(new IPEndPoint(ipAddress, 11000 + i));
  ((Socket)listenList[i]).Listen(10);
}

// Only the sockets that contain a connection request
// will remain in listenList after Select returns.

Socket.Select(listenList, null, null, 1000);

for( int i = 0; i < listenList.Count; i++ )
{
  acceptList[i] = ((Socket)listenList[i]).Accept();
}
Dim ipHostEntry As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostEntry.AddressList(0)

Dim socket0 As Socket = Nothing
Dim socket1 As Socket = Nothing
Dim socket2 As Socket = Nothing
Dim socket3 As Socket = Nothing
Dim socket4 As Socket = Nothing
Dim socket5 As Socket = Nothing

Dim listenList As New ArrayList()
listenList.Add(socket0)
listenList.Add(socket1)
listenList.Add(socket2)

Dim acceptList As New ArrayList()
acceptList.Add(socket3)
acceptList.Add(socket4)
acceptList.Add(socket5)

Dim i As Integer
For i = 0 To 2
   listenList(i) = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
   CType(listenList(i), Socket).Bind(New IPEndPoint(ipAddress, 11000 + i))
   CType(listenList(i), Socket).Listen(10)
Next i

'Only the sockets that contain a connection request
'will remain in listenList after Select returns.
Socket.Select(listenList, Nothing, Nothing, 1000)

For i = 0 To listenList.Count - 1
   acceptList(i) = CType(listenList(i), Socket).Accept()
Next i

注解

Select 是确定的一个或多个状态的静态方法Socket实例。Select is a static method that determines the status of one or more Socket instances. 必须将放置到一个或多个套接字IList可以使用之前Select方法。You must place one or more sockets into an IList before you can use the Select method. 通过调用检查可读性SelectIList作为checkRead参数。Check for readability by calling Select with the IList as the checkRead parameter. 若要检查的可写性套接字,使用checkWrite参数。To check your sockets for writability, use the checkWrite parameter. 检测错误条件,请使用checkErrorFor detecting error conditions, use checkError. 在调用Select,则IList会填充使用满足条件这些套接字。After calling Select, the IList will be filled with only those sockets that satisfy the conditions.

如果您在侦听状态,可读性意味着调用Accept而不会阻止将会成功。If you are in a listening state, readability means that a call to Accept will succeed without blocking. 如果已接受连接,可读性意味着数据可供读取。If you have already accepted the connection, readability means that data is available for reading. 在这些情况下,所有接收操作而不会阻止将会成功。In these cases, all receive operations will succeed without blocking. 可读性也可指示是否在远程Socket已关闭连接; 在这种情况下调用Receive会立即返回,返回零字节。Readability can also indicate whether the remote Socket has shut down the connection; in that case a call to Receive will return immediately, with zero bytes returned.

Select 返回时至少一个感兴趣的套接字 (在套接字checkReadcheckWrite,并checkError列出了) 满足指定的条件时,或microSeconds超过参数,具体取决于第一个。Select returns when at least one of the sockets of interest (the sockets in the checkRead, checkWrite, and checkError lists) meets its specified criteria, or the microSeconds parameter is exceeded, whichever comes first. 设置microSeconds为-1 指定无限期超时。Setting microSeconds to -1 specifies an infinite time-out.

如果进行到非阻止调用Connect,可写性表示您已成功连接。If you make a nonblocking call to Connect, writability means that you have connected successfully. 如果已建立的连接,可写性意味着所有发送操作都将成功而不会阻塞。If you already have a connection established, writability means that all send operations will succeed without blocking.

如果您已到非阻止调用Connect,则checkerror参数标识未成功连接的套接字。If you have made a non-blocking call to Connect, the checkerror parameter identifies sockets that have not connected successfully.

备注

使用Poll方法,如果只想要确定的单个状态SocketUse the Poll method if you only want to determine the status of a single Socket.

备注

此方法无法检测到某些类型的连接问题,例如断开的网络电缆,或远程主机意外关闭。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.

备注

如果你收到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.

适用于

另请参阅