• UNIX网络编程第4章4.5listen函数4.6accept函数


    FYWP4K`[U_SQ$L$D)NS3C$9

    (F8(QVOQ~7O6@S~V1ONL9PF

    D8{2M1H%YB`H`D$6[QPKGAO

    YKL))PCO_CYZWRAC8A80((W

    ]I%UBZ(_4UNMXF`3X6@Y}3W

    YGXX_9FFZ17U3[WQ34[6(6W

    OBD)W_CI`]B0]~@[O(M`_{U

    0E5US{JAW9S(VB$Y}RPPJR4

    QHCT)6(@UK3JS73}$F[4HOI

    _SBAIPR]65R1XR)0{{Q$YZE

    I])STCIFH`GTB@}D]]C]](J

    #include <time.h>
    #include "../lib/unpsunyj.h"
    
    // Daytime server that prints client IP address and port
    
    int main(int argc, char **argv)
    {
        int                 listenfd;
        int                 connfd;
        socklen_t           len;
        struct sockaddr_in  servaddr;
        struct sockaddr_in  cliaddr; // 它将存放客户的协议地址
        char                buff[MAXLINE];
        time_t              ticks;
    
        // listenfd = Socket(AF_INET, SOCK_STREAM, 0);
        if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
            err_sys("socket error");
    
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family      = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port        = htons(13);   /* daytime server */
    
        //Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
        if (bind(listenfd, (sockaddr*)&servaddr, sizeof(servaddr)) < 0)
        {
            err_sys("bind error");
        }
    
        // Listen(listenfd, LISTENQ);
        if (listen(listenfd, LISTENQ) < 0)
        {
            err_sys("listen error");
        }
    
        for ( ; ; )
        {
            len = sizeof(cliaddr);
            // connfd = Accept(listenfd, (SA *) &cliaddr, &len);
    again:
            if ((connfd = accept(listenfd, (sockaddr*)&cliaddr, &len)) < 0)
            {
    #ifdef  EPROTO
                if (errno == EPROTO || errno == ECONNABORTED)
    #else
                    if (errno == ECONNABORTED)
    #endif
                        goto again;
                    else
                        err_sys("accept error");
            }
    
            const char  *ptr;
            if ((ptr = inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff))) == NULL)
                err_sys("inet_ntop error");     /* sets errno */
    
            printf("connection from %s, port %d
    ",
                    // Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),
                    ptr,
                    ntohs(cliaddr.sin_port));
    
            ticks = time(NULL);
            snprintf(buff, sizeof(buff), "%.24s
    ", ctime(&ticks));
            // Write(connfd, buff, strlen(buff));
            int length = strlen(buff);
            if (write(connfd, buff, length) != length)
            {
                err_sys("write error");
            }
    
            // Close(connfd);
            if (close(connfd) == -1)
            {
                err_sys("close error");
            }
        }
    }

    dROSI_003_001

    ROSI_003_002

    ROSI_003_003

    ROSI_003_004ROSI_003_005ROSI_003_006

    ROSI_003_007

    ROSI_003_008

    ROSI_003_009ROSI_003_010ROSI_003_011ROSI_003_012

    ROSI_003_013

    ROSI_003_014

    ROSI_003_015

    ROSI_003_016ROSI_003_017ROSI_003_018ROSI_003_019ROSI_003_020

    ROSI_003_021

    ROSI_003_022ROSI_003_023ROSI_003_024ROSI_003_025ROSI_003_026

    ROSI_003_027ROSI_003_028ROSI_003_029

    ROSI_003_030

    ROSI_003_031

    ROSI_003_032

    ROSI_003_033

    ROSI_003_034

    ROSI_003_035ROSI_003_036

  • 相关阅读:
    java web 工程更改名字
    [转]Eclipse下开发Struts奇怪异常:org.apache.struts.taglib.bean.CookieTei
    【转】myeclipse 自定义视图Customize Perspective 没有反应
    latex建立参考文献的超链接
    latex 脚注编号也成为超链接
    自定义标签TLD文件中,rtexprvalue子标签的意思
    设计模式观察者
    设计模式模板方法
    设计模式策略
    设计模式享元
  • 原文地址:https://www.cnblogs.com/sunyongjie1984/p/4340049.html
Copyright © 2020-2023  润新知