Share via


Socket.Select Metode

Definisi

Overload

Select(IList, IList, IList, TimeSpan)

Menentukan status satu atau beberapa soket.

Select(IList, IList, IList, Int32)

Menentukan status satu atau beberapa soket.

Select(IList, IList, IList, TimeSpan)

Sumber:
Socket.cs
Sumber:
Socket.cs
Sumber:
Socket.cs

Menentukan status satu atau beberapa soket.

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)

Parameter

checkRead
IList

Instans IListSocket untuk memeriksa keterbacaan.

checkWrite
IList

Instans IListSocket untuk memeriksa writabilitas.

checkError
IList

Instans IListSocket untuk memeriksa kesalahan.

timeout
TimeSpan

Nilai batas waktu. Nilai yang sama dengan -1 mikrosekon menunjukkan batas waktu yang tak terbatas.

Pengecualian

Parameter checkRead, checkWrite, atau checkError adalah null atau kosong.

Parameter checkRead, checkWrite, atau checkError berisi terlalu banyak soket.

kurang timeout dari -1 mikrodetik atau lebih besar dari MaxValue mikrodetik

Terjadi kesalahan saat mencoba mengakses soket.

Satu atau beberapa soket dibuang.

Berlaku untuk

Select(IList, IList, IList, Int32)

Sumber:
Socket.cs
Sumber:
Socket.cs
Sumber:
Socket.cs

Menentukan status satu atau beberapa soket.

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)

Parameter

checkRead
IList

Instans IListSocket untuk memeriksa keterbacaan.

checkWrite
IList

Instans IListSocket untuk memeriksa writabilitas.

checkError
IList

Instans IListSocket untuk memeriksa kesalahan.

microSeconds
Int32

Nilai waktu habis, dalam mikrostik. Nilai -1 menunjukkan batas waktu tak terbatas.

Pengecualian

Parameternya checkRead adalah null atau kosong.

-dan-

Parameternya checkWrite adalah null atau kosong

-dan-

Parameternya checkError adalah null atau kosong.

Terjadi kesalahan saat mencoba mengakses soket.

.NET 5 dan yang lebih baru: Satu atau beberapa soket dibuang.

Parameter checkRead, checkWrite, atau checkError berisi terlalu banyak soket.

Contoh

Contoh kode berikut menggunakan Select untuk menentukan soket mendengarkan mana yang memiliki permintaan koneksi.

      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

Keterangan

Select adalah metode statis yang menentukan status satu atau beberapa Socket instans. Anda harus menempatkan satu atau beberapa soket ke dalam sebelum IList Anda dapat menggunakan metode ini Select . Periksa keterbacaan dengan memanggil Select dengan IList sebagai checkRead parameter . Untuk memeriksa soket Anda untuk writability, gunakan checkWrite parameter . Untuk mendeteksi kondisi kesalahan, gunakan checkError. Setelah memanggil Select, IList hanya akan diisi dengan soket yang memenuhi kondisi.

Jika Anda berada dalam status mendengarkan, keterbacaan berarti bahwa panggilan ke Accept akan berhasil tanpa memblokir. Jika Anda telah menerima koneksi, keterbacaan berarti data tersedia untuk dibaca. Dalam kasus ini, semua operasi penerima akan berhasil tanpa memblokir. Keterbacaan juga dapat menunjukkan apakah jarak jauh Socket telah mematikan koneksi; dalam hal ini panggilan ke Receive akan segera kembali, dengan nol byte dikembalikan.

Selectmengembalikan ketika setidaknya salah satu soket yang menarik (soket dalam checkReadcheckWrite, , dan checkError daftar) memenuhi kriteria yang ditentukan, atau microSeconds parameter terlampaui, mana yang lebih dulu. Pengaturan microSeconds ke -1 menentukan batas waktu tak terbatas.

Jika Anda melakukan panggilan nonblocking ke Connect, writability berarti Anda telah berhasil tersambung. Jika Anda sudah memiliki koneksi yang dibuat, writability berarti bahwa semua operasi pengiriman akan berhasil tanpa memblokir.

Jika Anda telah melakukan panggilan non-pemblokiran ke Connect, checkerror parameter mengidentifikasi soket yang belum berhasil tersambung.

Catatan

Poll Gunakan metode jika Anda hanya ingin menentukan status satu Socket.

Catatan

Metode ini tidak dapat mendeteksi jenis masalah koneksi tertentu, seperti kabel jaringan yang rusak, atau bahwa host jarak jauh dimatikan dengan tidak memalukan. Anda harus mencoba mengirim atau menerima data untuk mendeteksi jenis kesalahan ini.

Catatan

Jika Anda menerima SocketException, gunakan SocketException.ErrorCode properti untuk mendapatkan kode kesalahan tertentu. Setelah Anda mendapatkan kode ini, lihat dokumentasi kode kesalahan API Windows Sockets versi 2 untuk deskripsi terperinci tentang kesalahan tersebut.

Lihat juga

Berlaku untuk