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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:23:12+00:00 2026-06-18T12:23:12+00:00

This is a bit of an odd one. My code wasn’t outputting what I

  • 0

This is a bit of an odd one. My code wasn’t outputting what I thought it should. I added some print statements at various stages to see where it was going wrong. Still nothing.
So I added a printf statement at the start of main. That’s where I got really confused.

So I presumed something funny was happening with the file descriptors. I changed the printf to a fprintf. Still nothing. Printing to stderr with fprintf does work! Why is this happening?

Removing all of the body from main except the initial print statement and the return does print.

Code

int main(void) {
    fprintf(stdout, "STARTED!");
    //Create an Internet domain socket
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    //If this fails exit and print the error
    if (sockfd == -1) {
        printf("Error %d, cannot create socket", errno);
        return 1;
    }
    printf("SOCKET CREATED!");

    //Creates a socket address
    struct sockaddr_in  addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8080);
    addr.sin_addr.s_addr = INADDR_ANY;

    //Attempts to bind to the socket address, again prints to error if this fails.
    if (bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
    {
        printf("Error %d, cannot bind", errno);
        return 1;
    }

    //Starts Listening for a client
    if (listen(sockfd, 1) == -1)
    {
        printf("Error %d, cannot listen", errno);
        return 1;
    }

    //If all is successful, server is operational
    while(1)
    {
        //Creates a file descripter for the connection
        int connfd;
        //And a socket address for the client
        struct sockaddr_in cliaddr;
        socklen_t cliaddrlen = sizeof(cliaddr);
        //If a connection attempt is made accepts it.
        connfd = accept(sockfd, (struct sockaddr *) &cliaddr, &cliaddrlen);
        if (connfd == -1) {
            //If the connection fails print an error
            printf("Error %d, cannot accept connection", errno);
            continue;
        }

        //Otherwise process the request
        else {
            printf("CONNECTED!");
            char end;
            end = 1;
            while (end)
            {
                process_request(connfd);
                end = 0;
            }
        }
        close(connfd);

    }
    close(sockfd);
    return 0;
}
  • 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-18T12:23:14+00:00Added an answer on June 18, 2026 at 12:23 pm

    Output is often buffered by the system. You can call fflush, but sometimes, depending on how the caching works, simply ending the output with a newline is sufficient. So try changing

    fprintf(stdout, "STARTED!");
    

    to

    fprintf(stdout, "STARTED!\n");
    

    And, if that doesn’t help, to

    fprintf(stdout, "STARTED!\n");
    fflush(stdout)
    

    (And stderr often isn’t cached, as you want to see errors immediately.)

    Finally, you will see output when the program finishes (as things are flushed then), which probably explains the rest of the behaviour.

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

Sidebar

Related Questions

So this one is a bit odd to describe, please bear with me :)
This is a bit of an odd one and apologies if I don't explain
This is an odd one. Note this is cut down example code, and misses
This is a bit of an odd one and was wondering if anyone had
This is a bit of an odd one. I'm trying to stub a method
This is a bit odd, but is it possible to use an NSCalendar (or
This might be a little bit odd question, but Im trying to figure out
This is a bit of an odd situation. Here's what's happening. So, we have
This is a bit of an odd question and more of a though experiment
This might sound a bit of an odd question but I know what I

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.