获取IP(windows和linux)
#ifdef _WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <unistd.h>
#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<count;i++)
{
ipbuf=*(unsigned long*)inaddrs->h_addr_list;
}
}
}
#else
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<bufcount;i++)
{
sprintf(ifr.ifr_name,"eth%d",i);
if(ioctl(sock,SIOCGIFADDR,&ifr)<0) break;
::memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
ipbuf[count++]=sin.sin_addr.s_addr;
}
close(sock);
}
#endif
return count;
}
#ifdef _WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <unistd.h>
#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<count;i++)
{
ipbuf=*(unsigned long*)inaddrs->h_addr_list;
}
}
}
#else
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<bufcount;i++)
{
sprintf(ifr.ifr_name,"eth%d",i);
if(ioctl(sock,SIOCGIFADDR,&ifr)<0) break;
::memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
ipbuf[count++]=sin.sin_addr.s_addr;
}
close(sock);
}
#endif
return count;
}
获取IP(windows和linux)的更多相关文章
- windows和linux下的本机IP的获取(亲测有效)
package com.handsight.platform.fras.util; import org.apache.log4j.Logger; import javax.servlet.http. ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- linux如何自动获取ip地址
第一步:激活网卡 系统装好后默认的网卡是eth0,用下面的命令将这块网卡激活. # ifconfig eth0 up 第二步:设置网卡进入系统时启动 想要每次开机就可以自动获取IP地址上网,就要设置网 ...
- windows和linux下如何远程获取操作系统版本和主机名
远程获取windows和linux操作系统版本和主机名需要具备以下条件: 假设 主机A(windows 7),ip:192.168.12.2 主机B(centos 6.3),ip:192.168.12 ...
- linux python3获取ip地址
一.不带参数 #!/usr/bin/python # -*- coding: UTF-8 -*- import os def get_ip(): #注意外围使用双引号而非单引号,并且假设默认是第一个网 ...
- linux系统下获取IP,MAC,子网掩码,网关
获取IP和子网掩码 int getLocalInfo(char IP[],char Mask[]) { int fd; int interfaceNum = 0; struct ifreq buf[1 ...
- Linux准确获取IP
有时搞一些跨网段的工程和应用,需要尽量准确的知道电信.网通.铁通等电信运营商的IP地址段分配情况,可网上的资料不但很少,而且经常都是N个月前的过期资料…… APNIC是管理亚太地区IP地址分配的机构, ...
- [解决]Kali Linux DHCP自动获取IP失败 坑爹的VMWare桥接
root@kali:~# service networking restart [....] Running /etc/init.d/networking restart is deprecated ...
- Kali Linux常用服务配置教程获取IP地址
Kali Linux常用服务配置教程获取IP地址 下面以Kali Linux为例,演示获取IP地址的方法 (1)设置网络接口为自动获取IP地址.在Kali Linux的收藏夹中单击图标,将显示所有的程 ...
- Linux——获取IP及其中发现的问题
由于最近在学习网络编程,喜欢玩IP地址. 首先,我需要搞清楚如何获得本机IP. ===========================================11.04============ ...
随机推荐
- linux SecureCRT ssh key认证登陆
转自:http://blog.chinaunix.net/uid-20639775-id-3207171.html 通过SecureCRT创建key登录认证 一.生成公钥/密钥对 使用SecureCR ...
- Kmeans聚类算法原理与实现
Kmeans聚类算法 1 Kmeans聚类算法的基本原理 K-means算法是最为经典的基于划分的聚类方法,是十大经典数据挖掘算法之一.K-means算法的基本思想是:以空间中k个点为中心进行聚类,对 ...
- JavaScript BOM对象介绍
bom:即broswer object model(浏览器对象模型),由五个对象组成: Window:对象表示浏览器中打开的窗口 最顶层对象. Navigator :浏览器对 ...
- LoadRunner下载文件脚本
LoadRunner下载文件脚本 在看普泽关于pezybase的测试报告的时候,发现里面有用到jmeter(http协议)并发测试下载文件,考虑到后面可能需要在公司pezybase的并发下载,把之前 ...
- Android优化
ListView的优化 复用convertview , 历史的view对象 减少子孩子查询的次数 viewholder 异步加载数据(把图片缓存) 条目多时分页加载数据 加载时显示进度条让用户等待 I ...
- 上传文件大于 2G以上
1. 开始->运行中输入以下路径, 回车. %windir%\system32\inetsrv\config\applicationhost.config 2. 在打开的配置文件中搜索" ...
- 2.2 代码块--delphi 写日志模块
//2.2 代码块--写日志 //调用例句如:LogMsg('FTP上传线程终止',False,true); procedure LogMsg(AMsg: string; const blnIsErr ...
- Android App 性能优化实践
本文记录了Android App优化需要用到的工具和以及在实践中的Tips.也算对我这半年来部分工作的总结. 工具 Hierarchy Viewer 是 Android SDK 自带的 Layout ...
- Wiki上的Ue4文件结构以及命名规范
https://wiki.unrealengine.com/Assets_Naming_Convention
- html5大纲
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...