inet_ntoa() or not?

This is a discussion on inet_ntoa() or not? within the C Programming forums, part of the General Programming Boards category; Hi all, char temp[50]; printf("Current system IP is: %s", inet_ntoa(*((struct in_addr *)temp))); I have these lines, and it's basically feeding ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    4

    inet_ntoa() or not?

    Hi all,

    char temp[50];

    printf("Current system IP is: %s", inet_ntoa(*((struct in_addr *)temp)));

    I have these lines, and it's basically feeding me with crap IP addresses (one is a 252.x.x.x and another 40.x.x.x) which are both wrong as my IPs are 192.168.x.x and 24.x.x.x (it does the 40.x address second time around in the procedure that I'm calling)

    I'm wondering whether I need to do a htons() but am new to network programming and C in general, so wondering if there is anyone that could help me,

    Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Looks like your passing the wrong thing to inet_ntoa().

    Remember this post? You should, you started it!! Have a look at the code in there and have another go.

    In direct answer though, you need to pass the h_addr element of a struct hostent (I think it's a long). Using a char array as you have done is incorrect. Like I said, look at the code in the other post.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

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