//for windows
#include <windows.h>
#include <winsock.h>
#include <stdio.h>int main()
{
WSADATA wd;
char szHostName[255]; WSAStartup(MAKEWORD(1,0), &wd); gethostname(szHostName, sizeof szHostName);
         PHOSTENT phe = gethostbyname(szHostName); printf("IP addr = %s\n", inet_ntoa(*(IN_ADDR*)phe->h_addr)); WSACleanup(); return 0;
}
_____________________________________________________________
//for linux
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>#define HOSTENT struct hostentvoid getMyIPAddress(char* IPaddress);int main(void) {
        char buf[20];
        getMyIPAddress(buf);
        puts(buf);
        return 0;
}void getMyIPAddress(char* IPaddress)
{
        char hostname[255]; 
        HOSTENT* phe;
        
        IPaddress[0] = 0;
        gethostname(hostname, sizeof(hostname));
        phe = gethostbyname(hostname);
        if (phe) 
                strcpy(IPaddress, inet_ntoa(*(struct in_addr*)phe->h_addr));
}
绝对没问题
好久没有分了
你不会不给分吧
有什么不明白的可以问我
[email protected]