WINDOWS and Linux get a universal function of this machine IP address

xiaoxiao2021-03-06  79

//parameter:

IPBUF: The array of list of IP addresses is stored, and the actual size is specified by bufcount.

BUFCOUNT: The number of IP addresses you want to get.

#ifdef _win32

#include #include #pragma Comment (lib, "ws2_32.lib")

#ELSE

#include #include #include #include #include #include #include

#ENDIF

int getselfiplist (unsigned long ipbuf [], int bufcount) {int i, count = 0; #ifdef _WIN32 char hostname [128]; struct hostent * inaddrs; if (gethostname (hostname, 128) == 0) {inaddrs = gethostbyname (Hostname); if (inaddrs) {count = INADDRS-> h_length / sizeof (in_addr); if (count> bufcount) count = bufcount; for (i = 0; i h_addr_list [i];}}} #} #} #} # = socketdr; sock = socket (AF_INET, SOCK_DGRAM, 0); if (SOCK> = 0) //! <0 {IF (BUFCOUNT> 100) bufcount = 100; for (i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-120734.html

New Post(0)