ACE Client/Server Connection

Posted on 2006-06-06
Medium Priority
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
  • 2
LVL 53

Accepted Solution

Infinity08 earned 2000 total points
ID: 16842719
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

ID: 16842781
Thank you... do you know of any examples of this...

I am doing everything linux.
LVL 17

Expert Comment

ID: 16842985
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

ID: 16843200
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;

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

850 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