Thread: Weird code execution order problem

  1. #1
    42
    Join Date
    May 2005
    Location
    Estonia
    Posts
    7

    Question Weird code execution order problem

    Hi
    I wrote a simple server in C and it seems like the functions are executed in the wrong order or something. (btw I'm on linux (ubuntu:hoary) if that matters anything in this case)

    it goes like this:
    Code:
    ...socket()...bind()...
    
    if(listen(sfd, 10) == -1) { //sfd = main socket
    	printf("\nFailed on listen(): Terminating!");
    	exit(1); 
    }
    printf("\nServer established---");
    
    while(1) { //A loop to accept incoming connections
    	cfd = accept(sfd, (struct sockaddr *)&client, &sin_size);
    	printf("\nIncoming connection");
    ...
    The program compiles and runs fine...

    So this should print "Server established" and then start accepting incoming connections but instead
    it enters the while() loop, waits until accept() returns for the first time and THEN prints "Server established" (but no "incoming connection")

    what's up with this

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Output isn't guaranteed to appear unless there's a newline. To force it to appear right away add fflush(stdout); right after your printf().
    If you understand what you're doing, you're not learning anything.

  3. #3
    42
    Join Date
    May 2005
    Location
    Estonia
    Posts
    7
    hey it works!
    So it was just a simple output problem. heh I thought it was something really messy

    thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  2. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 06:26 PM
  3. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  4. Input File HELP, weird problem
    By gravity-1 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 08:43 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM