IP address network order to string?

This is a discussion on IP address network order to string? within the Networking/Device Communication forums, part of the General Programming Boards category; I'm trying to convert an IP address in network order into a dotted decimal string... I cannot seem to figure ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    IP address network order to string?

    I'm trying to convert an IP address in network order into a dotted decimal string... I cannot seem to figure out how to do this! I've been looking for AGES. I've tried the following ways but they both do absolutely nothing :/

    Code:
    #include <iostream>
    #include <Winsock2.h>
    #include <Ws2tcpip.h>
    
    using namespace std;
    
    int main() {
    	sockaddr_in serv;
    	memset(&serv, sizeof(serv), 0);
    
    	serv.sin_addr.S_un.S_addr = inet_addr("192.168.1.1");
    	serv.sin_family = AF_INET;
    	serv.sin_port = htons(22);
    
    	char ip[16] = {0};
    
    	WSAAddressToString((sockaddr *)&serv, sizeof(serv), 0, ip, (LPDWORD)sizeof(ip));
    	printf("IP: %s\n", ip);
    	getnameinfo((sockaddr *)&serv, sizeof(serv), 0, 0, ip, sizeof(ip), 0);
    	printf("IP: %s\n", ip);
    }
    Output:

    Code:
    IP: 
    IP:
    I'm using Windows XP which is why I'm not using inetNtop or whatever it is... (It's Vista only apparently). Compiler is Visual C++ Express. Thanks.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    The function you are looking for is inet_ntop()
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 10:20 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 02:26 AM

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