当前位置: 技术问答>linux和unix
如何获得本地机器IP问题?
来源: 互联网 发布时间:2016-04-06
本文导语: #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int get_host_info(string &local_ip,string &local_m...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int get_host_info(string &local_ip,string &local_mask)
{
struct sockaddr_in *my_ip;
struct sockaddr_in *addr;
struct sockaddr_in myip;
my_ip = &myip;
struct ifreq ifr;
int sock;
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) sin_addr = ((struct sockaddr_in *)(&ifr.ifr_addr))->sin_addr;
local_ip = inet_ntoa(my_ip->sin_addr);
//取本机掩码
if( ioctl( sock, SIOCGIFNETMASK, &ifr) == -1 ){
perror("[-] ioctl");
return -1;
}
addr = (struct sockaddr_in *) & (ifr.ifr_addr);
local_mask = inet_ntoa( addr->sin_addr);
close(sock);
return 0;
}
void main()
{
string local_ip, string local_mask;
int ret =get_host_info(local_ip,local_mask)
printf("ip = %sn",local_ip);
printf("ip mask = %sn",local_mask);
while(1)pause();
}
gdbtest.c:12:18: string: 没有那个文件或目录
gdbtest.c:20: parse error before "namespace"
gdbtest.c:20: warning: data definition has no type or storage class
gdbtest.c:21: parse error before '&' token
gdbtest.c:21: parse error before '&' token
gdbtest.c: In function `get_host_info':
gdbtest.c:29: parse error before "sin_addr"
gdbtest.c:38: `local_ip' undeclared (first use in this function)
gdbtest.c:38: (Each undeclared identifier is reported only once
gdbtest.c:38: for each function it appears in.)
gdbtest.c:38: `local_mask' undeclared (first use in this function)
gdbtest.c: In function `main':
gdbtest.c:46: `string' undeclared (first use in this function)
gdbtest.c:46: parse error before "local_ip"
gdbtest.c:47: `local_ip' undeclared (first use in this function)
gdbtest.c:47: `local_mask' undeclared (first use in this function)
gdbtest.c:48: parse error before "printf"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int get_host_info(string &local_ip,string &local_mask)
{
struct sockaddr_in *my_ip;
struct sockaddr_in *addr;
struct sockaddr_in myip;
my_ip = &myip;
struct ifreq ifr;
int sock;
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) sin_addr = ((struct sockaddr_in *)(&ifr.ifr_addr))->sin_addr;
local_ip = inet_ntoa(my_ip->sin_addr);
//取本机掩码
if( ioctl( sock, SIOCGIFNETMASK, &ifr) == -1 ){
perror("[-] ioctl");
return -1;
}
addr = (struct sockaddr_in *) & (ifr.ifr_addr);
local_mask = inet_ntoa( addr->sin_addr);
close(sock);
return 0;
}
void main()
{
string local_ip, string local_mask;
int ret =get_host_info(local_ip,local_mask)
printf("ip = %sn",local_ip);
printf("ip mask = %sn",local_mask);
while(1)pause();
}
gdbtest.c:12:18: string: 没有那个文件或目录
gdbtest.c:20: parse error before "namespace"
gdbtest.c:20: warning: data definition has no type or storage class
gdbtest.c:21: parse error before '&' token
gdbtest.c:21: parse error before '&' token
gdbtest.c: In function `get_host_info':
gdbtest.c:29: parse error before "sin_addr"
gdbtest.c:38: `local_ip' undeclared (first use in this function)
gdbtest.c:38: (Each undeclared identifier is reported only once
gdbtest.c:38: for each function it appears in.)
gdbtest.c:38: `local_mask' undeclared (first use in this function)
gdbtest.c: In function `main':
gdbtest.c:46: `string' undeclared (first use in this function)
gdbtest.c:46: parse error before "local_ip"
gdbtest.c:47: `local_ip' undeclared (first use in this function)
gdbtest.c:47: `local_mask' undeclared (first use in this function)
gdbtest.c:48: parse error before "printf"
|
套接字在创建了连接后,可以调用以下函数获取套接字双方的协议地址信息,其原型如下:
int getsockname(int s, struct sockaddr *name, int *namelen);
函数getsockname获取套接字描述符s在本地主机命名的协议地址信息,指针name指定了存取该地址信息的缓冲区,参数namelen指定了该缓冲区可容纳的最大字节大小,并回传实际写入缓冲区的信息长度。
比如:
int LocateNativeAddr(int nSock, char *pAddr)
{
struct sockaddr_in addrin;
int lSize;
ASSERT(nSock > 0 && pAddr != NULL);
memset(&addrin, 0, sizeof(addrin));
lSize = sizeof(addrin);
ASSERT(getsockname(nSock, (struct sockaddr*)&addrin, &lSize) >= 0); /* 获取本地主机套接字协议地址信息 */
strcpy(pAddr, (char *)inet_ntoa(addrin.sin_addr)); /* 转换套接字地址信息为以点分隔的字符串形式 */
return 0;
}
返回的是xx.xx.xx.xx格式的IP地址
int getsockname(int s, struct sockaddr *name, int *namelen);
函数getsockname获取套接字描述符s在本地主机命名的协议地址信息,指针name指定了存取该地址信息的缓冲区,参数namelen指定了该缓冲区可容纳的最大字节大小,并回传实际写入缓冲区的信息长度。
比如:
int LocateNativeAddr(int nSock, char *pAddr)
{
struct sockaddr_in addrin;
int lSize;
ASSERT(nSock > 0 && pAddr != NULL);
memset(&addrin, 0, sizeof(addrin));
lSize = sizeof(addrin);
ASSERT(getsockname(nSock, (struct sockaddr*)&addrin, &lSize) >= 0); /* 获取本地主机套接字协议地址信息 */
strcpy(pAddr, (char *)inet_ntoa(addrin.sin_addr)); /* 转换套接字地址信息为以点分隔的字符串形式 */
return 0;
}
返回的是xx.xx.xx.xx格式的IP地址
|
#include 没有这个文件或目录
using namespace std; using parse issue
using namespace std; using parse issue