Getting the status of a socket through code

Discussion in 'C++' started by oshaer, Dec 11, 2011.

  1. oshaer

    oshaer Guest

    Hi Everybody


    I need to write a code that forcibly closes connections that are in
    CLOSE_WAIT status. It should run at a father process and constantly
    check the forked child processes(servers). I need this code to work on
    Unix/Linux, thus usage of WINAPI is not possible.

    My question is:

    1) Should I use netstat through code and parse the result to learn
    which socket is in a CLOSE_WAIT situation?

    2) Is there any C++ code that enables me to easily get socket status,
    given the file descriptor number?

    3) If you have any other ideas/recommendations please share.

    Thanks a lot.
    Ofira.
    oshaer, Dec 11, 2011
    #1
    1. Advertising

  2. On 12/11/2011 9:41 AM, oshaer wrote:
    > I need to write a code that forcibly closes connections that are in
    > CLOSE_WAIT status. It should run at a father process and constantly
    > check the forked child processes(servers). I need this code to work on
    > Unix/Linux, thus usage of WINAPI is not possible.
    >
    > My question is:
    >
    > 1) Should I use netstat through code and parse the result to learn
    > which socket is in a CLOSE_WAIT situation?
    >
    > 2) Is there any C++ code that enables me to easily get socket status,
    > given the file descriptor number?
    >
    > 3) If you have any other ideas/recommendations please share.


    Sockets are platform-specific. Please consider asking in the newsgroup
    dedicated to your OS/platform.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 11, 2011
    #2
    1. Advertising

  3. Leigh Johnston <> wrote:
    > On 11/12/2011 14:41, oshaer wrote:
    >> Hi Everybody
    >>
    >>
    >> I need to write a code that forcibly closes connections that are in
    >> CLOSE_WAIT status. It should run at a father process and constantly
    >> check the forked child processes(servers). I need this code to work on
    >> Unix/Linux, thus usage of WINAPI is not possible.
    >>
    >> My question is:
    >>
    >> 1) Should I use netstat through code and parse the result to learn
    >> which socket is in a CLOSE_WAIT situation?
    >>
    >> 2) Is there any C++ code that enables me to easily get socket status,
    >> given the file descriptor number?
    >>
    >> 3) If you have any other ideas/recommendations please share.

    >
    > Off-topic.
    >
    > HTH.
    >
    > /Leigh


    Not everyone knows that sockets are platform specific and there is no
    standard C++ API. You could at least point that out.

    To the topic, I don't think that it is possible to close connections of a
    different process. You can just kill those processes.
    But a connection in CLOSE_WAIT just means that the other peer has already
    closed the connection. If the child process is implemented correctly, it
    just closes those connections and there won't be any zombie connections.

    Tobi
    Tobias Müller, Dec 11, 2011
    #3
  4. On Monday, December 12, 2011 1:15:07 AM UTC+8, Tobias Müller wrote:
    > Leigh Johnston <> wrote:
    > > On 11/12/2011 14:41, oshaer wrote:
    > >> Hi Everybody
    > >>
    > >>
    > >> I need to write a code that forcibly closes connections that are in
    > >> CLOSE_WAIT status. It should run at a father process and constantly
    > >> check the forked child processes(servers). I need this code to work on
    > >> Unix/Linux, thus usage of WINAPI is not possible.
    > >>
    > >> My question is:
    > >>
    > >> 1) Should I use netstat through code and parse the result to learn
    > >> which socket is in a CLOSE_WAIT situation?
    > >>
    > >> 2) Is there any C++ code that enables me to easily get socket status,
    > >> given the file descriptor number?
    > >>
    > >> 3) If you have any other ideas/recommendations please share.

    > >
    > > Off-topic.
    > >
    > > HTH.
    > >
    > > /Leigh

    >
    > Not everyone knows that sockets are platform specific and there is no
    > standard C++ API. You could at least point that out.
    >
    > To the topic, I don't think that it is possible to close connections of a
    > different process. You can just kill those processes.
    > But a connection in CLOSE_WAIT just means that the other peer has already
    > closed the connection. If the child process is implemented correctly, it
    > just closes those connections and there won't be any zombie connections.
    >
    > Tobi


    This kind of trivial jobs in shell scripts that can be invoked by a C++ program
    to spawn a process is really boring.

    In POSIX compatible OSes the standard output can be directed to a file.
    88888 Dihedral, Dec 11, 2011
    #4
  5. oshaer

    Jorgen Grahn Guest

    On Sun, 2011-12-11, oshaer wrote:
    > I need to write a code that forcibly closes connections that are in
    > CLOSE_WAIT status. It should run at a father process and constantly
    > check the forked child processes(servers). I need this code to work on
    > Unix/Linux, thus usage of WINAPI is not possible.
    >
    > My question is:
    >
    > 1) Should I use netstat through code and parse the result to learn
    > which socket is in a CLOSE_WAIT situation?
    >
    > 2) Is there any C++ code that enables me to easily get socket status,
    > given the file descriptor number?
    >
    > 3) If you have any other ideas/recommendations please share.


    Like others mentioned, go seek advice in some Unix/Linux/POSIX group,
    and/or refer to W R Stevens' books. Also mention there what the actual
    problem is that you're trying to solve -- the way you're trying to do
    it is almost certainly the wrong approach.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Dec 11, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,757
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    942
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    524
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    643
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    698
Loading...

Share This Page