Imports System Imports System.Net Imports System.Net.Sockets ' This sample demonstrates how to use the NetworkStream class to ' perform IO between 2 sockets. When using TCP Sockets you have to ' build a listening Socket application that will receive a connection ' from another application. In Socket terminology the application that ' receives a connection is know as a server and the peer that connects ' to a server is know as a client. ' ' The focus of this sample is not on Sockets but on network streams. We ' call this application a NetworkStreamServer because it is designed to ' receive a Socket connection and perform IO using a NetworkStream. ' ' To run this sample, simply just run the program without parameters and ' it will listen for a client connection on TCP port 5150. If you want to ' use a different port than 5150 then you can optionally supply a command ' line parameter "/port " and the listening socket will use ' your port instead. Module Module1 ' The main entry point for the application. Sub Main() ' The following are default values, change accordingly 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 = 0 To args.Length - 1 Try If 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: Receiver [/port ]") Exit Sub End Try Next Dim ServerSocket As Socket = Nothing Dim ListeningSocket As Socket = Nothing Try ' Setup a listening Socket to await a connection from a peer socket. ListeningSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP) Dim ListeningEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Parse(ServerName), Port) ListeningSocket.Bind(ListeningEndPoint) Console.WriteLine("Bind() is OK...") ListeningSocket.Listen(5) Console.WriteLine("Listen is OK...") Console.WriteLine("Awaiting a TCP connection on IP: " _ + ListeningEndPoint.Address.ToString() _ + " Port: " _ + ListeningEndPoint.Port.ToString() _ + "...") Console.WriteLine("Please run your sender program, I'm waiting...") ServerSocket = ListeningSocket.Accept() Console.WriteLine("Accept() is OK") Console.WriteLine("Received a connection - awaiting data...") Catch e As SocketException Console.WriteLine("Failure to create Sockets: " + e.Message) Exit Sub Finally ' Close the listening socket - we do not plan to handle any ' additional connections. ListeningSocket.Close() End Try ' Let's create a network stream to communicate over the connected Socket. Dim ServerNetworkStream As NetworkStream = Nothing Try Try ' Setup a network stream on the server Socket Console.WriteLine("Instantiate a NetworkStream object...") ServerNetworkStream = New NetworkStream(ServerSocket, True) Catch e As Exception Throw New Exception("Failed to create a Network Stream with error: " + e.Message) End Try Try Dim BytesRead As Integer = 0 Dim ReadBuffer(4096) As Byte Console.WriteLine("Reading...") Do BytesRead = ServerNetworkStream.Read(ReadBuffer, 0, ReadBuffer.GetUpperBound(0)) Console.WriteLine("We read " + BytesRead.ToString() + " byte(s) from a peer socket.") For i = 0 To BytesRead - 1 Console.WriteLine("The byte #" + i.ToString() + " contains " + ReadBuffer(i).ToString()) Next Loop Until BytesRead > 0 Catch e As Exception Throw New Exception("Failed to read from a network stream with error: " + e.Message) End Try Catch e As Exception Console.WriteLine(e.Message) Finally ' We are finished with the NetworkStream, so we will close it. ' Note: the ServerSocket will be closed by the NetworkStream. ServerNetworkStream.Close() End Try End Sub End Module