坚持学习100天:本机网络信息

本文详细介绍了网络信息的基础知识,包括MTU、IP分组的分片、数据段、数据报等术语,并讲解了如何获取本地计算机名称、IP、子网IP和掩码以及物理网卡地址信息。通过gethostname()、gethostbyname()、inet_ntoa()等函数和GetAdaptersInfo()方法进行实际操作,同时讨论了各种获取MAC地址的方法及其优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

Hello,我是修齊。
学习C++的第一百零二十三天,不为人知的日子里熠熠生辉,用心学习。
在这里记录一些学习的东西和学习的心情,内容主要是一些自己学习整理的小笔记。

一、术语

MTU
1.Maximum Transfer Unit:最大传输单位,物理帧的大小。
2.MTU的值由采用的物理技术决定。
IP分组的分片
1.IP数据包从网络层到链路层,就封装成数据帧,IP数据包无法封装在一个数据帧中,将数据包分成几个长度小于MTU的分片,每分片也叫数据报,IP分片也叫IP数据报,将分片封装在帧中传输,分解的分片传输到目的地在将这些分片重新组成原来的IP数据包。
数据段
1.传输层的单元:segment
数据报
1.不同场合不同含义:datagram
2.场合一:专指UDP数据报,面向无连接的数据传输
3.场合二:数据包的分组,IP数据包大于MTU值时分片,分片数据就是一个IP数据报。
数据包
1.网路层传输的数据单元:packet、IP包
2.带有足够的寻址信息(IP地址),可独立从源主机传输到目的主机
3.是IP协议中完整的数据单元,一个或多个数据报组成
数据帧
1.数据链路层的传输单元:frame
2.为网络层传入的数据添加一个头部和尾部,组成帧,帧根据MAC寻址。
比特流
1.物理层的介质是上直接实现无结构bit流传送的,即高低电平信号。

二、获取本地计算机名称和IP

1.gethostname():检索本地的计算机的标准主机名

int gethostname(char*name,int namelen);

2.gethostbyname():从主机数据库中检索与主机名对应的主机信息<如IP地址>

hostent *gethostbyname( const char *name)
如果没有错误,返回指向hostent结构的指针;否则,返回一个空指针,并可调用WSAGetLastError来检索特定的错误。
错误号:WSANOTINITIALISED,表示没有预先成功调用WSAStartup函数

3.hostent是一个结构体

hostent定义:
typedef struct hostent{
   char*h_name;//h_name:主机的正式名
   char**h_aliases;//h_aliases:指向以NULL结尾的主机别名函数组
   short h_addrtype;//h_addrtype:返回地址类型
   short h_length;//h_length:表示IP地址的字节长度,IPv4对应4个字节,一般主机可有多个IP地址
   char **h_addr_list;//h_addr_list:用来保存多个IP地址
}HOSTENT, *PHOSTENT, *LPHOSTENT;

4.inet_ntoa():将十进制网络字节序转换为点分十进制IP格式的字符串

函数声明:
char*inet_ntoa(struct in_addr in);
in:表Internet主机地址的结构,函数争取,返回一个字符指针,指向一块存储着点分格式IP地址的静态缓冲区,否则,返回NULL

三、获取本机子网IP地址和掩码

1.子网掩码(subnet mssk):用来指明一个IP地址的那些位标识的是主机的位掩码,不能单独存在,必须结合IP地址一起使用,作用就是将某个IP地址划分成网络地址和主机地址两部分
2.GetAdaptersInfo():检索本地计算机的适配器信息

函数声明:
ULONG
GetAdapersInfo(PIP_ADARTER_INFO pAdapterInfo, PULONG pOutBufLen);
pAdapterInfo:指向接收IP适配器信息结构链表的缓冲区的指针(链表节点的指针)
pOutBufLen:指向ulong变量的指针,变量指定PAdapterInfo参数指向的缓冲区大小,若大小不足以保存适配器信息,pAdapterInfo将使用所需的大小填充此变量,并返回错误代码:ERROR_BUFFER_OVERFLOW.函数成功返回值为ERROR_SUCCESS。函数失败,返回值为5种错误代码:
ERROR_BUFFER_OVERFLOW:接收适配器信息的缓冲区太小。
ERROR_INVALID_DATA:检索到无效的适配器信息。
ERROR_INVALID_PARAMETED:存在某个参数无效。
ERROR_NO_DATA:本地计算机不存在适配器信息。
ERROR_NOT_SUPPORTED:本地计算机上运行的操作系统不支持GetAdaptersInfo函数。

四、获取本机物理网卡地址信息

