' Compile using /r:System.dll.
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Globalization
Imports System.Net
Imports System.Net.Sockets
Public Class Client
Const dataArraySize As Integer = 100
Const streamBufferSize As Integer = 1000
Const numberOfLoops As Integer = 10000
Shared Sub Main(args As String())
' Check that an argument was specified when the
' program was invoked.
If args.Length = 0 Then
Console.WriteLine("Error: The name of the host " & _
"computer must be specified when the program " & _
"is invoked.")
Return
End If
Dim remoteName As String = args(0)
' Create the underlying socket and connect to the server.
Dim clientSocket As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
clientSocket.Connect(New IPEndPoint( _
Dns.Resolve(remoteName).AddressList(0), 1800))
Console.WriteLine("Client is connected." & vbCrLf)
' Create a NetworkStream that owns clientSocket and then
' create a BufferedStream on top of the NetworkStream.
Dim netStream As New NetworkStream(clientSocket, True)
Dim bufStream As New _
BufferedStream(netStream, streamBufferSize)
Try
' Check whether the underlying stream supports seeking.
If bufStream.CanSeek Then
Console.WriteLine("NetworkStream supports" & _
"seeking." & vbCrLf)
Else
Console.WriteLine("NetworkStream does not " & _
"support seeking." & vbCrLf)
End If
' Send and receive data.
If bufStream.CanWrite Then
SendData(netStream, bufStream)
End If
If bufStream.CanRead Then
ReceiveData(netStream, bufStream)
End If
Finally
' When bufStream is closed, netStream is in turn
' closed, which in turn shuts down the connection
' and closes clientSocket.
Console.WriteLine(vbCrLf & "Shutting down the connection.")
bufStream.Close()
End Try
End Sub
Shared Sub SendData(netStream As Stream, bufStream As Stream)
Dim startTime As DateTime
Dim networkTime As Double, bufferedTime As Double
' Create random data to send to the server.
Dim dataToSend(dataArraySize - 1) As Byte
Dim randomGenerator As New Random()
randomGenerator.NextBytes(dataToSend)
' Send the data using the NetworkStream.
Console.WriteLine("Sending data using NetworkStream.")
startTime = DateTime.Now
For i As Integer = 1 To numberOfLoops
netStream.Write(dataToSend, 0, dataToSend.Length)
Next i
networkTime = DateTime.Now.Subtract(startTime).TotalSeconds
Console.WriteLine("{0} bytes sent in {1} seconds." & vbCrLf, _
numberOfLoops * dataToSend.Length, _
networkTime.ToString("F1"))
' Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.")
startTime = DateTime.Now
For i As Integer = 1 To numberOfLoops
bufStream.Write(dataToSend, 0, dataToSend.Length)
Next i
bufStream.Flush()
bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds
Console.WriteLine("{0} bytes sent In {1} seconds." & vbCrLf, _
numberOfLoops * dataToSend.Length, _
bufferedTime.ToString("F1"))
' Print the ratio of write times.
Console.Write("Sending data using the buffered " & _
"network stream was {0}", _
(networkTime/bufferedTime).ToString("P0"))
If bufferedTime < networkTime Then
Console.Write(" faster")
Else
Console.Write(" slower")
End If
Console.WriteLine(" than using the network stream alone.")
End Sub
Shared Sub ReceiveData(netStream As Stream, bufStream As Stream)
Dim startTime As DateTime
Dim networkTime As Double, bufferedTime As Double = 0
Dim bytesReceived As Integer = 0
Dim receivedData(dataArraySize - 1) As Byte
' Receive data using the NetworkStream.
Console.WriteLine("Receiving data using NetworkStream.")
startTime = DateTime.Now
While bytesReceived < numberOfLoops * receivedData.Length
bytesReceived += netStream.Read( _
receivedData, 0, receivedData.Length)
End While
networkTime = DateTime.Now.Subtract(startTime).TotalSeconds
Console.WriteLine("{0} bytes received in {1} " & _
"seconds." & vbCrLf, _
bytesReceived.ToString(), _
networkTime.ToString("F1"))
' Receive data using the BufferedStream.
Console.WriteLine("Receiving data using BufferedStream.")
bytesReceived = 0
startTime = DateTime.Now
Dim numBytesToRead As Integer = receivedData.Length
Dim n As Integer
Do While numBytesToRead > 0
'Read my return anything from 0 to numBytesToRead
n = bufStream.Read(receivedData, 0, receivedData.Length)
'The end of the file is reached.
If n = 0 Then
Exit Do
End If
bytesReceived += n
numBytesToRead -= n
Loop
bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds
Console.WriteLine("{0} bytes received in {1} " & _
"seconds." & vbCrLf, _
bytesReceived.ToString(), _
bufferedTime.ToString("F1"))
' Print the ratio of read times.
Console.Write("Receiving data using the buffered " & _
"network stream was {0}", _
(networkTime/bufferedTime).ToString("P0"))
If bufferedTime < networkTime Then
Console.Write(" faster")
Else
Console.Write(" slower")
End If
Console.WriteLine(" than using the network stream alone.")
End Sub
End Class
using System;
using System.IO;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
public class Client
{
const int dataArraySize = 100;
const int streamBufferSize = 1000;
const int numberOfLoops = 10000;
static void Main(string[] args)
{
// Check that an argument was specified when the
// program was invoked.
if(args.Length == 0)
{
Console.WriteLine("Error: The name of the host computer" +
" must be specified when the program is invoked.");
return;
}
string remoteName = args[0];
// Create the underlying socket and connect to the server.
Socket clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(new IPEndPoint(
Dns.Resolve(remoteName).AddressList[0], 1800));
Console.WriteLine("Client is connected.\n");
// Create a NetworkStream that owns clientSocket and
// then create a BufferedStream on top of the NetworkStream.
// Both streams are disposed when execution exits the
// using statement.
using(Stream
netStream = new NetworkStream(clientSocket, true),
bufStream =
new BufferedStream(netStream, streamBufferSize))
{
// Check whether the underlying stream supports seeking.
Console.WriteLine("NetworkStream {0} seeking.\n",
bufStream.CanSeek ? "supports" : "does not support");
// Send and receive data.
if(bufStream.CanWrite)
{
SendData(netStream, bufStream);
}
if(bufStream.CanRead)
{
ReceiveData(netStream, bufStream);
}
// When bufStream is closed, netStream is in turn
// closed, which in turn shuts down the connection
// and closes clientSocket.
Console.WriteLine("\nShutting down the connection.");
bufStream.Close();
}
}
static void SendData(Stream netStream, Stream bufStream)
{
DateTime startTime;
double networkTime, bufferedTime;
// Create random data to send to the server.
byte[] dataToSend = new byte[dataArraySize];
new Random().NextBytes(dataToSend);
// Send the data using the NetworkStream.
Console.WriteLine("Sending data using NetworkStream.");
startTime = DateTime.Now;
for(int i = 0; i < numberOfLoops; i++)
{
netStream.Write(dataToSend, 0, dataToSend.Length);
}
networkTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes sent in {1} seconds.\n",
numberOfLoops * dataToSend.Length,
networkTime.ToString("F1"));
// Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.");
startTime = DateTime.Now;
for(int i = 0; i < numberOfLoops; i++)
{
bufStream.Write(dataToSend, 0, dataToSend.Length);
}
bufStream.Flush();
bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes sent in {1} seconds.\n",
numberOfLoops * dataToSend.Length,
bufferedTime.ToString("F1"));
// Print the ratio of write times.
Console.WriteLine("Sending data using the buffered " +
"network stream was {0} {1} than using the network " +
"stream alone.\n",
(networkTime/bufferedTime).ToString("P0"),
bufferedTime < networkTime ? "faster" : "slower");
}
static void ReceiveData(Stream netStream, Stream bufStream)
{
DateTime startTime;
double networkTime, bufferedTime = 0;
int bytesReceived = 0;
byte[] receivedData = new byte[dataArraySize];
// Receive data using the NetworkStream.
Console.WriteLine("Receiving data using NetworkStream.");
startTime = DateTime.Now;
while(bytesReceived < numberOfLoops * receivedData.Length)
{
bytesReceived += netStream.Read(
receivedData, 0, receivedData.Length);
}
networkTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes received in {1} seconds.\n",
bytesReceived.ToString(),
networkTime.ToString("F1"));
// Receive data using the BufferedStream.
Console.WriteLine("Receiving data using BufferedStream.");
bytesReceived = 0;
startTime = DateTime.Now;
int numBytesToRead = receivedData.Length;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = bufStream.Read(receivedData,0, receivedData.Length);
// The end of the file is reached.
if (n == 0)
break;
bytesReceived += n;
numBytesToRead -= n;
}
bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes received in {1} seconds.\n",
bytesReceived.ToString(),
bufferedTime.ToString("F1"));
// Print the ratio of read times.
Console.WriteLine("Receiving data using the buffered network" +
" stream was {0} {1} than using the network stream alone.",
(networkTime/bufferedTime).ToString("P0"),
bufferedTime < networkTime ? "faster" : "slower");
}
}
#using <system.dll>
using namespace System;
using namespace System::IO;
using namespace System::Globalization;
using namespace System::Net;
using namespace System::Net::Sockets;
static const int streamBufferSize = 1000;
public ref class Client
{
private:
literal int dataArraySize = 100;
literal int numberOfLoops = 10000;
Client(){}
public:
static void ReceiveData( Stream^ netStream, Stream^ bufStream )
{
DateTime startTime;
Double networkTime;
Double bufferedTime = 0;
int bytesReceived = 0;
array<Byte>^receivedData = gcnew array<Byte>(dataArraySize);
// Receive data using the NetworkStream.
Console::WriteLine( "Receiving data using NetworkStream." );
startTime = DateTime::Now;
while ( bytesReceived < numberOfLoops * receivedData->Length )
{
bytesReceived += netStream->Read( receivedData, 0, receivedData->Length );
}
networkTime = (DateTime::Now - startTime).TotalSeconds;
Console::WriteLine( "{0} bytes received in {1} seconds.\n", bytesReceived.ToString(), networkTime.ToString( "F1" ) );
// Receive data using the BufferedStream.
Console::WriteLine( "Receiving data using BufferedStream." );
bytesReceived = 0;
startTime = DateTime::Now;
while ( bytesReceived < numberOfLoops * receivedData->Length )
{
bytesReceived += bufStream->Read( receivedData, 0, receivedData->Length );
}
bufferedTime = (DateTime::Now - startTime).TotalSeconds;
Console::WriteLine( "{0} bytes received in {1} seconds.\n", bytesReceived.ToString(), bufferedTime.ToString( "F1" ) );
// Print the ratio of read times.
Console::WriteLine( "Receiving data using the buffered "
"network stream was {0} {1} than using the network "
"stream alone.", (networkTime / bufferedTime).ToString( "P0" ), bufferedTime < networkTime ? (String^)"faster" : "slower" );
}
static void SendData( Stream^ netStream, Stream^ bufStream )
{
DateTime startTime;
Double networkTime;
Double bufferedTime;
// Create random data to send to the server.
array<Byte>^dataToSend = gcnew array<Byte>(dataArraySize);
(gcnew Random)->NextBytes( dataToSend );
// Send the data using the NetworkStream.
Console::WriteLine( "Sending data using NetworkStream." );
startTime = DateTime::Now;
for ( int i = 0; i < numberOfLoops; i++ )
{
netStream->Write( dataToSend, 0, dataToSend->Length );
}
networkTime = (DateTime::Now - startTime).TotalSeconds;
Console::WriteLine( "{0} bytes sent in {1} seconds.\n", (numberOfLoops * dataToSend->Length).ToString(), networkTime.ToString( "F1" ) );
// Send the data using the BufferedStream.
Console::WriteLine( "Sending data using BufferedStream." );
startTime = DateTime::Now;
for ( int i = 0; i < numberOfLoops; i++ )
{
bufStream->Write( dataToSend, 0, dataToSend->Length );
}
bufStream->Flush();
bufferedTime = (DateTime::Now - startTime).TotalSeconds;
Console::WriteLine( "{0} bytes sent in {1} seconds.\n", (numberOfLoops * dataToSend->Length).ToString(), bufferedTime.ToString( "F1" ) );
// Print the ratio of write times.
Console::WriteLine( "Sending data using the buffered "
"network stream was {0} {1} than using the network "
"stream alone.\n", (networkTime / bufferedTime).ToString( "P0" ), bufferedTime < networkTime ? (String^)"faster" : "slower" );
}
};
int main( int argc, char *argv[] )
{
// Check that an argument was specified when the
// program was invoked.
if ( argc == 1 )
{
Console::WriteLine( "Error: The name of the host computer"
" must be specified when the program is invoked." );
return -1;
}
String^ remoteName = gcnew String( argv[ 1 ] );
// Create the underlying socket and connect to the server.
Socket^ clientSocket = gcnew Socket( AddressFamily::InterNetwork,SocketType::Stream,ProtocolType::Tcp );
clientSocket->Connect( gcnew IPEndPoint( Dns::Resolve( remoteName )->AddressList[ 0 ],1800 ) );
Console::WriteLine( "Client is connected.\n" );
// Create a NetworkStream that owns clientSocket and
// then create a BufferedStream on top of the NetworkStream.
NetworkStream^ netStream = gcnew NetworkStream( clientSocket,true );
BufferedStream^ bufStream = gcnew BufferedStream( netStream,streamBufferSize );
try
{
// Check whether the underlying stream supports seeking.
Console::WriteLine( "NetworkStream {0} seeking.\n", bufStream->CanSeek ? (String^)"supports" : "does not support" );
// Send and receive data.
if ( bufStream->CanWrite )
{
Client::SendData( netStream, bufStream );
}
if ( bufStream->CanRead )
{
Client::ReceiveData( netStream, bufStream );
}
}
finally
{
// When bufStream is closed, netStream is in turn closed,
// which in turn shuts down the connection and closes
// clientSocket.
Console::WriteLine( "\nShutting down connection." );
bufStream->Close();
}
}