Thread: I cant understand something in my server/client with sockets and fork

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    I cant understand something in my server/client with sockets and fork

    hey... This is not a problem per se, I am just curious just what I am doing wrong or if is something in the way things work and I dont know it
    So I am making a simple server/client using sockets where I use fork on the server to accomodate the client requests. I am just at the beggining but when I tested thing there was somethign I coudnt understand.
    So I am entering the server in an endless loop where it acccepts connections from the clients. When a connection is accepted fork() is called and the child takes over. As I understand it the parent goes on to accept new connection. It doesnt wait the child to finish. So i made the child send two messages to the child that prints them and then exit/return...

    The thing is when a connection is accpeted i print a message "ACCEPTED". Normally it should print once right, but for some reason it prints then the child printf's and then it prints again and then the parent printf's. Any idea why, Is there something I dont understan in the fork usage??
    I have the code in the files...


    Also, socket are integer that show to an index table right? So when the child is create we have two copies of the same integer so two copies pointing to the same index/socket. When I close a socket what exactly happens. I know I must close the listening socket in the child process so it wont accept connections as well (like the parent) but what exactly does close does?
    Last edited by Phoenix_Rebirth; 07-16-2009 at 06:53 AM.

  2. #2
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    The problem occurs because printf to stdout is buffered. When the fork is done, both parent and child get copies of the buffer. When the buffer is flushed, you get ACCEPTED printed twice. Do a fflush(NULL) before the fork and you'll get the expected behavior.

    On my system it looks like this:
    Child prints: ACCEPTED
    Child prints: This is the child speaking...
    Child prints: hello
    Child prints: hello again
    Parent prints: ACCEPTED
    Parent prints: This is the parent speaking...

    As you probably know, there is no guarantee of which will run first after the fork.

    The socket is an integer index into the process memory, the memory is not shared between processes, so closing the socket in child, does not affect the parent.

  3. #3
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Ahhhh, yes you are right.. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concurrent chat, Sockets
    By jakemott in forum Linux Programming
    Replies: 6
    Last Post: 11-29-2008, 05:41 PM
  2. Chat udp server/client
    By Phoenix_Rebirth in forum C Programming
    Replies: 1
    Last Post: 11-17-2008, 12:30 PM
  3. Question about forking with sockets listening
    By Phoenix_Rebirth in forum C Programming
    Replies: 2
    Last Post: 11-10-2008, 04:05 AM
  4. Unix sockets
    By karas in forum Linux Programming
    Replies: 8
    Last Post: 10-13-2007, 12:20 AM
  5. fork() and sockets
    By biosx in forum Linux Programming
    Replies: 2
    Last Post: 02-12-2002, 09:00 AM