1.网卡地址就是MAC地址,是一个用来确认网上设备位置的地址,长度是48比特(6字节),由16进制的数字组成,分前24位和后24位。
2.window下,cmd控制台中,输入“ipconfig/all”或输入“ipconfig-all”可以查看网卡地址。
3.Netbios、SNMP、GetAdaptersInfo也可以获取网卡MAC地址
4.Netbios:在拔出网线的情况下获取不到MAC。
5.SNMP:有时会获取多个重复的网卡的MAC。
6.GetAdaptersInfo:两者兼得,最适合
7.GetAdaptersInfo问题:

区分物理网卡和虚拟网卡
区分无线网卡和有线网卡
“禁用”的网卡获取不到

五、小脑袋,大疑问

加油

上述内容如有侵权,联系即删。
感谢阅读-感谢支持

利用多进程,调用DOS命令查看网络信息 public class ipconfig extends JFrame{ static TextField ip_text=new TextField(15); static TextField mac_text=new TextField(15); static TextField subnetmask_text=new TextField(15); static TextField gateway_text=new TextField(15); static TextField dns_text=new TextField(15); static JPanel subnetmask=new JPanel(new FlowLayout()); static JPanel gateway=new JPanel(new FlowLayout()); static JPanel IP=new JPanel(new FlowLayout()); static JPanel dns=new JPanel(new FlowLayout()); public static void main(String[] args) throws UnknownHostException { ipconfig f=new ipconfig(); f.setTitle("查看本机网络设置QQ125004485"); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocation(400,400); f.setSize(new Dimension(200,300)); // gotoUrlWindows("http://ygsn.taobao.com/"); //gotoUrlWindows("www.sohu.com"); f.setResizable(false); f.setLayout(new GridLayout(6,1)); //IP文本框 setCFont(IP); IP.add(new Label("IP:")); IP.add(ip_text); ip_text.setText(getLocalIp()); //MAC 文本框 JPanel MAC=new JPanel(new FlowLayout()); setCFont(MAC); MAC.add(new Label("MAC:")); MAC.add(mac_text); mac_text.setText(getMacAddressIP(ip_text.getText())); //子网掩码 setCFont(subnetmask); subnetmask.add(new Label("子网掩码:")); subnetmask.add(subnetmask_text); subnetmask_text.setText(getSubnetMask()); //网关 setCFont(gateway); gateway.add(new Label("网关:")); gateway.add(gateway_text); gateway_text.setText(getGateWay()); //DNS setCFont(dns); dns.add(new Label("DNS:")); dns.add(dns_text); dns_text.setText(getDns()); //加载文本框 f.add(subnetmask); f.add(MAC); f.add(IP); f.add(gateway); f.add(dns); f.pack(); } // Windows platform goto url private static void gotoUrlWindows(String url){ String cmd = "rundll32 url.dll,FileProtocolHandler " + url; try { Runtime.getRuntime().exec(cmd); } catch (IOException e) { e.printStackTrace(); } } //取IP private static String getLocalIp() throws UnknownHostException { return InetAddress.getLocalHost().getHostAddress(); } //设置字体 private static void setCFont(Container c) { c.setFont(new Font( "宋体 ",Font.BOLD,15)); } public static String getMacAddressIP(String remotePcIP) { String str = ""; String macAddress = ""; try { Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i < 100; i++) { str = input.readLine(); if (str != null) { if (str.indexOf("MAC Address") > 1) { macAddress = str.substring( str.indexOf("MAC Address") + 14, str.length()); break; } } } } catch (IOException ex) { } return macAddress; } public static String getSubnetMask() { String str = ""; String subnetmask = ""; try { Process pp = Runtime.getRuntime().exec("ipconfig /all"); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i < 100; i++) { str = input.readLine(); if (str != null) { if (str.indexOf("Subnet Mask") > -1) { subnetmask = str.substring( str.indexOf("Subnet Mask") + 36, str.length()); break; } } } } catch (IOException ex) { } return subnetmask; } public static String getGateWay() { String str = ""; String gateway = ""; try { Process pp = Runtime.getRuntime().exec("ipconfig /all"); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i < 100; i++) { str = input.readLine(); if (str != null) { if (str.indexOf("Default Gateway") > -1) { gateway = str.substring( str.indexOf("Default Gateway") + 36, str.length()); break; } } } } catch (IOException ex) { } return gateway; } public static String getDns() { String str = ""; String dns = ""; try { Process pp = Runtime.getRuntime().exec("ipconfig /all"); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i < 100; i++) { str = input.readLine(); if (str != null) { if (str.indexOf("DNS Servers") > -1) { dns = str.substring( str.indexOf("DNS Servers") + 36, str.length()); break; } } } } catch (IOException ex) { } return dns; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值