.Net: Parse IP address String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • romcab
    New Member
    • Sep 2007
    • 108

    #1

    .Net: Parse IP address String

    Hi guys,

    just want to ask how can I convert an IP address that is declared as String to type IPAddress? The reason why I come up with String IP address is because it came from a file. the connect method of socket class need IPaddress or host.

    Hope you can help me...
    Last edited by Shashi Sadasivan; Nov 21 '07, 06:03 AM. Reason: changed topic title. Old Title : "Socket programming"
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    IPAddress ipAddr = IPAddress.Parse (strHostName);

    Comment

    • Shashi Sadasivan
      Recognized Expert Top Contributor
      • Aug 2007
      • 1435

      #3
      Hi romcab

      I changed your Topic title to make it more meaningful and appropriate to your query

      Please remember to provide a meaningful Title for any threads started (Please Use Appropriate Titles for New Threads!). This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

      - Shashi

      Comment

      • romcab
        New Member
        • Sep 2007
        • 108

        #4
        Hi..

        I dont have the hostname, I only have an Ip address but it's on type String since I get it on a textfile. My question is how can I translate the IpAdd of type String to type IPAdress..

        Thanks

        Originally posted by mzmishra
        IPAddress ipAddr = IPAddress.Parse (strHostName);

        Comment

        • mzmishra
          Recognized Expert Contributor
          • Aug 2007
          • 390

          #5
          IPAddress.Parse Method Converts an IP address string to an IPAddress instance.
          A string that contains an IP address in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.

          Comment

          • romcab
            New Member
            • Sep 2007
            • 108

            #6
            Originally posted by mzmishra
            IPAddress.Parse Method Converts an IP address string to an IPAddress instance.
            A string that contains an IP address in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.
            my mistake...Your right. I follow your advice but I think I still have an error...Below is my code, I passed Ip "192.168.20.125 " which is my pc ip address but after I looked at myIpAdd value i didnt see the 192.168.20.125. Is there anything wrong?


            [CODE=vbnet]Private Sub SendData(ByVal IpAdd As String, ByVal sData As String)

            Dim myIpAdd As IPAddress

            'create a socket
            Dim isocket As Socket
            isocket = New Socket(AddressF amily.InterNetw ork, SocketType.Stre am, ProtocolType.Tc p)

            myIpAdd = IPAddress.Parse (IpAdd)

            'resolve IPAddress
            'myIpAdd = Dns.GetHostAddr esses(IpAdd)

            'connect to remote host
            Try
            isocket.Connect (myIpAdd, 80)

            Catch se As SocketException
            MsgBox("Socket Exception: {0}", se.SocketErrorC ode)

            Catch ex As Exception
            MsgBox("UnExpec ted exception : {0}", ex.ToString)

            End Try

            End Sub[/CODE]
            Last edited by Shashi Sadasivan; Nov 21 '07, 06:29 AM. Reason: adding code tags to the code

            Comment

            • Shashi Sadasivan
              Recognized Expert Top Contributor
              • Aug 2007
              • 1435

              #7
              Hi,
              is there any exception that is being thrown?

              If you want to check use the myIpAdd.ToStrin g() to check if the IPAddress is correct.
              It should show the same (worked for me)

              Comment

              • romcab
                New Member
                • Sep 2007
                • 108

                #8
                Originally posted by Shashi Sadasivan
                Hi,
                is there any exception that is being thrown?

                If you want to check use the myIpAdd.ToStrin g() to check if the IPAddress is correct.
                It should show the same (worked for me)

                Hi there,

                Yes it is the same. The Address member is obsolete based on msdn. Anyway, is there a way how can I typecast String type to a Byte() ? Sorry, this is my first time to use VB that's why i'm having this weird/stupid questions.

                Thanks

                Comment

                • Shashi Sadasivan
                  Recognized Expert Top Contributor
                  • Aug 2007
                  • 1435

                  #9
                  if you use the Parse method then it creates the class for you with the required ip address.
                  Its only the Address Property which is obsolete.

                  Your IPAddress class object is fine otherwise

                  is there an exception due to which you are worried with this object?

                  Comment

                  • mzmishra
                    Recognized Expert Contributor
                    • Aug 2007
                    • 390

                    #10
                    http://www.chilkatsoft .com/faq/DotNetStrToByte s.html

                    Comment

                    • romcab
                      New Member
                      • Sep 2007
                      • 108

                      #11
                      Originally posted by Shashi Sadasivan
                      if you use the Parse method then it creates the class for you with the required ip address.
                      Its only the Address Property which is obsolete.

                      Your IPAddress class object is fine otherwise

                      is there an exception due to which you are worried with this object?
                      I'm receiving Socket exception when I changed the port number.

                      Comment

                      • romcab
                        New Member
                        • Sep 2007
                        • 108

                        #12
                        Originally posted by mzmishra
                        http://www.chilkatsoft .com/faq/DotNetStrToByte s.html

                        Thanks...I have one last question.. I'm having hard time to add double quote to a string.
                        example:
                        stemp1 = "<" + stemp + ">" = <stemp>

                        but how about for double quote?
                        output should be: "stemp"?

                        I found one answer but it only applies when value of string is constant.
                        example
                        stemp = ""Hello"" = "Hello"

                        Comment

                        • Shashi Sadasivan
                          Recognized Expert Top Contributor
                          • Aug 2007
                          • 1435

                          #13
                          the \ is used as an escape character

                          string s = "\"hello\"" ;
                          that will give the output "hello" and not hello

                          Comment

                          • romcab
                            New Member
                            • Sep 2007
                            • 108

                            #14
                            Originally posted by Shashi Sadasivan
                            the \ is used as an escape character

                            string s = "\"hello\"" ;
                            that will give the output "hello" and not hello

                            Hi tnx for all your help. I was able to send the data but byte per byte. I would like to try sending the whole string but there is not connect overload that can accept string as Input. There is another overload method which uses IList but I cannot understand. Can you help me guys how to use it?

                            Tnx again...

                            Comment

                            • romcab
                              New Member
                              • Sep 2007
                              • 108

                              #15
                              Originally posted by Shashi Sadasivan
                              the \ is used as an escape character

                              string s = "\"hello\"" ;
                              that will give the output "hello" and not hello

                              Hi...

                              It's not working when hello is a variable...It only works for constant...

                              Comment

                              Working...