网络编程基础:适配器列表、套接字及通信模型详解
1. Linux和macOS上列出网络适配器
在基于Unix的系统(如Linux和macOS)上列出本地网络地址相对Windows来说更为容易。下面是具体实现步骤:
1. 包含必要的系统头文件 :
/*unix_list.c*/
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
- 进入主函数 :
/*unix_list.c continued*/
int main() {
struct ifaddrs *addresses;
if (getifaddrs(&addresses) == -1) {
printf("getifaddrs call failed\n");
return -1;
}
这里定义了一个变量 addresses 来存储地址,调用 getifaddrs() 函数会分配内存并填充一个地址链表。该函数成功时返回0,失败时返回 -1。
3. 遍历地址链表
超级会员免费看
订阅专栏 解锁全文
4796

被折叠的 条评论
为什么被折叠?



