网络编程中本机地址常用函数
来源:互联网 发布:js中array的map方法 编辑:程序博客网 时间:2024/06/05 10:51
- gethostbyname根据给定的主机名,获取主机信息。已经过时,仅用于IPv4,且线程不安全。
#include <stdio.h>#include <netdb.h>#include <arpa/inet.h>extern int h_errno;int main(int argc, char *argv[]){ struct hostent *host; char str[128]; host = gethostbyname(argv[1]); printf("%s\n", host->h_name); while (*(host->h_aliases) != NULL) printf("%s\n", *host->h_aliases++); switch (host->h_addrtype) { case AF_INET: while (*(host->h_addr_list) != NULL) printf("%s\n", inet_ntop(AF_INET, (*host->h_addr_list++), str, sizeof(str))); break; default: printf("unknown address type\n"); break; } return 0;}
gethostname、gethostbyname、gethostbyaddr The domain name queries carried out by gethostbyname() and gethostbyaddr() use a combination of any or all of the name server named(8), a broken out line from /etc/hosts, and the Network Information Service (NIS or YP), depending upon the contents of the order line in /etc/host.conf. The default action is to query named(8), followed by /etc/hosts. The hostent structure is defined in <netdb.h> as follows: struct hostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses */ } #define h_addr h_addr_list[0] /* for backward compatibility */ The members of the hostent structure are:
- gethostbyaddr函数。
此函数只能获取域名解析服务器的url和/etc/hosts里登记的IP对应的域名。
#include <stdio.h>#include <netdb.h>#include <arpa/inet.h>extern int h_errno;int main(int argc, char *argv[]){ struct hostent *host; char str[128]; struct in_addr addr; inet_pton(AF_INET, argv[1], &addr); host = gethostbyaddr((char *)&addr, 4, AF_INET); printf("%s\n", host->h_name); while (*(host->h_aliases) != NULL) printf("%s\n", *host->h_aliases++); switch (host->h_addrtype) { case AF_INET: while (*(host->h_addr_list) != NULL) printf("%s\n", inet_ntop(AF_INET, (*host->h_addr_list++), str, sizeof(str))); break; default: printf("unknown address type\n"); break; } return 0;}
- getservbyname
- getservbyport
根据服务程序名字或端口号获取信息。使用频率不高。 - getaddrinfo
- getnameinfo
freeaddrinfo
可同时处理IPv4和IPv6,线程安全的。套接口和地址关联
- getsockname
根据accpet返回的sockfd,得到临时端口号
- getsockname
getsockname & getpeername 获取本地的地址(注意是已连接以后的套接字)NAME getsockname - get socket nameSYNOPSIS #include <sys/socket.h> int getsockname(int s, struct sockaddr *name, socklen_t *namelen); struct sockaddr_in localaddr; socklen_t addrlen = sizeof(localaddr); //获取本地的地址 if (getsockname(sock, (struct sockaddr*)&localaddr, &addrlen) < 0) ERR_EXIT("getsockname"); printf("ip=%s port=%d\n", inet_ntoa(localaddr.sin_addr), ntohs(localaddr.sin_port));
getpeername
根据accpet返回的sockfd,得到远端链接的端口号,在exec后可以获取客户端信息。总体示例代码
int getlocalip(char *ip){ char host[100] = {0}; if (gethostname(host, sizeof(host)) < 0) return -1; struct hostent *hp; if ((hp = gethostbyname(host)) == NULL) return -1; strcpy(ip, inet_ntoa(*(struct in_addr*)hp->h_addr)); return 0;}int main(void){ char host[100] = {0}; if (gethostname(host, sizeof(host)) < 0) ERR_EXIT("gethostname"); printf("\n\n\nhost:%s \n", host); struct hostent *hp; if ((hp = gethostbyname(host)) == NULL) ERR_EXIT("gethostbyname"); int i = 0; while (hp->h_addr_list[i] != NULL) { //char *inet_ntoa(struct in_addr in); 要求填入一个结构体元素 printf("%s\n", inet_ntoa(*(struct in_addr*)hp->h_addr_list[i])); i++; } char ip[16] = {0}; getlocalip(ip); printf("localip=%s\n", ip); return 0;}
0 0
- 网络编程中本机地址常用函数
- 网络编程1 -----获取本机IP地址
- windows获得本机IPV6地址(网络编程)
- 网络编程1 -----获取本机IP地址
- 获取本机IP地址(java网络编程)
- 网络编程1-----获取本机IP地址
- Windows网络编程获得本机IP地址
- 网络编程中常用的一些函数
- 网络编程中常用函数实现
- Linux中网络编程常用函数
- 网络编程常用函数
- 网络编程常用函数
- 网络编程常用函数
- 网络编程常用函数
- 网络编程常用函数
- 套接字编程中常用的地址转换函数
- linux网络编程中常用的辅助函数
- 关于网络编程中一些常用函数的理解
- 【30.43%】【codeforces 746C】Tram
- 【LeetCode】 217. Contains Duplicate
- Memcache源码阅读(5)---哈希表管理
- android stutio使用注解式框架Butterknife教程
- 用nfs挂载内核时出现错误T T T T T T T *** ERROR: Cannot umount的解决办法
- 网络编程中本机地址常用函数
- 1619-7 张良 十二月十八号总结 [连续第七十九天]
- Netty系列-客户端启动源码分析
- 日志管理
- Android Volley完全解析(二),使用Volley加载网络图片
- Unity关于2D角色序列帧8个方向动作资源优化
- 【鸟哥Linux】Linux 磁盘与文件系统管理
- 1283.山师好男友(开关灯问题)
- c语言编程---性能优化