Advertisement

Is this a bad idea?

Started by October 24, 2000 11:51 AM
5 comments, last by Yanroy 24 years, 2 months ago
I want to get the IP address of a computer on my system (not the standard IP, the way it actually looks on my computer) and break it down into its four parts. Is this a bad way to do it?
    
BYTE IPBreakdown[4];

(unsigned long int)IPBreakdown = IPAddress;

printf("%u.%u.%u.%u", IPBreakdown[0], IPBreakdown[1], IPBreakdown[2], IPBreakdown[4]);
    
%u is probably not the right one to use, but I don''t have the list in front of me and it was the first one that came to mind. --------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

IPBreakdown is of type BYTE *. If you try to assign IPAddress to IPBreakdown, you''d be trying to change the address of an array reference. What you really want to do is (unsigned long int)(*IPBreakdown) = IPAddress.

Though chances are that you got the original IP address in a in_addr structure. In that case you can get to the individual pieces by referencing the bytes directly from that.

Also I think the byte order of your print statement is off.
Advertisement
You''re right about that last comment... just a little typo. I don''t think I am going to fix it. Too much work. But the whole point is that I want to get the IP as it appears normally on my system, not as it is translated by winsock.

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming


You are unique. Just like everybody else.

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com


WSADATA data;
WSAStartup(MAKEWORD(2,0), &data);

char szName[256];
if(gethostname(szName, 256) == SOCKET_ERROR){
printf(_T("Error getting hostname\n"));
return 1;
}

LPHOSTENT pHe = gethostbyname(szName);
if(pHe == NULL){
printf(_T("Error getting host entry\n"));
return 1;
}

LPBYTE pbAddr = (LPBYTE)pHe->h_addr_list[0];

printf(_T("%s: %u.%u.%u.%u\n"), szName, pbAddr[0], pbAddr[1], pbAddr[2], pbAddr[3]);

WSACleanup();
The best way to do this is to use the standard socket function that is provided for it.

inet_ntoa will take an IP address and convert it to a string for you.

-Mike
Yeah, M$ took care of that for you!

    char FAR * inet_ntoa(  struct   in_addr in  );struct in_addr {  union {          struct { u_char s_b1,s_b2,s_b3,s_b4; }   S_un_b;          struct { u_short s_w1,s_w2; }            S_un_w;          u_long                                   S_addr;  } S_un;};    
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
Just to pick nits, Microsoft has nothing to do with it. The socket api is (more or less) standardized. inet_ntoa is a standard function and work on Windows, Linux, whatever.

Winsock is the name for the Microsoft port of the socket api. It contains a bunch of Microsoft specific extensions (all the WSA stuff) but it''s pretty easy to write a network program that runs on Windows and Linux with no changes other than the headers that you include.

-Mike

This topic is closed to new replies.

Advertisement