Host name to IP address

Hi, I did develop at simple code to access data remotely using IP address. The issue is every time my remote unit power reset the IP address changes. I did registered with dyn can created host name for this purpose.

How do i convert this host name to ip address to access the my remote unit. Please do advice. My program in VB.net
Rama TitoProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

clockwatcherCommented:
Can you post the code that you're using to set up your connection?  I can't think of any reason that your typical connection setup would require the IP address rather than the name.  Have you tried simply setting up your connection with the hostname rather than the IP?

But if you really need the IP address, you can get it from a call to Dns.GetHostEntry method.

http://msdn.microsoft.com/en-us/library/system.net.dns.gethostentry.aspx

An example of its use:

http://msdn.microsoft.com/en-us/library/ms143998.aspx
0
Rama TitoProgrammerAuthor Commented:
I did tryout by entering host name, its doesn't work. Please do refer to my code attachment and do advice. Thank you
 Private Sub btnLANConn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLANConn.Click
        Try
            TextBox1.AppendText("Contacting PLC Webserver: " & IPAddr.Text & ":" & PortNo.Text & vbCrLf)
            'Dim client1 As New TcpClient(IPAddr.Text, PortNo.Text)
            client1 = New TcpClient(IPAddr.Text, PortNo.Text)
            'Dim stream1 As NetworkStream = client1.GetStream()
            stream1 = client1.GetStream()
            'Dim command, response As String
            stream1.ReadTimeout = 1000

            Dim Message = ConnectToServer(stream1, username.Text, password.Text)
            TextBox1.AppendText("Received:" & Message & vbCrLf)

            If Message.Length >= 4 Then
                If (Message.Substring(0, 4) = "OK-1" Or Message.Substring(0, 4) = "OK-2" Or Message.Substring(0, 4) = "OK-3") Then
                    command = appendFCS(1, "RI00")         ' Enter your hostlink commands for testing
                    response = sendCommand(stream1, command)
                    TextBox1.AppendText("Sent:" & command & vbCrLf)
                    TextBox1.AppendText("Received:" & response & vbCrLf)

                    command = appendFCS(1, "IR")         ' Enter your hostlink commands for testing
                    response = sendCommand(stream1, command)
                    TextBox1.AppendText("Sent:" & command & vbCrLf)
                    TextBox1.AppendText("Received:" & response & vbCrLf & vbCrLf)
                    LanActive = True
                    picSerialStatus.Image = My.Resources.Resources.CDR8_status_on
                    btnDisconnect.Enabled = True
                End If
            End If
            If LanActive Then
                'command = appendFCS(1, txtTesting.Text)         ' Enter your hostlink commands for testing
                response = sendCommand(stream1, "IR*").Trim
                TextBox1.AppendText("Sent:" & command & vbCrLf)
                TextBox1.AppendText("Received:" & response & vbCrLf)
                PLC_ID = Mid(response, 3, Len(response) - 3)
                TextBox1.AppendText("PLC ID - > " & PLC_ID & vbCrLf)


                getxlFile()
                xlON = TestEXCEL()
                btnxlPauseLogging.Enabled = False
                btnxlStartlogging.Enabled = True
                btnxlStopLogging.Enabled = False

                btnLANConn.Enabled = False
            Else
                TextBox1.AppendText("Sent:" & " Socket Disconnected" & vbCrLf)
                TextBox1.AppendText("Received:" & " Socket Disconnected" & vbCrLf)
            End If

        Catch ex As SocketException
            Console.WriteLine("SocketException: {0}", ex)
            TextBox1.AppendText(ex.ToString)
            LanActive = False
            'If LANFlag = True Then
            'AUTOLANConnections()
            'End If
        End Try
    End Sub

    Public Function ConnectToServer(ByRef stream As [NetworkStream], _
                                  ByVal usr As [String], ByVal pw As [String]) As String
        Try
            ' Translate the login data into a Byte array.
            Dim plcnet = New FServerSupport.PLCNetwork

            Dim str1 As String = "GET /TLcomm" & vbCrLf & usr & vbCrLf & plcnet.encrypt(pw) & vbCrLf
            Dim OutData As [Byte]() = System.Text.Encoding.ASCII.GetBytes(str1)
            Dim InData = New [Byte](127) {}

            ' Send the message to the connected TcpServer. 
            stream.Write(OutData, 0, OutData.Length)
            Dim responseData As [String]
            Dim byteCount As Int32 = stream.Read(InData, 0, InData.Length)
            responseData = System.Text.Encoding.ASCII.GetString(InData, 0, byteCount)
            If stream.DataAvailable Then
                byteCount = stream.Read(InData, 0, InData.Length) ' clear input buffer
                Dim responseData2 = System.Text.Encoding.ASCII.GetString(InData, 0, byteCount)
            End If
            ConnectToServer = responseData

            Exit Function
        Catch e As System.IO.IOException
            Dim errMsg = "No response from server"
            Console.WriteLine(errMsg)
            ConnectToServer = errMsg
            Exit Function
        End Try
        ConnectToServer = ""
    End Function

Open in new window

0
clockwatcherCommented:
TcpClient accepts a host name.  So there's likely something else wrong (e.g., your client machine is having problems with host name resolution or your host name resolves to multiple IPs and is listening on a specific one) .

Imports System
Imports System.Net.Sockets

Public Module modmain

    Sub Main()
        Dim client as new TcpClient("www.google.com", 80)

        Dim buffer as Byte() = System.Text.Encoding.ASCII.GetBytes("GET / HTTP/1.0" & vbCrlf & vbCrlf)
        Dim stream as NetworkStream = client.GetStream()
        stream.Write(buffer, 0, buffer.Length)

        buffer = New [Byte](256) {}
        Dim response as String
        Dim bytesRead as Int32

        Do 
           bytesRead = stream.Read(buffer, 0, buffer.Length)
           if bytesRead > 0 then
              response = response & System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead)
           end if

        Loop while bytesRead > 0

        stream.Close()
        client.Close()

        Console.WriteLine(response)

    End Sub

End Module

Open in new window


What error are you getting when you try connecting via host name?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rama TitoProgrammerAuthor Commented:
Yes, I did manage to resolve by getting the class on tcpclient from ms support.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.