Socket.Select 方法

定义

重载

Select(IList, IList, IList, TimeSpan)

确定一个或多个套接字的状态。

Select(IList, IList, IList, Int32)

确定一个或多个套接字的状态。

Select(IList, IList, IList, TimeSpan)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

确定一个或多个套接字的状态。

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

参数

checkRead
IList

要检查可读性的 Socket 实例的 IList

checkWrite
IList

要检查可写性的 Socket 实例的 IList

checkError
IList

用于检查错误的 Socket 实例的 IList

timeout
TimeSpan

超时值。 等于 -1 微秒的值表示无限超时。

例外

checkReadcheckWritecheckError 参数都是 null 或空的。 至少有一个 checkReadcheckWritecheckError 必须至少包含一个 Socket

checkReadcheckWritecheckError 参数包含过多的套接字。

timeout 小于 -1 微秒或大于 MaxValue 微秒

尝试访问套接字时出错。

释放了一个或多个套接字。

适用于

Select(IList, IList, IList, Int32)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

确定一个或多个套接字的状态。

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);
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

要检查可读性的 Socket 实例的 IList

checkWrite
IList

要检查可写性的 Socket 实例的 IList

checkError
IList

用于检查错误的 Socket 实例的 IList

microSeconds
Int32

超时值(以微秒为单位)。 -1 值表示无限超时。

例外

checkReadcheckWritecheckError 参数都是 null 或空的。 至少有一个 checkReadcheckWritecheckError 必须至少包含一个 Socket

尝试访问套接字时出错。

.NET 5 及更高版本:释放一个或多个套接字。

checkReadcheckWritecheckError 参数包含过多的套接字。

示例

下面的代码示例使用 Select 来确定哪些侦听套接字具有连接请求。

      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 实例的状态。 必须先将一个或多个套接字放入 IList,然后才能使用 Select 方法。 通过将 IList 作为 checkRead 参数调用 Select 来检查可读性。 若要检查套接字是否可写,请使用 checkWrite 参数。 若要检测错误条件,请使用 checkError。 调用 Select后,IList 将只填充满足条件的套接字。

如果处于侦听状态,可读性意味着对 Accept 的调用将成功,而不会阻止。 如果已接受连接,可读性意味着数据可用于读取。 在这些情况下,所有接收操作都会成功,而不会阻止。 可读性还可以指示远程 Socket 是否已关闭连接;在这种情况下,调用 Receive 将立即返回,并返回零个字节。

Select 当至少一个感兴趣的套接字(checkReadcheckWritecheckError 列表中的套接字)满足其指定条件或超出 microSeconds 参数时,将返回 microSeconds 参数,以先行者为准。 将 microSeconds 设置为 -1 指定无限超时。

如果对 Connect进行非阻止调用,可写性意味着已成功连接。 如果已建立连接,可写性意味着所有发送操作都将成功,而不会阻止。

如果对 Connect进行了非阻止调用,checkerror 参数将标识未成功连接的套接字。

注意

如果只想确定单个 Socket的状态,请使用 Poll 方法。

注意

此方法无法检测某些类型的连接问题,例如网络电缆断开,或者远程主机未正常关闭。 必须尝试发送或接收数据以检测这些类型的错误。

注意

如果收到 SocketException,请使用 SocketException.ErrorCode 属性获取特定的错误代码。 获取此代码后,请参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

另请参阅

适用于