diddom 发表于 2012-5-13 16:20:31

Get Public IP

本帖最后由 diddom 于 2012-5-14 00:26 编辑

// GetPublicIP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>//這個必須在<windows.h>前面
#include <windows.h>
//#include <winsock2.h>
#include <ws2tcpip.h>


#pragma comment(lib,"ws2_32.lib")

unsigned long dwProtocol=IPPROTO_IP;
unsigned long dwInterface=0;
//unsigned long dwIoControlCode=SIO_RCVALL;

#define HI_WORD(byte)    (((byte) >> 4) & 0x0F)
#define LO_WORD(byte)    ((byte) & 0x0F)


int EnumNetworkInterfaces()
{

        SOCKET hSocket;
        SOCKET_ADDRESS_LIST *pstlist=NULL;

        int i;
        char szBuf;
        unsigned long dwBytesRet;
       

        hSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);

        if(hSocket==SOCKET_ERROR)
        {
                printf("socket() failed!error code:%d\n",WSAGetLastError());
                return 0;
        }

        if(WSAIoctl(hSocket,SIO_ADDRESS_LIST_QUERY,NULL,0,szBuf,4096,&dwBytesRet,NULL,NULL)==SOCKET_ERROR)
        {
                printf("WSAIoctl(SIO_ADDRESS_LIST_QUERY) failed!Error code:%d\n",WSAGetLastError());
                return 0;
        }

        pstlist=(SOCKET_ADDRESS_LIST *)szBuf;
       
        for(i=0; i<pstlist->iAddressCount; i++)
        {
                printf(" %d%s\n",i,inet_ntoa(((SOCKADDR_IN *)pstlist->Address.lpSockaddr)->sin_addr));;
        }

        printf("%d\n",hSocket);
        system("pause");
        closesocket(hSocket);
        return 0;

}



int main(int argc, char* argv[])
{

        WSADATA wsaData;
        WSAStartup(0x0202, &wsaData);
        EnumNetworkInterfaces();
        WSACleanup();

        return 0;
}
页: [1]
查看完整版本: Get Public IP