Thread: Correct post processing in multi-threaded server?

  1. #1
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355

    Correct post processing in multi-threaded server?

    Hi and thanks for any help.

    I am writing a multi-threaded server but since i am still at the testing phase, i sometimes send a SIGINT to it (Ctrl-C) to stop it. But when i try to put it back up, the socket is still bound to the address. So i thought to capture sigint() and handle cleanup myself, but the POSIX man pages for signal, indicate that the behaviour of signal() is unspecified when in multi-threaded environment.

    Also, i am not sure whether this is the correct thread to post.

    You thoughts on this? My testing of the code revealed it correctly released the port number, but i am not sure whether using 'unspecified' solutions is ok...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    For testing, it's probably OK.

    But you should generally use sigaction() instead of signal(), and I can't find any such notice in the man page for that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The reason this happens is that the port number is reserved for some set amount of time after closing the socket. This is to avoid picking up extremely delayed, stale packets from the wrong connection.

    It has nothing to do with SIGINT, or failing to clean up the socket. This is something happening down in the network stack. Before you bind() your socket, do:

    Code:
    int reuse = 1;
    setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
    Then your bind() should work.

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Thanx brewbuck! That actually is what i wanted, and i avoid the signal handler too, which forced me to global some variables i didn't want. The POSIX docs are a little tough to read...
    Last edited by xuftugulus; 03-07-2008 at 05:13 PM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by xuftugulus View Post
    Thanx brewbuck! That actually is what i wanted, and i avoid the signal handler too, which forced me to global some variables i didn't want. The POSIX docs are a little tough to read...
    You probably want to handle signals anyway though, so that you can close your clients gracefully. Unfortunately, global variables (actually, global volatile sig_atomic_t's) are the only way to communicate between the signal handler and the rest of the program, aside from one very awful trick that doesn't deserve to be described...

    EDIT: Actually, there are a few other ways, ranging on the spectrum of awfulness.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  2. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 11:21 PM
  3. Client works on a LAN but don't send all the data
    By Niara in forum Networking/Device Communication
    Replies: 9
    Last Post: 01-04-2007, 04:44 PM
  4. Post processing shaders
    By VirtualAce in forum Game Programming
    Replies: 9
    Last Post: 12-12-2006, 05:06 AM
  5. Another multi boot Newbie Linux post!
    By Bajanine in forum Tech Board
    Replies: 4
    Last Post: 06-18-2004, 09:19 PM