Socket address (convert to string "10.1.4.3")

This is a discussion on Socket address (convert to string "10.1.4.3") within the C Programming forums, part of the General Programming Boards category; Hi all, I am working on a socket program and once I have merged two different programs together, I found ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    28

    Socket address (convert to string "10.1.4.3")

    Hi all,
    I am working on a socket program and once I have merged two different programs together, I found out that there is an inconsistency that I am not able to find the answer to.

    My program takes in an IP in this form 10.1.4.3 however, the other program provides ip addresses in this form (0x0A010403) which translates into 10.1.4.3

    Is there a function available in the socket related libraries that does this for me?

    I want to provide the value 0x0A010403 and it gives me back a string "10.1.4.3"

    Thanks in advance!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    inet_ntoa()
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    28
    Thanks brewbuck!
    Right now, my ip is displaying 3.4.1.10
    I am one step closer to the answer....


    Is there a functionality in the socket library that resembles the ping command?

    Usually, I type ping 10.1.4.3 to see that the ip is taken (ready for me to connect to) then I run my program. Can I do something similar with findhostbyaddr() or something else?

    thanks
    Last edited by v333k; 05-10-2010 at 02:28 PM.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by v333k View Post
    Thanks brewbuck!
    Right now, my ip is displaying 3.4.1.10
    I am one step closer to the answer....
    You need to pass the address in network order, not host order. Pass the address through htonl() before feeding it to inet_ntoa()
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    28
    Great! That fixed it.... although something funny is going on but I worked around it.

    Thanks again brewbuck for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21