ACE Client/Server Connection

Posted on 2006-06-06
Last Modified: 2012-06-22
I have a C++ Server written using the ACE acceptot/service model.

When something is connected to the server and I stop the C++ app (by using ctrl-C) it is unhappy when I try and bring it back up.  It seems like the port remains open or something to that nature.  Is there way to make sure the port gets closed happily when the program is interupted?  Is there way to force close the port at startup?

Question by:DanRaposo
    LVL 53

    Accepted Solution

    You'll have to gracefully close the application (ie. release all resources, including sockets).

    Use signal() to catch the Ctrl+C interrupt, and then treat it accordingly ...

    Note, that for Win32, you can use the SetConsoleCtrlHandler() API :

    Author Comment

    Thank you... do you know of any examples of this...

    I am doing everything linux.
    LVL 17

    Expert Comment

    If you open your own socket for listening, you can set SO_REUSEADDR to allow the address to be re-used even if it is in the TIME_WAIT state, which it will be in if it has not been cleanly closed.

          int opt = 1;
          if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char*)&opt,sizeof(opt)) < 0) {

    If your ACE library(?) hides the socket from you, you will not have this option.
    LVL 53

    Expert Comment

    Something like this :

        #include <signal.h>

        void sigproc() {
          signal(SIGINT, sigproc); /* set the signal handler again in case it was reset to the default */
          // handle Ctrl+C

        int main() {
          signal(SIGINT, sigproc);
          /* your code */
          return 0;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now