Tuesday, May 28, 2013

Example to connect to a WAN using VB.NET

Example to connect to a WAN using VB.NET

Small example using VB.NET to connect to a remote server

Choosing a programming language to make your program is only one part of a programming project. You also need to choose the network protocol to use.
The most popular network protocol use is IPv4 use in the 3rd network layer from the OSI model. Why?  I don’t know. Maybe ordinary people starting earing keywords such as IP address and thing like that. Maybe the World Wide Web (WWW) is made of email address and http address. Who knows?
Anyway, most people don’t master or even know all the protocol available in the world. Hopefully, Microsoft made a .NET framework with a lot of network protocol all in a single Enumeration: AddressFamily

Here a few sample inside AddressFamily:
  • InterNetwork
  • InterNetworkV6
  • NetBios
  • AppleTalk
  • HyperChannel
  • Unknown
  • Unspecified

If don’t specifiy an AddressFamily, your program will not be able to resolve an address. For example, if you set your program to InterNetwork (IPv4), you socket will expect an IPv4 address when it connect to the endpoint.

Here is an example with the minimum requirement to connect to a server from a client program using: “AddressFamily. InterNetwork”

Imports System
Imports System.Text
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
''' <summary>
''' This is a sample on how to conenct to a server using the
''' minimum stuff in IPv4.
''' Base from an example from MSDN.
''' </summary>
''' <remarks></remarks>
Module Module1

    Sub Main()
        Dim aString As String
        Dim s As Socket = Nothing
        Dim hostEndPoint As IPEndPoint
        Dim hostAddress As IPAddress = Nothing
        Dim conPort As Integer = 80

        ' Get DNS host information.
        Dim hostInfo As IPHostEntry ' = Dns.Resolve(server)
        ' Get the DNS IP addresses associated with the host.
        Dim IPaddresses As IPAddress() '= hostInfo.AddressList
                Console.Write("host to test (exemple : www.yahoo.com) ?  ")
                aString = Console.ReadLine()
                hostInfo = Dns.Resolve(aString)

                Console.Write("port to test (exemple : 80) ? ")
                aString = Console.ReadLine()
                conPort = aString

                IPaddresses = hostInfo.AddressList

                For index = 0 To IPaddresses.Length - 1
                    hostAddress = IPaddresses(index)
                    hostEndPoint = New IPEndPoint(hostAddress, conPort)

                    ' Creates the Socket to send data over a TCP connection.
                    s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

                    ' Connect to the host using its IPEndPoint.

                    If Not s.Connected Then
                        ' Connection failed, try next IPaddress.
                        Console.Write("Unable to connect to host")
                        s = Nothing

                        Console.Write("connected to host !  ")
                    End If

                Next index  ' End of the for loop.

                Console.Write("Quit or Retry? (Q/R)")
                aString = Console.ReadLine()

            Loop Until aString = "Q" Or aString = "q"
        Catch ex As Exception

        End Try

    End Sub

End Module

I use Microsoft Visual Studio 2010 to do all my projects. I suggest you to buy and get the best IDE in the world and the newest one: Microsoft Visual Studio 2012 on Amazon
Download the sample code here: SampleConsoleIPv4.zip

Others related post:

No comments:

Post a Comment