kode yard
all about C++ and PHP
Thursday, October 1, 2009
get IP address in C(windows)
I have written this code snippet in my 2nd year which finds the ip address of windows machine. This is a simple code. Just go throw this...
<br />#include <stdio.h><br />#include <WinSock.h><br />#include <stdlib.h><br />#pragma comment (lib, "wsock32.lib")<br /><br />int main()<br />{<br /> WORD wVersionRequested;<br /> WSADATA wsaData;<br /> char caHostname[255+1];<br /> PHOSTENT hostData;<br /> char *pIP;<br /> int i=0;<br /><br /> wVersionRequested=MAKEWORD(1,1);<br /> if(WSAStartup(wVersionRequested, &wsaData)!=0)<br /> {<br /> printf("Error......code is not compartible with platform\n");<br /> exit(EXIT_FAILURE);<br /> }<br /> if(gethostname(caHostname, sizeof caHostname)!=0)<br /> printf("Error......Hostname not found\n");<br /> printf("HOSTNAME : %s\n",caHostname);<br /> if((hostData=gethostbyname(caHostname))==NULL)<br /> {<br /> printf("Error......code is not compartible with platform\n");<br /> exit(EXIT_FAILURE);<br /> }<br /> while(hostData->h_addr_list[i])<br /> {<br /> pIP = inet_ntoa(*(struct in_addr *)hostData->h_addr_list[i]);<br /><br /> printf("IP ADDRESS %d: %s\n", ++i, pIP);<br /> }<br /><br /> return 0;<br />}<br />
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment