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

xiaoxiao2021-03-06  55

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

//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

Published on September 02, 2004 7:50 PM

-------------------------------------------------- ----------------

comment

#

Reply: Windows and Linux get a general function of this machine IP address

2004-09-03 12:21 AM

Meteor135

I am dizzy!

is not it:

#ifdef _win32

#include

#include

#pragma comment (Lib, "WS2_32.LIB")

INT getselFiplist (unsigned long ipbuff [], int bufcount) {

INT i, count = 0;

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

{

IPBUF [i] = * (unsigned long *) INADDRS-> h_addr_list [i];

}

}

}

Return count;

}

#ELSE

#include

#include

#include

#include

#include

#include

#include

Int getselFiplist (unsigned long ipbuff [], int bufcount)

{

INT i, count = 0;

Int Sock;

Struct SockAddr_in sin;

Struct ifReq IFR;

SOCK = Socket (AF_INET, SOCK_DGRAM, 0);

IF (SOCK> = 0) //! <0

{

IF (BUFCOUNT> 100) bufcount = 100;

For (i = 0; i

{

Sprintf (ifr.ifr_name, "eth% d", i);

IF (IOCK, Siocgifaddr, & IFR) <0) Break;

:: Memcpy (& SiN, & IFr.IFR_ADDR, SIZEOF (SIN));

IPBUF [count ] = sin.sin_addr.s_addr;

}

Close (SOCK);

}

Return count;

}

#ENDIF

In addition to the function interface specification, others have almost the same, from the code, there is no university!

#

Reply: Windows and Linux get a general function of this machine IP address

2004-09-03 7:15 AM

Perfect

This is the difference between this two functions ...

#

Reply: Windows and Linux get a general function of this machine IP address

2004-09-03 8:11 AM

Vietor

Perfect waste:

If the function is encapsulated in Windows to .DLL in the Linux package to provide a join interface to provide a join interface to become a common function? :-)

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

New Post(0)