共用方式為


Socket.Select 方法

定義

多載

Select(IList, IList, IList, TimeSpan)

決定一或多個套接字的狀態。

Select(IList, IList, IList, Int32)

決定一或多個套接字的狀態。

Select(IList, IList, IList, TimeSpan)

來源:
Socket.cs
來源:
Socket.cs
來源:
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)

來源:
Socket.cs
來源:
Socket.cs
來源:
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 設定為 -1 會指定無限逾時。

如果您對 Connect進行非封鎖呼叫,則可寫入性表示您已成功連線。 如果您已建立連線,可寫入性表示所有傳送作業都會成功,而不會封鎖。

如果您已對 Connect進行非封鎖呼叫,checkerror 參數會識別未成功連接的套接字。

注意

如果您只想要判斷單一 Socket的狀態,請使用 Poll 方法。

注意

此方法無法偵測某些類型的連線問題,例如中斷的網路纜線,或遠端主機無法正常關閉。 您必須嘗試傳送或接收數據,以偵測這類錯誤。

注意

如果您收到 SocketException,請使用 SocketException.ErrorCode 屬性來取得特定的錯誤碼。 取得此程式代碼之後,請參閱 Windows Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

另請參閱

適用於