| Visual Basic (Declaration) | |
|---|---|
<DescriptionAttribute("Returns the Available property of the Socket instance.")> <BrowsableAttribute(False)> Public ReadOnly Property Available As Integer | |
| C# | |
|---|---|
[DescriptionAttribute("Returns the Available property of the Socket instance.")] [BrowsableAttribute(false)] public int Available {get;} | |
| Managed Extensions for C++ | |
|---|---|
[DescriptionAttribute("Returns the Available property of the Socket instance.")] [BrowsableAttribute(false)] public: __property int get_Available(); | |
| C++/CLI | |
|---|---|
[DescriptionAttribute("Returns the Available property of the Socket instance.")] [BrowsableAttribute(false)] public: property int Available { int get(); } | |
Property Value
The number of bytes of data that has been received from the network and are available to be read.The following example demonstrates receiving a chunked response from the server. HTTP 1.1 allows a server to send a "chunked" response. When this occurs, the server will send the headers then a hex value representing the size of the next "chunk". This is continued until all data has been sent. This is a good stream example because it demonstrates both reading until a delimiter is found and reading fixed-size "chunks" of data.
| Visual Basic | Copy Code |
|---|---|
' Connect to HTTP Port Tcp1.Connect("www.yahoo.com", 80) ' Send GET Request for home page. Be sure to specify HTTP/1.1 and Host ' to get a chunked response. Dim command As Byte() = System.Text.Encoding.Default.GetBytes("GET / HTTP/1.1" + vbCrLf + "Host:www.yahoo.com" + vbCrLf + vbCrLf) Tcp1.Stream.Write(command) ' Get the headers by reading until a double CRLF is found. Dim found As Boolean = False Dim s As String = Tcp1.Stream.Read(vbCrLf + vbCrLf, 1024, found) Dim pagedata As String = "" ' Keep receiving data. When all data is sent, the server will close the connection. Do While Tcp1.Connected ' Get the chunk size by reading until a CRLF is found. s = Tcp1.Stream.Read(vbCrLf, 1024, found) ' Remove the delimiter s = s.Trim() ' Make sure s has data If s <> "" Then ' Convert from string hex to decimal integer. Dim chunksize As Integer = System.Convert.ToInt32(s, 16) ' The final chunk will have size 0 and does not need to be handled. If chunksize <> 0 Then ' initialize byte array with chunk size Dim data(chunksize) As Byte ' Fill the byte array. Tcp1.Stream.Read(data, True) ' Add to the string pagedata += System.Text.Encoding.Default.GetString(data) End If End If Loop ' Entire page should be retrieved by this point. Display Debug.WriteLine(pagedata) | |
| C# | Copy Code |
|---|---|
// Connect to HTTP Port tcp1.Connect("www.yahoo.com", 80); // Send GET Request for home page. Be sure to specify HTTP/1.1 and Host // to get a chunked response. byte[] command = System.Text.Encoding.Default.GetBytes("GET / HTTP/1.1\r\nHost:www.yahoo.com\r\n\r\n"); tcp1.Stream.Write(command); // Get the headers by reading until a double CRLF is found. bool found = false; string s = tcp1.Stream.Read("\r\n\r\n", 1024, ref found); string pagedata = ""; // Keep receiving data. When all data is sent, the server will close the connection. while(tcp1.Connected) { // Get the chunk size by reading until a CRLF is found. s = tcp1.Stream.Read("\r\n", 1024, ref found); // Remove the delimiter s = s.Trim(); // Make sure s has data if(s != "") { // Convert from string hex to decimal integer. int chunksize = System.Convert.ToInt32(s, 16); // The final chunk will have size 0 and does not need to be handled. if(chunksize != 0) { // initialize byte array with chunk size byte[] data = new byte[chunksize]; // Fill the byte array. tcp1.Stream.Read(data, true); // Add to the string pagedata+= System.Text.Encoding.Default.GetString(data); } } } // Entire page should be retrieved at this point. Display Debug.WriteLine(pagedata); | |
When data is received from the network, it is collected in system buffers. The Tcp.Available property contains the amount of bytes of data that has been received. In order to receive the data into a useful data structure such as a string, byte array, or Segment object that you can use within your application you must use the Tcp.Receive or Tcp.Stream.Read methods. If these methods are not invoked or are seldom invoked, then "back pressure" is exerted at the remote host and packet transmission rates will decrease. If these methods are invoked, the data is received and removed from the system buffers, making room for more data to be received from the network.
Target Platforms: Microsoft .NET Framework 2.0
Copy Code