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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:33:38+00:00 2026-06-14T20:33:38+00:00

In my C application, I wait for data on the socket in the following

  • 0

In my C application, I wait for data on the socket in the following way:

printf("Opening socket and wait for data.\n");
while (i < 5)  
   while((connection_fd = accept(socket_fd, 
                            (struct sockaddr *) &address,
                            &address_length)) > -1)
   {
    bzero(buffer, 64);
    n = read(connection_fd,buffer,64);
    if (n < 0) printf("ERROR reading from socket");
    printf("Here is the message of length %d bytes:\n\n", n);
    for (int i = 0; i < n; i++)
    {
      printf("%02X", buffer[i]);
    } 
    printf("\n\n");          
    break;  
    }
 i++
 }

That means I read 5 times data from the Socket, however, by the looks of it I seem to be opening 5 different connections is that right? Is it possible to open the connection just once, keep it alive, and then check whether there is any data available on this connection?

Thanks, Patrick!

  • 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-14T20:33:39+00:00Added an answer on June 14, 2026 at 8:33 pm

    your code needs some restructuring, you should only accept once for each new connection:

    while (1) {
        connection_fd = accept(socket_fd, ...);
    
        /* check for errors */
        if (connection_fd < 0) {
          /* handle error */
        }
    
        /* note this could block, if you don't want
           that use non-blocking I/O and select */    
        while ((n=read(connection_fd, buf, ...)) > 0) {
            /* do some work */
        }
    
        /* close fd */ 
        close(fd);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to get send() to wait until all the data that
The WaitNamedPipe function allows a pipe client application to synchronously wait for an available
Application is running in my local system app.config file <add key=dbconn value=Data Source=(local);database=VTech;User ID=sa;Password=sa;
My C# application is such that a background worker is being used to wait
My Flex application does a remote call to weborb to save some data from
I want to send data from a c# application to a c++ application through
I have a wpf xbap application that downloads some data in background. There is
I am developing an iPhone application which is completely based on web data. If
I'm working on an asp.net application that allows the user to update data for
We are implementing a C# application that needs to make large numbers of socket

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.