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.

相关推荐
月明长歌3 小时前
Java 网络编程套接字入门:从“发一段数据”到“写一个可并发的服务器”
java·服务器·网络
冗量3 小时前
《性能之巅》第七章:内存 读书笔记
服务器·性能优化·性能调优
Codefengfeng3 小时前
binwalk提取文件时无输出文件夹(kali-linux环境)
linux·运维·服务器
EmbedLinX4 小时前
嵌入式Linux根文件系统制作与移植
linux·服务器·笔记·学习
开开心心_Every5 小时前
局域网大文件传输,设密码双向共享易用工具
运维·服务器·网络·游戏·pdf·电脑·excel
qq_24218863325 小时前
分享Xshell在服务器管理中的高级技巧与自动化脚本案例
运维·服务器·自动化
袁袁袁袁满5 小时前
Linux网络连接之ss命令详细使用指南(从入门到运维实战)
linux·运维·服务器·网络·ssh·网络连接·ss命令
爱华晨宇6 小时前
Xshell高效运维实战指南
运维·服务器
未来之窗软件服务6 小时前
服务器运维(四十一)日服务器linux-audit.log分析工具—东方仙盟
linux·运维·服务器·服务器运维·仙盟创梦ide·东方仙盟
guizhoumen6 小时前
建站从零开始之域名、服务器和CMS网站程序的选择
运维·服务器·网络