Search code examples
c++cserversocket

Unusual Server behavior with sockets


When writing my server code I have this line:

newsockfd =  accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);

When I run the program I get no errors, but the program just freezes, and I put a print statement at the first line of the main() (so it should run before anything runs) but the print statement never gets executed.

This line of code is definitely the problem because once I comment it out, my print statements work.

What might create such bizarre behavior?
(I'm not allowed to post homework code, so unfortunately I can't post all of it)


Solution

  • Since this was apparently the answer, I'll write it here: If your printf format strings don't end with "\n", then they'll be buffered until either you do print a newline or your program exits. (I'm simplifying a bit.) Since your accept call stopped your program after that output was buffered, you couldn't see the output even though the printf calls were working fine.