Read_timeout experiment in UNIX Network Programming

Source: Internet
Author: User
Tags htons

Recently, in a project, a TCP-based network programming for the server and client is required. Because the data returned by the server is not so timely, some latency needs to be done on the client, then you can read the data. The test results are as follows.

 

First, let's take a look at a encapsulated read latency function:

# Define err_exit (m) do {perror (m); exit (exit_failure);} while (0)/*** read timeout-read timeout detection function, excluding read Operations * @ FD: file descriptor * @ wait_seconds: Waiting For timeout seconds. If the value is 0, the system returns 0 if the detection does not detect timeout * If the operation succeeds (no timeout), and-1 if the operation fails, timeout returns-1 and errno = etimeout */INT read_timeout (int fd, unsigned int wait_seconds) {int ret; If (wait_seconds> 0) {fd_set read_fdset; struct timeval timeout; fd_zero (& read_fdset); fd_set (FD, & read_fdset); timeout. TV _sec = wait_seconds; timeout. TV _usec = 0; do {ret = select (FD + 1, & read_fdset, null, null, & timeout) ;}while (Ret <0 & errno = eintr ); if (ret = 0) // fail {// time out. ret =-1; errno = etimedout;} else if (ret = 1) // success {ret = 0 ;}} return ret ;}

 

The following describes how to use this function. The pseudocode is as follows:

Int ret = read_timeout (sockfd, 15 );

If (ret = 0)

{

Readnum = read (sockfd, recvbuff, sizeof (recvbuff ));

}

Else if (ret =-1 & errno = etimedout)

{

// Time Out dealing.

}

 

Okay. Now let's continue watching my server and client programs:

Server

void str_echo(int sock){    ssize_t n;    char buff[1024];    again:    while( (n = read(sock, buff, sizeof(buff))) > 0)    {        fputs(buff, stdout);        sleep(3);//for testing client read_timeout        write(sock, buff, n);    }    if(n < 0 && errno == EINTR)    {        goto again;    }    else if(n < 0)    {        ERR_EXIT("read");    }}int main(){    int listenfd, connfd;    pid_t childpid;    socklen_t clilen;    struct sockaddr_in servaddr, cliaddr;        if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)    {        ERR_EXIT("socket");    }    memset(&servaddr, 0, sizeof(servaddr));    servaddr.sin_family = AF_INET;    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);    servaddr.sin_port = htons(5188);    if((bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) < 0)    {        ERR_EXIT("bind");    }    if( (listen(listenfd, SOMAXCONN)) < 0)    {        ERR_EXIT("listen");    }    for(;;)    {        clilen = sizeof(cliaddr);        connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen);        if(connfd < 0)        {            ERR_EXIT("connect");        }        if( (childpid = fork()) == 0)        {            //child            close(listenfd);            str_echo(connfd);            exit(0);        }        else        {            //parent            close(connfd);        }    }}

Client

void str_cli(int sock){    char sendbuff[1024];    char recvbuff[1024];    memset(sendbuff, 0, sizeof(sendbuff));    memset(recvbuff, 0, sizeof(recvbuff));    int ret = -1;    //ssize_t n;    while(fgets(sendbuff, sizeof(sendbuff), stdin) != NULL)    {        write(sock, sendbuff, strlen(sendbuff));            ret = read_timeout(sock, 15);        if(ret == 0)        {            read(sock, recvbuff, sizeof(recvbuff));        }        else if(ret == -1 && errno == ETIMEDOUT)        {            ERR_EXIT("read_timeout");        }        fputs(recvbuff, stdout);    }}int main(int argc, char **argv){        int sockfd;    struct sockaddr_in servaddr;    memset(&servaddr, 0, sizeof(servaddr));    servaddr.sin_family = AF_INET;    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");    servaddr.sin_port = htons(5188);    sockfd = socket(AF_INET, SOCK_STREAM, 0);    if(sockfd < 0)    {        ERR_EXIT("socket");    }    if( (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) < 0)    {        ERR_EXIT("connect");    }    str_cli(sockfd);    return 0;}

 

Example 1:

3 S server latency, client read_timeout (socd, 5); Result: Success

 

Read_timeout experiment in UNIX Network Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.