Solved

Host name to IP address

Posted on 2013-05-30
4
388 Views
Last Modified: 2013-05-31
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
0
Comment
Question by:TITO2009
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 39206870
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
 

Author Comment

by:TITO2009
ID: 39206982
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
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 39208171
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
 

Author Closing Comment

by:TITO2009
ID: 39212251
Yes, I did manage to resolve by getting the class on tcpclient from ms support.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now