|
|
|
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
1195
|
|
Downloads :
|
5
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
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

Conclusion
Hope this article would have helped you in understanding BufferedStream class in
VB.NET.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|