-
December 8th, 2002, 01:46 PM
#1
method to get IP address of the host
hi all,
I am new to VC++ programming . Is there any way to get
information about the localhost (i.e.,its IP address , hostname) ?
In general , how network related issues are handled in VC++?
Do help this novice user.
Ashwini
-
December 9th, 2002, 01:57 AM
#2
The following example will get up to ten assigned IP addresses...
Code:
#include <winsock2.h>
// Add 'ws2_32.lib' to your linker options
WSADATA WSAData;
// Initialize winsock dll
if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
// Error handling
// Get local host name
char szHostName[128] = "";
if(::gethostname(szHostName, sizeof(szHostName)))
// Error handling -> call 'WSAGetLastError()'
// Get local IP addresses
struct sockaddr_in SocketAddress;
struct hostent *pHost = 0;
pHost = ::gethostbyname(szHostName);
if(!pHost)
// Error handling -> call 'WSAGetLastError()'
char aszIPAddresses[10][16] // maximum of ten IP addresses
for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
{
memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
}
// Cleanup
WSACleanup();
Last edited by Andreas Masur; December 9th, 2002 at 02:00 AM.
-
December 9th, 2002, 02:27 AM
#3
In the IpHlpApi there are two not so difficult functions -GetAdaptersInfo, GetNetworkParams.
Good luck
-
December 9th, 2002, 12:48 PM
#4
Using Netbios to obtain an IP Address
You can use the NetBios API to obtain the IP Address...
/**
*************************************************
* Attempt to obtain a non-zero MAC (Media Access
* Control) from the * network cards by issuing
* net bios commands.
* We need to link to Netapi32.lib for this.
*
* @methodName McbGetMACAddress
*
* @param char uMAC[6]
*
* @return bool
*
* @exception none
*
* @author Martyn C Brown
*
* @changeHistory
* 5th October 2001 - (V1.0) Creation (MCB)
*************************************************
*/
bool McbGetMACAddress(unsigned char uMAC[6])
{
bool bResult = false;
typedef struct tagMcbAdaptor
{
ADAPTER_STATUS adaptStatus;
NAME_BUFFER szName[30];
} McbAdaptor;
McbAdaptor adapter;
unsigned char * lpMAC =
adapter.adaptStatus.adapter_address;
/*
*********************************************
* Prepare network control block for
* enumeration command
*********************************************
*/
NCB ncb;
LANA_ENUM lenum;
::memset(&ncb, 0, sizeof(ncb));
ncb.ncb_command = NCBENUM;
ncb.ncb_buffer = (UCHAR *)&lenum;
ncb.ncb_length = sizeof(lenum);
/*
*********************************************
* Call netbios to enumerate LAN adapters
* (LANA).
*********************************************
*/
UCHAR uRetCode = Netbios(&ncb);
/*
*********************************************
* Enmerate LAN adapters until we find a non
* zero MAC address.
*********************************************
*/
for(int i=0; i<lenum.length && !bResult; i++)
{
/*
*****************************************
* Prepare network control block for LAN
* adapter reset. An adapter must be
* reset before it can accept any other
* NCB command that specifies the same
* number in the ncb_lana_num member.
*****************************************
*/
::memset(&ncb, 0, sizeof(ncb));
ncb.ncb_command = NCBRESET;
ncb.ncb_lana_num = lenum.lana[i];
/*
*****************************************
* Call netbios with the reset command
*****************************************
*/
uRetCode = Netbios(&ncb);
/*
*****************************************
* Prepare network control block to
* retrieve the LAN adapter status.
*****************************************
*/
::memset(&ncb, 0, sizeof (ncb) );
ncb.ncb_command = NCBASTAT;
ncb.ncb_lana_num = lenum.lana[i];
::strcpy((char*)ncb.ncb_callname,
"* ");
ncb.ncb_buffer = (unsigned char*)
&adapter;
ncb.ncb_length = sizeof(adapter);
::memset(lpMAC, 0, 6);
/*
*****************************************
* Call netbios to obtain MAC address
*****************************************
*/
uRetCode = Netbios(&ncb);
/*
*****************************************
* If the MAC address is non-zero
*****************************************
*/
if (::memcmp(lpMAC, "\x0\x0\x0\x0\x0\x0",
6) != 0)
{
/*
*************************************
* Take a copy and set the result to
* indicate success.
*************************************
*/
::memcpy(uMAC, lpMAC, 6);
bResult = true;
}
}
return bResult;
}/* McbGetMACAddress */
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|