
Accessing the IP address of multiple network adapters using the MS Winsock Control
Quote:
> Hello,
> How can I access ALL the IP addresses for the different network adapters
> I have installed? All I can currently access is the first one bound.
these programs will work in c and you can call the api from vb, but you
will have to do some work to unravel the array of struct in_addr's.
second program requires winsock 2.2
#include <stdio.h>
#pragma warning(disable:4115 4201 4214 4514)
#include <winsock.h>
#pragma warning(default:4115 4201 4214 /*4514*/)
#define IPFAIL(x) (x==SOCKET_ERROR)
#define BAD_SOCKET(x) (x==INVALID_SOCKET)
static WSADATA WSAData;
void findIPAddresses(char *name)
{
struct hostent *host;
struct in_addr *ip;
printf("ip address(es) for %s: ",name);
if((host=gethostbyname(name))==NULL)
{ WSACleanup(); fprintf(stderr,"gethostbyname fails");
exit(1); }
while((ip=(struct in_addr *)*host->h_addr_list++)!=NULL)
printf("%s ",inet_ntoa(*ip));
printf("\n");
}
int main(int argc,char *argv[])
{
char name[BUFSIZ],*hostName=name;
int I;
if(WSAStartup(0x0101,&WSAData))
{ WSACleanup(); fprintf(stderr,"WSAStartup fails");
exit(1); }
if(argc==1)
{
if(IPFAIL(gethostname(name,BUFSIZ-1)))
{ WSACleanup(); fprintf(stderr,"gethostname fails");
exit(1); }
printf("gethostname returns: %s\n",name);
findIPAddresses(name);
}
else for(I=1;I<argc;I++)
findIPAddresses(argv[I]);
(void)getchar();
return WSACleanup();
}
#include <iostream.h>
#include <winsock2.h>
#include <ws2tcpip.h>
int doit()
{
SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
if (sd == SOCKET_ERROR) {
cerr << "Failed to get a socket. Error " <<
WSAGetLastError() << endl;
return 1;
}
INTERFACE_INFO InterfaceList[20];
unsigned long nBytesReturned;
if (WSAIoctl(sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList,
sizeof(InterfaceList), &nBytesReturned, 0, 0) ==
SOCKET_ERROR) {
cerr << "Failed calling WSAIoctl: error " <<
WSAGetLastError() << endl;
return 1;
}
int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
cout << "There are " << nNumInterfaces << " interfaces:" << endl;
for (int I = 0; I < nNumInterfaces; ++I) {
cout << endl;
sockaddr_in* pAddress;
pAddress = (sockaddr_in*)&(InterfaceList[I].iiAddress);
cout << " " << inet_ntoa(pAddress->sin_addr);
pAddress = (sockaddr_in*)&(InterfaceList[I].iiBroadcastAddress);
cout << " has bcast " << inet_ntoa(pAddress->sin_addr);
pAddress = (sockaddr_in*)&(InterfaceList[I].iiNetmask);
cout << " and netmask " << inet_ntoa(pAddress->sin_addr) << endl;
cout << " Iface is ";
u_long nFlags = InterfaceList[I].iiFlags;
if (nFlags & IFF_UP) cout << "up";
else cout << "down";
if (nFlags & IFF_POINTTOPOINT) cout << ", is point-to-point";
if (nFlags & IFF_LOOPBACK) cout << ", is a loopback iface";
cout << ", and can do: ";
if (nFlags & IFF_BROADCAST) cout << "bcast ";
if (nFlags & IFF_MULTICAST) cout << "multicast ";
cout << endl;
}
return 0;
Quote:
}
int main()
{
WSADATA WinsockData;
if (WSAStartup(MAKEWORD(2, 2), &WinsockData) != 0) {
cerr << "Failed to find Winsock 2.2!" << endl;
return 2;
}
int nRetVal = doit();
WSACleanup();
return nRetVal;
Quote:
}
hth
--
Ray (will hack java for food) http://home.pacbell.net/rtayek/
hate Spam? http://www.blighty.com/products/spade/