Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8589119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:58:17+00:00 2026-06-11T22:58:17+00:00

I am currently creating an echo server that disconnects clients after a maxWaitTime of

  • 0

I am currently creating an echo server that disconnects clients after a maxWaitTime of being in idle.

I was hoping the program would block the socket until the client sent data but when I run the program in gdb it goes through the select and blocks on Readline.

I know retval = 0 whenever it goes through the select and that the fd_set sock goes to [256, (31 zeroes)] and after the select, sock goes to [32 zeroes].

The accepting of the connection happens in another function and the connection descriptor is passed to the echo function.

If you are able to help point me in the right direction or let me know how I can disconnect a client after a certain amount of time please let me know.

If you require any further information please let me know.

Thanks in advance!

    FD_ZERO(&sock);
    FD_SET(sockfd,&sock);

    int opt = 3;

    setsockopt(sockfd, SOL_SOCKET, SO_RCVLOWAT,&opt,sizeof(opt));

    timeout.tv_sec = maxWaitTime;
    timeout.tv_usec = 0;

    for ( ; ; ) {
            FD_SET(sockfd,&sock);

            printf("Set is %d\n",FD_ISSET(sockfd,&sock));

            int retval;
            retval = select(1, &sock, NULL, NULL, &timeout);

            if(retval)
            {
                    quitProgram(number);
            }
            else
            {
            printf("n is %d\n",retval);

            if ( (n = Readline(sockfd, line, MAXLINE)) == 0)
            {
                    return;         /* connection closed by other end */
            }

            Writen(sockfd, line, n);

    }

`

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:58:19+00:00Added an answer on June 11, 2026 at 10:58 pm

    As others have commented, you have some logic holes in your code. By your own admission:

    I know retval = 0 whenever it goes through the select and that the fd_set sock goes to [256, (31 zeroes)] and after the select, sock goes to [32 zeroes].

    That should have been an indication to you that something was going wrong. The socket was not in the fd_set after select() exited, which meant the socket was not readible yet. retval=0 means select() timed out.

    You have to reset not only the fd_set every time select() is called, but also the timeval as well. Try this instead:

    int opt = 3; 
    setsockopt(sockfd, SOL_SOCKET, SO_RCVLOWAT,&opt,sizeof(opt)); 
    
    for ( ; ; )
    { 
        timeout.tv_sec = maxWaitTime; 
        timeout.tv_usec = 0; 
    
        FD_ZERO(&sock); 
        FD_SET(sockfd,&sock); 
    
        int retval = select(sockfd+1, &sock, NULL, NULL, &timeout); 
        if (retval <= 0) 
        { 
            quitProgram(number); /* error or connection timed out */
        } 
        else 
        { 
            if ( (n = Readline(sockfd, line, MAXLINE)) <= 0) 
            { 
                return; /* error or connection closed by other end */ 
            } 
    
            Writen(sockfd, line, n); 
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm after creating a webpage that includes a blog section and it currently displays
I'm currently creating an iPhone app (Xcode 4.3.1, IOS 5) that could use Bluetooth
im creating a website that requires user authorisation to access some features. im currently
I'm creating a web application that uploads websites from my server to youtube on
i'm currently creating a piece of code which should simply show the text that
im currently creating a graph for an app, using coreplot and have a problem
I am currently creating an application for compiling multiple java projects in one go
I'm currently creating a payment SDK for android, as such I want to send
I'm currently creating a iOS 5 iPad app where there will be heavy network
I'm currently creating a neighbourhood graph by doing roughly this: for every voxel look

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.