| | |
read ip on xp
![]() |
•
•
Join Date: Jul 2003
Posts: 58
Reputation:
Solved Threads: 0
i found the code to read local ip but i was wondering could it be any smaller then this.
#include <winsock.h>
#include <wsipx.h>
#include <wsnwlink.h>
#include <stdio.h>
int main()
{
////////////////
// Initialize windows sockets API.
//
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
if (WSAStartup(wVersionRequested, &wsaData)) {
printf("WSAStartup failed %s\n", WSAGetLastError());
return -1;
}
//////////////////
// Get host name.
//
char hostname[256];
int res = gethostname(hostname, sizeof(hostname));
if (res != 0) {
printf("Error: %u\n", WSAGetLastError());
return -1;
}
printf("hostname=%s\n", hostname);
////////////////
// Get host info for hostname.
//
hostent* pHostent = gethostbyname(hostname);
if (pHostent==NULL) {
printf("Error: %u\n", WSAGetLastError());
return -1;
}
//////////////////
// Parse the hostent information returned
//
hostent& he = *pHostent;
printf("name=%s\naliases=%s\naddrtype=%d\nlength=%d\n",
he.h_name, he.h_aliases, he.h_addrtype, he.h_length);
sockaddr_in sa;
for (int nAdapter=0; he.h_addr_list[nAdapter]; nAdapter++) {
memcpy ( &sa.sin_addr.s_addr, he.h_addr_list[nAdapter],he.h_length);
// Output the machines IP Address.
printf("Address: %s\n", inet_ntoa(sa.sin_addr)); // display as string
}
//////////////////
// Terminate windows sockets API
//
WSACleanup();
return 0;
}
#include <winsock.h>
#include <wsipx.h>
#include <wsnwlink.h>
#include <stdio.h>
int main()
{
////////////////
// Initialize windows sockets API.
//
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
if (WSAStartup(wVersionRequested, &wsaData)) {
printf("WSAStartup failed %s\n", WSAGetLastError());
return -1;
}
//////////////////
// Get host name.
//
char hostname[256];
int res = gethostname(hostname, sizeof(hostname));
if (res != 0) {
printf("Error: %u\n", WSAGetLastError());
return -1;
}
printf("hostname=%s\n", hostname);
////////////////
// Get host info for hostname.
//
hostent* pHostent = gethostbyname(hostname);
if (pHostent==NULL) {
printf("Error: %u\n", WSAGetLastError());
return -1;
}
//////////////////
// Parse the hostent information returned
//
hostent& he = *pHostent;
printf("name=%s\naliases=%s\naddrtype=%d\nlength=%d\n",
he.h_name, he.h_aliases, he.h_addrtype, he.h_length);
sockaddr_in sa;
for (int nAdapter=0; he.h_addr_list[nAdapter]; nAdapter++) {
memcpy ( &sa.sin_addr.s_addr, he.h_addr_list[nAdapter],he.h_length);
// Output the machines IP Address.
printf("Address: %s\n", inet_ntoa(sa.sin_addr)); // display as string
}
//////////////////
// Terminate windows sockets API
//
WSACleanup();
return 0;
}
![]() |
Similar Threads
- read to end of line problem (C)
- Read/write to same file > once, Help (C++)
- Cant read IO.SYS (Windows NT / 2000 / XP)
- cd rom does not read cd (Storage)
- About CD(Compact Disk) Writing (C++)
Other Threads in the C Forum
- Previous Thread: for loops
- Next Thread: plzzz help! i did the work need
| Thread Tools | Search this Thread |
adobe ansi api array asterisks binarysearch calculate centimeter changingto char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory fflush file fork forloop frequency givemetehcodez global grade gtkgcurlcompiling hacking highest histogram homework i/o inches infiniteloop input interest iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match meter microsoft mysql number open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming socketprogramming stack standard string suggestions systemcall threads turboc unix user variable voidmain() wab win32api windows.h windowsapi





