Socket.Available Socket.Available Socket.Available Socket.Available Property

定义

获取已经从网络接收且可供读取的数据量。Gets the amount of data that has been received from the network and is available to be read.

public:
 property int Available { int get(); };
public int Available { get; }
member this.Available : int
Public ReadOnly Property Available As Integer

属性值

从网络接收的、可供读取的数据的字节数。The number of bytes of data received from the network and available to be read.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

示例

下面的代码示例将调用 IOControl 使用 FIONREAD 和可用属性的结果进行比较。The following code example compares the results of calling IOControl with FIONREAD and the Available property.

// 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;
 }

注解

如果使用非阻塞SocketAvailable是好的方法,以确定是否将数据排队以进行读取,然后才能调用ReceiveIf you are using a non-blocking Socket, Available is a good way to determine whether data is queued for reading, before calling Receive. 可用的数据是在读取的网络缓冲区中对排队的总数据量。The available data is the total amount of data queued in the network buffer for reading. 如果无数据排队网络缓冲区中Available返回 0。If no data is queued in the network buffer, Available returns 0.

如果远程主机关闭,或关闭的连接Available可能会引发SocketExceptionIf the remote host shuts down or closes the connection, Available can throw a SocketException. 如果你收到SocketException,使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码有关错误的详细说明的文档。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅在.NET Framework 中进行网络跟踪For more information, see Network Tracing in the .NET Framework.

适用于

另请参阅