void GetInterfaceInfomationMyIpAddress()
{
WORD versionRequested;
int wsError;
WSADATA winsockData;
SOCKET s;
DWORD bytesReturned;
char* pAddrString;
SOCKADDR_IN* pAddrInet;
u_long SetFlags;
INTERFACE_INFO localAddr[10]; // Assume there will be no more than 10 IP interfaces
int numLocalAddr;
versionRequested = MAKEWORD(2, 2);
wsError = WSAStartup(versionRequested, &winsockData);
if (wsError)
{
fprintf (stderr, "Startup failed\n");
return;
}
if((s = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0)) == INVALID_SOCKET)
{
fprintf (stderr, "Socket creation failed\n");
WSACleanup();
return;
}
// Enumerate all IP interfaces
fprintf(stderr, "Scanning Interfaces . . .\n\n");
wsError = WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0, &localAddr,
sizeof(localAddr), &bytesReturned, NULL, NULL);
if (wsError == SOCKET_ERROR)
{
fprintf(stderr, "WSAIoctl fails with error %d\n", GetLastError());
closesocket(s);
WSACleanup();
return;
}
closesocket(s);
// Display interface information
numLocalAddr = (bytesReturned/sizeof(INTERFACE_INFO));
for (int i=0; i<numLocalAddr; i++)
{
pAddrInet = (SOCKADDR_IN*)&localAddr[i].iiAddress;
pAddrString = inet_ntoa(pAddrInet->sin_addr);
if (pAddrString)
printf("IP: %s ", pAddrString);
pAddrInet = (SOCKADDR_IN*)&localAddr[i].iiNetmask;
pAddrString = inet_ntoa(pAddrInet->sin_addr);
if (pAddrString)
printf(" SubnetMask: %s ", pAddrString);
pAddrInet = (SOCKADDR_IN*)&localAddr[i].iiBroadcastAddress;
pAddrString = inet_ntoa(pAddrInet->sin_addr);
if (pAddrString)
printf(" Bcast Addr: %s\n", pAddrString);
SetFlags = localAddr[i].iiFlags;
if (SetFlags & IFF_UP)
printf("This interface is up");
if (SetFlags & IFF_BROADCAST)
printf(", broadcasts are supported");
if (SetFlags & IFF_MULTICAST)
printf(", and so are multicasts");
if (SetFlags & IFF_LOOPBACK)
printf(". BTW, this is the loopback interface");
if (SetFlags & IFF_POINTTOPOINT)
printf(". BTW, this is a point-to-point link");
printf("\n\n");
}
WSACleanup();
}
'개발 노트' 카테고리의 다른 글
[C++] 동적으로 메모리 할당하기 (2) | 2008.10.07 |
---|---|
이클립스 에러 - JVM terminated. Exit code = -1 .... (0) | 2008.10.02 |
[C/C++] 현재 시간 얻기 (1) | 2008.09.29 |
OpenSSL 시작하기 (0) | 2008.09.09 |
리눅스에서 메모리 잠그기 (0) | 2008.09.09 |