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);
#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;
}

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;
}

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);

你可能感兴趣的:(Unix,Network,Programming,unix,网络,tcp/ip)