Unix Network Programming Episode 75

Controlling Termination

复制代码
for(;;)
{
    len=sizeof(struct sockaddr_in);
    rd_sz=Sctp_recvmsg(sock_fd, readbuf, sizeof(readbuf), (SA *)&cliaddr, &len, &sri, &msg_flags);
    if(stream_increment){
        sri.sinfo_stream++;
        if(sri.sinfo_stream>=sctp_get_no_strms(sock_fd,(SA *)&cliaddr, len))
        sri.sinfo_stream=0;
    }
    Sctp_sendmsg(sock_fd, readbuf, rd_sz,(SA *)&cliaddr, len, sri.sinfo_ppid, (sri.sinfo_flags|MSG_EOF), sri.sinfo_stream,0,0);
}

The server terminates an association on reply

复制代码
if(echo_to_all==0)
    sctpstr_client(stdin,sock_fd, (SA *)&servaddr, sizoef(seraddr));
else
    sctpstr_client_echoall(stdin,sock_fd, (SA *)&servaddr, sizeof(servaddr));
strcpy(byemsg,"goodbye");
Sctp_sendmsg(sock_fd, byemsg, strlen(byemsg), (SA *)&servaddr, sizeof(servaddr),0,MSG_ABORT,0,0,0);
Close(sock_fd);

The client aborts the association before closing

Name and Address Conversions

Introduction

All the examples so far in this text have used numeric addresses for the hosts (e.g., 206.6.226.33) and numeric port numbers to identify the servers (e.g., port 13 for the standard daytime server and port 9877 for our echo server). We should, however, use names instead of numbers for numerous reasons: Names are easier to remember; the numeric address can change but the name can remain the same; and with the move to IPv6, numeric addresses become much longer, making it much more error-prone to enter an address by hand.

Domain Name System (DNS)

The DNS is used primarily to map between hostnames and IP addresses. A hostname can be either a simple name, such as solaris or freebsd, or a fully qualified domain name '(FQDN), such as solaris.unpbook.com.

Technically, an FQDN is also called an absolute name and must end with a period, but users often omit the ending period. The trailing period tells the resolver that this name is fully qualified and it doesn't need to search its list of possible domains.

In this section, we will cover only the basics of the DNS that we need for network programming.

'gethostbyname' Function

Host computers are normally known by human-readable names. All the examples that we have shown so far in this book have intentionally used IP addresses instead of names, so we know exactly what goes into the socket address structures for functions such as connect and sendto, and what is returned by functions such as accept and recvfrom. But, most applications should deal with names, not addresses. This is especially true as we move to IPv6, since IPv6 addresses (hex strings) are much longer than IPv4 dotted-decimal numbers. (The example AAAA record and ip6.arpa PTR record in the previous section should make this obvious.)

The most basic function that looks up a hostname is gethostbyname. If successful, it returns a pointer to a hostent structure that contains all the IPv4 addresses for the host. However, it is limited in that it can only return IPv4 addresses.

相关推荐
程序员与背包客_CoderZ31 分钟前
Linux C++异步网络编程:c-ares高性能DNS解析编码实战
linux·服务器·c语言·开发语言·网络·c++·网络编程
Regulus-Moon1 小时前
纠删码:用代数换存储
服务器·数据库·分布式
ai_xiaogui1 小时前
Ubuntu Server 安装避坑实录:服务器装成桌面版、网卡驱动缺失怎么办?
linux·服务器·ubuntu·ubuntu server·ubuntu 网卡驱动安装·服务器装机避坑·服务器装机
z2014z1 小时前
信息安全工程师 第9章 虚拟专用网 技术原理与应用 笔记
服务器·网络
飞花漫卷,情随岁迁1 小时前
命令进阶与 Playbook 快速入门
java·linux·运维·服务器·网络·数据库·智能路由器
master3362 小时前
Linux 内核参数详解:原理、调优与生产实践
linux·运维·服务器
shoufeipin90hou2 小时前
同色异料的物理边界:颜色通道与材质通道的选择方法
服务器·计算机视觉·性能优化·材质
lgbisha2 小时前
银河麒麟:软链接还在却打不开,怎么查它指向了哪里
linux·服务器·前端·银河麒麟
@atweiwei11 小时前
LangChainRust 0.33.0 发布:让 Agent 应用更容易运行、观察和评测
服务器·前端·javascript
工业一体机老司机11 小时前
rsync+inotify 收集车间工位机日志的完整配置
服务器·数据库·postgresql