• Unix Network Programming Episode 80


    ‘tcp_connect’ Function

    We will now write two functions that use getaddrinfo to handle most scenarios for
    the TCP clients and servers that we write. The first function, tcp_connect, performs
    the normal client steps: create a TCP socket and connect to a server.

    #include "unp.h"
    int tcp_connect (const char *hostname, const char *service);
    
    • 1
    • 2
    #include "unp.h"
    
    int tcp_connect(const char *host, const char *serv)
    {
        int socfd, n;
        struct addrinfo hints, *res, *ressave;
    
        bzero(&hints, sizeof(struct addrinfo));
        hints.ai_family=AF_UNSPEC;
        hints.ai_socktype=SOCK_STREAM;
        
        if((n=getaddrinfo(host, serv, &hints, &res))!=0)
            err_quit("tcp_connect error for %s, %s: %s", host, serv, gai_strerror(n));
        
        ressave=res;
    
        do{
            sockfd=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
            if(sockfd<0)
            {
                continue;
            }
            if(connect(sockfd, res->ai_addr, res->ai_addrlen)==0)
                break;
            Close(sockfd);
        }while((res=res->ai_next)!=null);
    
        if(res==NULL)
            err_sys("tcp_connect error for %s, %s",host, serv);
        
        freeaddrinfo(ressave);
    
        return sockfd;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    tcp_connect function: performs normal client steps

    #include "unp.h"
    
    int main(int argc, char **argc)
    {
        int sockfd, n;
        char recvline[MAXLINE+1];
        socklen_t len;
        struct sockaddr_storage ss;
    
        if(argc!=3)
            err_quit("usage: daytimetcpcli  ");
        
        sockfd=Tcp_connect(argv[1], argv[2]);
        len=sizeof(ss);
        Getpeername(sockfd, (SA *)&ss, &len);
        printf("connected to %s\n", Sock_ntop_host((SA *)&ss, len));
    
        while((n=Read(sockfd, recvline, MAXLINE))>0)
        {
            recvline[n]=0;
            Fputs(recvline,stdout);
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    Daytime client recorded to use tcp_connect

    ‘tcp_listen’ Function

    Our next function, tcp_listen, performs the normal TCP server steps: create a TCP socket, bind the server’s well-known port, and allow incoming connection requests to be accepted. Figure 11.12 shows the source code.

    #include "unp.h"
    int tcp_listen (const char *hostname, const char *service, socklen_t *addrlenp);
    
    • 1
    • 2
  • 相关阅读:
    『第十一章』数据持久化:CoreData 与 CloudKit
    英语学术论文简短语句摘抄
    【LeetCode】58. 最后一个单词的长度
    理论修炼---JVM之内存结构
    视频批量添加背景图片教程,详细步骤一看就会
    第六章:Java内存模型之JMM
    ABB电磁流量计维修信号变送器维修41F/E4技术参数
    netty系列之:netty对marshalling的支持
    蓝桥杯——递增序列和货物摆放
    取消本次commit,git远程和本地同步,SourceTree分支合并
  • 原文地址:https://blog.csdn.net/myfather103/article/details/82705198