Socket.Available 屬性
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得已從網路接收且可供讀取的資料量。
public:
property int Available { int get(); };
C#
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;
}
C#
// 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 Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。
備註
在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤。
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.3, 1.4, 1.6, 2.0, 2.1 |