BufferedStream Class in VB.NET

In this article I will explain you about BufferedStream Class in VB.NET.
  • 3529
 

BufferedStream Class: The BufferedStream class also extends the Stream class. Buffers, or cached blocks of data in memory, provide speed and stability to the process of reading or writing because they prevent numerous calls to the operating system. Buffered streams are used in conjunction with other streams to provide better read/write performance. The BufferedStream class can be used to either read data or write data but it cannot be used to perform both read and write operations together. The class has been optimized so that it maintains a suitable buffer at all times. When a buffer is not required, instead of slowing down the process, the class does not allocate any space in memory. File streams are already buffered and therefore a buffered stream is generally used to buffer network streams used in networking applications.

Example of BufferedStream Class

Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Module Module1
    'The main entry point for the application
    Sub Main()
        ' The following is the default value
        Dim ServerName As String = "127.0.0.1"
        Dim Port As Integer = 5150
        ' Parse command line arguments if any
        Dim args As String() = Environment.GetCommandLineArgs()
        Dim i As Integer
        For i = 1 To args.Length - 1
            Try
                If args(i) = "/server" Then
                    ' The server's name we will connect to
                    i = i + 1
                    ServerName = args(i).ToString()
                ElseIf args(i) = "/port" Then
                    ' The port on which the server is listening
                    i = i + 1
                    Port = System.Convert.ToInt32(args(i).ToString())
                End If
            Catch e As System.IndexOutOfRangeException
                Console.WriteLine("Usage: Sender [/server <server IP>] [/port <port>]")
                Exit Sub
            End Try
        Next
        Dim ClientSocket As Socket = Nothing
        Try
            ' Let's connect to a listening server
            Try
                ClientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
                Console.WriteLine("Socket() is OK...")
            Catch e As Exception
                Throw New Exception("Failed to create client Socket: " + e.Message)
            End Try
            Dim ServerEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Parse(ServerName), Convert.ToInt16(Port))
            Try
                ClientSocket.Connect(ServerEndPoint)
                Console.WriteLine("Connect() is OK...")
            Catch e As Exception
                Throw New Exception("Failed to connect client Socket: " + e.Message)
            End Try
        Catch e As Exception
            Console.WriteLine(e.Message)
            ClientSocket.Close()
            Console.ReadLine()
            Exit Sub
        End Try
        ' Let's create a network stream to communicate over the connected Socket.
        Dim ClientNetworkStream As NetworkStream = Nothing
        Try
            Try
                ' Setup a network stream on the client Socket
                ClientNetworkStream = New NetworkStream(ClientSocket, True)
                Console.WriteLine("Instantiate NetworkStream is OK...")
            Catch e As Exception
                ' We have to close the client socket here because the network stream did not take ownership of the socket.
                ClientSocket.Close()
                Throw New Exception("Failed to create a NetworkStream with error: " + e.Message)
            End Try
        Catch e As Exception
            Console.WriteLine(e.Message)
            ClientNetworkStream.Close()
            Console.ReadLine()
            Exit Sub
        End Try
        Dim ClientBufferedStream As BufferedStream = Nothing
        Try
            Try
                ' Setup a network stream on the client Socket
                ClientBufferedStream = New BufferedStream(ClientNetworkStream)
                Console.WriteLine("Instantiate BufferedStream is OK...")
            Catch e As Exception
                Throw New Exception("Failed to create a BufferedStream with error: " + e.Message)
            End Try
            Try
                Dim Buffer(1) As Byte
                Console.WriteLine("Writing/sending some data...")
                For i = 1 To 200
                    Buffer(0) = i
                    ClientBufferedStream.Write(Buffer, 0, Buffer.GetUpperBound(0))
                Next
                Console.WriteLine("We wrote 200 bytes one byte at a time to the server.")
            Catch e As Exception
                Throw New Exception("Failed to write to client BufferedStream with error: " + e.Message)
            End Try
        Catch e As Exception
            Console.WriteLine(e.Message)
        Finally
            ' We are finished with the Stream so we will close it.
            ClientBufferedStream.Close()
        End Try
        Console.ReadLine()
    End Sub
End Module

Output Window

buffer1.gif
 

Conclusion

Hope this article would have helped you in understanding BufferedStream class in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.