当前位置: 首页 > news >正文

Unix Network Programming Episode 77

‘gethostbyaddr’ Function

The function gethostbyaddr takes a binary IPv4 address and tries to find the hostname corresponding to that address. This is the reverse of gethostbyname.

#include <netdb.h>
struct hostent *gethostbyaddr (const char *addr, socklen_t len, int family);

‘getservbyname’ and ‘getservbyport’ Functions

Services, like hosts, are often known by names, too. If we refer to a service by its name in our code, instead of by its port number, and if the mapping from the name to port number is contained in a file (normally /etc/services), then if the port number changes, all we need to modify is one line in the /etc/services file instead of having to recompile the applications. The next function, getservbyname, looks up a service given its name.

#include <netdb.h>
struct servent *getservbyname (const char *servname, const char *protoname);

This function returns a pointer to the following structure:

struct servent {char *s_name; /* official service name */char **s_aliases; /* alias list */int s-port; /* port number, network-byte order */char *s_proto; /* protocol to use */
};

The service name servname must be specified. If a protocol is also specified (protoname is a non-null pointer), then the entry must also have a matching protocol.

The next function, getservbyport, looks up a service given its port number and an optional protocol.

#include <netdb.h>
struct servent *getservbyport (int port, const char *protoname);
#include "unp.h"int main(int argc, char **argv)
{int sockfd, n;char recvline[MAXLINE+1];struct sockaddr_in servaddr;struct in_addr **ptr;struct in_addr *inetaddrp[2];struct in_addr inetaddr;struct hostent *hp;struct servent *sp;if(argc!=3){err_quit("usage: daytimetcpclientl <hostname> <service>");}if((hp=gethostbyname(argv[1]))==NULL){if(inet_aton(argv[1],&inetaddr)==0){err_quit("hostname error for %s: %s",argv[1], hstrerror(h_errno));}else{inetaddrp[0]=&inetaddr;inetaddrp[1]=NULL;pptr=inetaddrp;}}else{pptr=(struct in_addr **)hp->h_addr_list;}if((sp=getservyname(argv[2],"tcp"))==NULL){err_quit("getservbyname error for %s", argv[2]);}for(;*pptr!=NULL;pptr++){sockfd=Socket(AF_INET, SOCK_STREAM,0);bzero(&servaddr, sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_port=sp->s_port;memcpy(&servaddr.sin_addr, *pptr, sizeof9struct in_addr));printf("trying %s\n", Sock_ntop((SA *)&servaddr, sizeof(servaddr)));if(connect(sockfd, (SA *)&servaddr, iszoef9servaddr))==0)break;err_ret("connect error");close(sockfd);}if(*pptr==NULL){err_quit("unable to connect");}while((n=Read(sockfd, recvline,MAXLINE))>0){recvlien[n]=0;Fputs(recvline, stdout);}return 0;
}

Our daytime client that uses gethostbyname and getservbyname

http://www.lryc.cn/news/163482.html

相关文章:

  • 解决Ubuntu无法安装pycairo和PyGObject
  • Android Handler 机制解析
  • 酒店固定资产管理怎么分类
  • OpenCV(三十一):形态学操作
  • Python之面向对象(二)
  • ESP32用作经典蓝牙串口透传模块与手机进行串口通信
  • Python 操作 CSV
  • 自动化运维工具Ansible教程(二)【进阶篇】
  • 嵌入式Linux基础学习笔记目录
  • JVM | 垃圾回收器(GC)- Java内存管理的守护者
  • yolov5添加ECA注意力机制
  • 华为在高端智能手机市场再次撕开了一道深深的口子
  • 前端设计模式和设计原则之设计模式
  • 提高在速卖通产品上的曝光率——自养号测评优势全面解析!
  • 指针进阶(二)
  • 【HCIE】03.BGP高级特性
  • 单个处理数据祖籍列表层级关系
  • Maven部署打包多环境(开发、测试、生产)配置教程
  • 【计算思维题】少儿编程 蓝桥杯青少组计算思维 数学逻辑思维真题详细解析第9套
  • 【Hello Algorithm】贪心算法
  • TOP-K问题
  • 【保姆级从0到1】UE5 蓝图入门教程1:关卡、蓝图入门
  • 【码银送书第六期】《ChatGPT原理与实战:大型语言模型的算法、技术和私有化》
  • redis 报错 Redis protected-mode 配置文件没有真正启动
  • 解决a标签内容中img标签和p标签垂直方向间隔太大的问题
  • 如何选择靠谱的全景平台?VR全景加盟从哪方面对比?
  • CentOS系统环境搭建(十八)——CentOS7安装Docker20.10.12和docker compose v2
  • NebulaGrap入门介绍和集群安装部署
  • thinkphp5.0 composer 安装oss提示php版本异常
  • AList dokcer安装及百度网盘挂载