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 8154995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:32:12+00:00 2026-06-06T16:32:12+00:00

I have the code below implementing a NON-Blocking TCP acceptor. Clients are able to

  • 0

I have the code below implementing a NON-Blocking TCP acceptor. Clients are able to connect without any problem and the writing seems occurring as well, but the acceptor doesn’t read anything from the socket and the call to read() blocks indefinitely.
Am I using some wrong setting for the acceptor?

Kind Regards
AFG

int main(){
    create_programming_socket();
    poll_programming_connect(); 
    while(1){
        poll_programming_read();
    }    
}


int create_programming_socket(){
   int cnt = 0;
   p_listen_socket = socket( AF_INET, SOCK_STREAM, 0 );
   if( p_listen_socket < 0 ){
       return 1;
   }      

   int flags = fcntl( p_listen_socket, F_GETFL, 0 );
   if( fcntl( p_listen_socket, F_SETFL, flags | O_NONBLOCK ) == -1 ){
       return 1;
   }

   bzero( (char*)&p_serv_addr, sizeof(p_serv_addr) );     

   p_serv_addr.sin_family = AF_INET;
   p_serv_addr.sin_addr.s_addr = INADDR_ANY;
   p_serv_addr.sin_port = htons( p_port );    

   if( bind( p_listen_socket, (struct sockaddr*)&p_serv_addr
   , sizeof(p_serv_addr) ) < 0 ) {
       return 1;
   }
   listen( p_listen_socket, 5 );
   return 0;
}


int poll_programming_connect(){
   int retval = 0;   
   static socklen_t p_clilen = sizeof(p_cli_addr);   
   int res = accept( p_listen_socket, (struct sockaddr*)&p_cli_addr, &p_clilen );
   if( res > 0 ){
       p_conn_socket = res;
       int flags = fcntl( p_conn_socket, F_GETFL, 0 );
       if( fcntl( p_conn_socket, F_SETFL, flags | O_NONBLOCK ) == -1 ){   
           retval = 1;
       }else{
           p_connected = true;       
       }
    }else if( res == -1 && ( errno == EWOULDBLOCK || errno == EAGAIN ) ) {
    //printf( "poll_sock(): accept(c_listen_socket) would block\n");    
    }else{
       retval = 1;
   }
   return retval;
  }

  int poll_programming_read(){
   int retval = 0;
   bzero( p_buffer, 256 );   
   int numbytes = read( p_conn_socket, p_buffer, 255 );
   if( numbytes > 0 ) {
       fprintf( stderr, "poll_sock(): read() read %d bytes\n", numbytes );
       int fred;
        int i;
   } else if( numbytes == -1 && ( errno == EWOULDBLOCK || errno == EAGAIN ) ) {
    //printf( "poll_sock(): read() would block\n");
   } else {
       close( p_conn_socket );
       p_connected = false;
       retval = 1;
   }
   return retval;
 }
  • 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-06T16:32:14+00:00Added an answer on June 6, 2026 at 4:32 pm

    The call to poll_programming_connect is outside your while loop – so it most likely doesn’t accept a connection (fails with EWOULDBLOCK). So you don’t have a valid socket to read from.

    You need to wait until you have a connection before calling poll_programming_read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with the code below. Basically im implementing a polling mechanism,
I have code below: <select id=testSelect> <option value=1>One</option> <option value=2>Two</option> </select> <asp:Button ID=btnTest runat=server
I have code as below: var s : String = hello world var xml
I have the code below : public class Anything { public int Data {
I have to code below - works great in IE and Opera, but does
I have the code below with 3 columns. I want to have the border
I have the code below that hides and shows the navigational bar. It is
I have the code below in my test code in many places: // //
I have the code below: using (SqlCommand command = new SqlCommand()) { command.CommandType =
I have the code below for getting a list of child processes on windows

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.