Socket.Available 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得已從網路接收且可供讀取的資料量。
public:
property int Available { int get(); };
public int Available { get; }
member this.Available : int
Public ReadOnly Property Available As Integer
屬性值
從網路收到的和可供讀取的資料位元組數。
例外狀況
嘗試存取通訊端時發生錯誤。
範例
下列程式碼範例會比較呼叫 IOControl 與 FIONREAD 和 Available 屬性的結果。
// FIONREAD is also available as the "Available" property.
const int FIONREAD = 0x4004667F;
void DisplayPendingByteCount( Socket^ s )
{
array<Byte>^ outValue = BitConverter::GetBytes( 0 );
// Check how many bytes have been received.
s->IOControl( FIONREAD, nullptr, outValue );
UInt32 bytesAvailable = BitConverter::ToUInt32( outValue, 0 );
Console::WriteLine( "server has {0} bytes pending. Available property says {1}.",
bytesAvailable, s->Available );
return;
}
// FIONREAD is also available as the "Available" property.
public const int FIONREAD = 0x4004667F;
static void DisplayPendingByteCount(Socket s)
{
byte[] outValue = BitConverter.GetBytes(0);
// Check how many bytes have been received.
s.IOControl(FIONREAD, null, outValue);
uint bytesAvailable = BitConverter.ToUInt32(outValue, 0);
Console.WriteLine("server has {0} bytes pending. Available property says {1}.",
bytesAvailable, s.Available);
return;
}
備註
如果您使用非封鎖 Socket , Available 在呼叫 Receive 之前,最好先判斷資料是否排入佇列以供讀取。 可用的資料是網路緩衝區中排入佇列以供讀取的資料總數。 如果網路緩衝區中沒有任何資料排入佇列,則 Available 傳回 0。
如果遠端主機關閉或關閉連線, Available 可能會擲回 SocketException 。 如果您收到 SocketException ,請使用 SocketException.ErrorCode 屬性來取得特定的錯誤碼。 取得此程式碼之後,請參閱Windows 通訊端第 2 版 API 錯誤碼檔,以取得錯誤的詳細描述。
注意
在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework中的網路追蹤。