zoukankan      html  css  js  c++  java
  • 【Unix网络编程】chapter5TCP回射服务器程序

    chapter5
     5.1 概述
     5.2 TCP回射服务器程序:main函数

    •   int main(int argc, char **argv)
    •   {
    •    int listenfd,connfd;
    •    pid_t childpid;
    •    socklen_t clilen;
    •    struct sockaddr_in, cliaddr, servaddr;
    •    listenfd = Socket(AF_INET, SOCK_STREAM, 0);
    •    bzero(&servaddr, sizeof(servaddr));
    •    servaddr.sin_famlily = AF+INET;
    •    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    •    servaddr.sin_port = htons(SERV_PORT);
    •    Bind(listenfd, (SA*)&servaddr, sizeof(servaddr));
    •    Listne(listenfd,LISTENQ);
    •    for(;;)
    •    {
    •     client = sizeof(cliaddr);
    •     connfd = Accept(listenfd, (SA*)&cliaddr, &client);
    •     if( (childpit = Fork()) == 0)
    •     {
    •      Close(listenfd);
    •      str_echo(connfd);
    •      exit(0);
    •     }
    •     Close(connfd);
    •    }
    •   }

     5.3 TCP 回射服务器程序:str_echo函数

    •   void str_echo(int sockfd)
    •   {
    •    ssize_t n;
    •    char buf[MAXLINE];
    •   again:
    •    while( (n = read(sockfd, buf, MAXLINE)) > 0)
    •     Writen(sockfd,buf, n);
    •    if(n < 0 && error =- EINTR)
    •     goto again;
    •    else if(n < 0)
    •     err_sys("error");
    •   }

     5.4 TCP 回射客户程序:main函数
     5.5
     5.6 正常启动
     5.7 正常终止
     5.8 POSIX信号
     5.9 处理SIGCHILD信号
     5.10 wait和waitpid函数

    •   #include <sys/wait.h>
    •   pid_t wait(int *status);
    •   pid_t waitpid(pit_t pid, int *status, int options);
    作者:长风 Email:844064492@qq.com QQ群:607717453 Git:https://github.com/zhaohu19910409Dz 开源项目:https://github.com/OriginMEK/MEK 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利. 感谢您的阅读。如果觉得有用的就请各位大神高抬贵手“推荐一下”吧!你的精神支持是博主强大的写作动力。 如果觉得我的博客有意思,欢迎点击首页左上角的“+加关注”按钮关注我!
  • 相关阅读:
    js debounce防抖技术
    我在项目中es6中数组的常用方法
    windows 部署Nginx转发http2.0协议
    AES加密,C#和java相同
    asp:Button js弹出提示框信息
    服务器不重启安装Asp.net Core 程序包
    C# string.Join的用法
    IIS部署asp.net core webapi
    ASP.net 加载不了字体Failed to load resource: the server responded with a status of 404 (Not Found)
    Windows Redis 取消保护模式C#进行访问
  • 原文地址:https://www.cnblogs.com/zhaohu/p/8939944.html
Copyright © 2011-2022 走看看