Unix Network Programming Episode 88

这篇具有很好参考价值的文章主要介绍了Unix Network Programming Episode 88。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

‘inetd’ Daemon

On a typical Unix system, there could be many servers in existence, just waiting for a client request to arrive. Examples are FTP, Telnet, Rlogin, TFTP, and so on. With systems before 4.3BSD, each of these services had a process associated with it. This process was started at boot-time from the file /etc/rc, and each process did nearly identical startup tasks: create a socket, bind the server’s well-known port to the socket, wait for a connection (if TCP) or a datagram (if UDP), and then fork. The child process serviced the client and the parent waited for the next client request. There are two problems with this model:

1.All these daemons contained nearly identical startup code, first with respect to socket creation, and also with respect to becoming a daemon process (similar to our daemon_init function).
2.Each daemon took a slot in the process table, but each daemon was asleep most of the time.

The 4.3BSD release simplified this by providing an Internet superserver: the inetd daemon. This daemon can be used by servers that use either TCP or UDP. It does not handle other protocols, such as Unix domain sockets. This daemon fixes the two problems just mentioned:

1.It simplifies writing daemon processes since most of the startup details are handled by inetd. This obviates the need for each server to call our daemon_init function.
2.It allows a single process (inetd) to be waiting for incoming client requests for multiple services, instead of one process for each service. This reduces the total number of processes in the system.

Specifying the wait flag for a datagram service changes the steps done by the parent process. This flag says that inetd must wait for its child to terminate before selecting on this socket again. The following changes occur:

1.When fork returns in the parent, the parent saves the process ID of the child. This allows the parent to know when this specific child process terminates, by looking at the value returned by waitpid.
2.The parent disables the socket from future selects by using the FD_CLR macro to turn off the bit in its descriptor set. This means that the child process takes over the socket until it terminates.
3.When the child terminates, the parent is notified by a SIGCHLD signal, and the parent’s signal handler obtains the process ID of the terminating child. It reenables select for the corresponding socket by turning on the bit in its descriptor set for this socket.

‘daemon_inetd’ Function

We can call from a server we know is invoked by inetd.

#include "unp.h"
#include <syslog.h>

extern int daemon_proc;

void daemon_inetd(const char *pname, int facility)
{
    daemon_proc=1;
    openlog(pname, LOG_PID, facility);
}

daemon_inetd function: daemonizes process run by inetd

#include "unp.h"
#include <time.h>

int main(int argc, char **argv)
{
    socklen_t len;
    struct sockaddr *clientaddr;
    char buff[MAXLINE];
    time_t ticks;

    daemon_inetd(argv[0],0);

    clientaddr=Malloc(sizeof(struct sockaddr_storage));
    len=sizeof(struct sockaddr_storage);
    Getpeername(0,clientaddr, &len);
    err_msg("connection from %s", Sock_ntop(clientaddr, len));

    ticks=time(NULL);
    snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
    Write(0,buff, strlen(buff));

    Close(0);
    return 0;
}

Protocol-independent daytime server that can be invoked by inetd文章来源地址https://www.toymoban.com/news/detail-838176.html

到了这里,关于Unix Network Programming Episode 88的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unix Network Programming Episode 76

    We encourage the use of getaddrinfo (Section 11.6(See 8.9.6)) in new programs. The non-null pointer returned by this function points to the following hostent structure: gethostbyname differs from the other socket functions that we have described in that it does not set errno when an error occurs. Instead, it sets the global integer h_errno to one of the foll

    2024年02月09日
    浏览(23)
  • unix网络编程-简易服务器与客户端程序解析

    a -- address f -- file        eg: fputs() -- file put stream fd -- file descriptor h - host(主机) in/inet -- internet        eg: sockaddr_in; inet_aton n -- network(网络字节序)/numeric(数值) p -- protocol(协议)/presentation(表达/呈现形式) s -- socket        eg: sin -- socket internet t -- type,用于指定某种

    2024年01月16日
    浏览(54)
  • UNIX网络编程卷一 学习笔记 第三十章 客户/服务器程序设计范式

    开发一个Unix服务器程序时,我们本书做过的进程控制: 1.迭代服务器(iterative server),它的适用情形极为有限,因为这样的服务器在完成对当前客户的服务前无法处理已等待服务的新客户。 2.并发服务器(concurrent server),为每个客户调用fork派生一个子进程。传统上大多U

    2024年02月09日
    浏览(38)
  • 【Socket】Unix环境下搭建简易本地时间获取服务

    本文搭建一个Unix环境下的、局域网内的、简易的本地时间获取服务。 主要用于验证: 当TCP连接成功后,可以在两个线程中分别进行读操作、写操作动作 当客户端自行终止连接后,服务端会在写操作时收到 SIGPIPE 信号 当客户端执行shutdown写操作后,客户端会在写操作时收到

    2024年02月04日
    浏览(28)
  • MobaXterm连接服务器:Network error: Connection refused

    centos7: ubuntu20.04

    2024年02月16日
    浏览(45)
  • 【Hello Network】DNS协议 NAT技术 代理服务器

    本篇博客简介:介绍DNS协议 NAT技术和代理服务器 DNS是一整套从域名映射到IP的系统 为什么要有域名 其实作为我们程序员来说 使用域名还是IP地址是无所谓的 但是站在商业公司和用户的角度就不这么认为了 商业公司希望用户能够快速的记住自己公司的网址 而用户也希望自己

    2024年02月11日
    浏览(30)
  • MobaXterm监控服务器的资源(CPU、RAM、Network、disk...) 使用情况

    使用服务器的时候比较喜欢随时查看的服务器资源使用情况,比如内存,CPU,网速,磁盘使用等情况,一次偶然的机会发现了MobaXterm提供有这项功能,在会话窗口底部: 完整窗口示意图 如果你发现你的会话窗口底部没有,可以这样开启: Settings→SSH→勾选Remote-monitoring 参考

    2024年02月14日
    浏览(32)
  • mobaxterm无法连接vmware虚拟机服务器,network error:connection refused

    场景描述: 电脑硬盘换了,重新安装vmware,ubuntu,mobaxterm..... 安装完ubuntu后,因为习惯了无UI的界面,所以关闭了ubuntu的桌面服务 (有需要的同学可以通过sudo systemctl set-default multi-user.target,然后sudo reboot就可以关闭桌面服务了,打开命令是sudo 6systemctl set-default graphical.targe

    2024年02月14日
    浏览(34)
  • /etc/netplan/network-manager-all.yaml 配置服务器ip

    本文为博主原创,转载请注明出处: /etc/netplan 是用于配置 Ubuntu 系统网络接口的目录。在 Ubuntu 中,网络配置的默认工具为   Netplan ,而 /etc/netplan 则是 Netplan 配置文件的存储位置。 在 /etc/netplan 目录中,通常会有一个或多个 YAML 格式的文件,用来定义系统中的网络接口、

    2024年02月07日
    浏览(23)
  • Putty连接服务器后弹出Network error: Software caused connection abort

    天行健,君子以自强不息;地势坤,君子以厚德载物。 每个人都有惰性,但不断学习是好好生活的根本,共勉! 文章均为学习整理笔记,分享记录为主,如有错误请指正,共同学习进步。 在使用putty连接服务器时,连接成功后过一会弹出如下错误 字面意思大概是 网络错误:

    2024年02月05日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包