Socket.Available 属性

定义

获取已经从网络接收且可供读取的数据量。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.

Socket 已关闭。The Socket has been closed.

示例

下面的代码示例将调用 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 是在调用 Receive之前确定数据是否排队等待读取的好方法。If 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.

适用于

另请参阅