How to test if a given socket descriptor is valid?

Discussion in 'C++' started by jungleman, Nov 4, 2011.

  1. jungleman

    jungleman Guest

    Hi,

    In my program, I will use a socket descriptor passed by another
    function, for the robust of the program, I wanna test if the given
    socket descriptor is valid!

    How would I achieve this goal?

    PS: I program with C++ in Linux.


    Thanks in advance.
     
    jungleman, Nov 4, 2011
    #1
    1. Advertising

  2. On 11/4/2011 11:52 AM, jungleman wrote:
    > In my program, I will use a socket descriptor passed by another
    > function, for the robust of the program, I wanna test if the given
    > socket descriptor is valid!
    >
    > How would I achieve this goal?
    >
    > PS: I program with C++ in Linux.


    So post to the Linux newsgroup. There is no such thing as "socket" in
    C++ *language* or *library*, it's all OS-specific.

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

  3. jungleman

    Noah Roberts Guest

    On Nov 4, 8:52 am, jungleman <> wrote:
    > Hi,
    >
    > In my program, I will use a socket descriptor passed by another
    > function, for the robust of the program, I wanna test if the given
    > socket descriptor is valid!
    >
    > How would I achieve this goal?
    >
    > PS: I program with C++ in Linux.
    >
    > Thanks in advance.


    You can't. You can only verify that it's within the range of viable
    sockets and then try to use it. Using it will cause error returns and
    errno to be set if it's not a valid socket.
     
    Noah Roberts, Nov 4, 2011
    #3
  4. jungleman

    Jorgen Grahn Guest

    On Fri, 2011-11-04, Noah Roberts wrote:
    > On Nov 4, 8:52 am, jungleman <> wrote:
    >> Hi,
    >>
    >> In my program, I will use a socket descriptor passed by another
    >> function, for the robust of the program, I wanna test if the given
    >> socket descriptor is valid!
    >>
    >> How would I achieve this goal?
    >>
    >> PS: I program with C++ in Linux.
    >>
    >> Thanks in advance.

    >
    > You can't. You can only verify that it's within the range of viable
    > sockets and then try to use it. Using it will cause error returns and
    > errno to be set if it's not a valid socket.


    It depends on his definition of "valid socket descriptor", doesn't it?
    He didn't define that term, so we cannot say.

    More on-topic, if he wants one particular, limited kind of robustness
    he could write a small RAII wrapper class which can only contain
    correctly opened sockets.

    Personally I find that with the right overall design, this is not a
    problem. You can give some higher-level object ownership of the socket,
    and let /it/ worry about validity.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Nov 7, 2011
    #4
  5. jungleman

    Werner Guest

    On Nov 8, 1:27 am, Jorgen Grahn <> wrote:
    > On Fri, 2011-11-04, Noah Roberts wrote:


    > > You can't.  You can only verify that it's within the range of viable
    > > sockets and then try to use it.  Using it will cause error returns and
    > > errno to be set if it's not a valid socket.

    >
    > It depends on his definition of "valid socket descriptor", doesn't it?
    > He didn't define that term, so we cannot say.


    We have a saying in my language - "'n helder verstand het net 'n
    halwe woord nodig". Dutch might help you translate :). But point
    taken - this is a C++ language newsgroup. I've just look at draft 3035
    and
    I've noticed the words socket and descriptor (amongst header
    system_error).

    > More on-topic, if he wants one particular, limited kind of robustness
    > he could write a small RAII wrapper class which can only contain
    > correctly opened sockets.


    The word "RAII" does not exist in the standard. Care to
    define it? Just kidding.

    For the RAII wrapper to guarantee a valid descriptor,
    it would have to open it during construction (otherwise
    we have the same problem).
    - Seeing that it is a RAII wrapper, I'm assuming it
    will close it during destruction.
    - Is it copyable?
    - What happens to the descriptor during copying. Is
    ownership transferred to the receiver?
    - Are we going to make the socket descriptor public? Because
    if we don't, it might be closed... How will we handle
    failure to close in our RAII wrapper (during destruction)?

    If we [do] write this RAII wrapper, why not give it some
    functions as indication what the intent of this particular
    descriptor is, seeing that this intent is not clear when
    looking at type SOCKET or int...

    The there exists the question of whether operations
    will complete asynchronously or synchronously, and how
    that relates to our RAII object... How would we expose
    our descriptor (Listening for clients, connecting to servers,
    and receiving having the potential of completing async).
    etc...

    >
    > Personally I find that with the right overall design, this is not a
    > problem. You can give some higher-level object ownership of the socket,
    > and let /it/ worry about validity.


    Agreed.

    Kind regards,

    Werner
     
    Werner, Nov 8, 2011
    #5
  6. jungleman

    Werner Guest

    On Nov 4, 5:52 pm, jungleman <> wrote:
    > Hi,
    >
    > In my program, I will use a socket descriptor passed by another
    > function, for the robust of the program, I wanna test if the given
    > socket descriptor is valid!
    >
    > How would I achieve this goal?


    This is one way in which you can achieve this (in c++).

    struct DescriptorCreator
    {
    typedef int fd_type;
    //Shall either return a new valid descriptor or throw...
    virtual fd_type create() const = 0;
    virtual ~DescriptorMaker(){ }
    };

    void myFunction( const DescriptorCreator& creator )
    {
    fd_type fd = creator.create();
    }

    You can be more elaborate wrt. the type of
    descriptor that you want to create, e.g:

    FileDescriptorCreator or TcpSocketDescriptorCreator etc.

    Kind regards,

    Werner
     
    Werner, Nov 8, 2011
    #6
  7. jungleman

    Jorgen Grahn Guest

    On Tue, 2011-11-08, Werner wrote:
    > On Nov 8, 1:27 am, Jorgen Grahn <> wrote:
    >> On Fri, 2011-11-04, Noah Roberts wrote:

    >
    >> > You can't.  You can only verify that it's within the range of viable
    >> > sockets and then try to use it.  Using it will cause error returns and
    >> > errno to be set if it's not a valid socket.

    >>
    >> It depends on his definition of "valid socket descriptor", doesn't it?
    >> He didn't define that term, so we cannot say.

    >
    > We have a saying in my language - "'n helder verstand het net 'n
    > halwe woord nodig". Dutch might help you translate :). But point
    > taken - this is a C++ language newsgroup. I've just look at draft 3035
    > and
    > I've noticed the words socket and descriptor (amongst header
    > system_error).
    >
    >> More on-topic, if he wants one particular, limited kind of robustness
    >> he could write a small RAII wrapper class which can only contain
    >> correctly opened sockets.

    >
    > The word "RAII" does not exist in the standard. Care to
    > define it? Just kidding.


    No, I was serious when I said I didn't understand what, in the mind of
    the OP, it means for a socket to be valid. Options include:
    - it's not -1
    - it's an open file descriptor
    - ... and it's referring to a socket
    - ... and it's a connected TCP socket
    - it won't be closed by that other thread until I've used it

    > For the RAII wrapper to guarantee a valid descriptor,
    > it would have to open it during construction (otherwise
    > we have the same problem).
    > - Seeing that it is a RAII wrapper, I'm assuming it
    > will close it during destruction.
    > - Is it copyable?


    Oops. That wrapper of mine was not such a hot idea.

    [snip more criticism]

    >> Personally I find that with the right overall design, this is not a
    >> problem. You can give some higher-level object ownership of the socket,
    >> and let /it/ worry about validity.

    >
    > Agreed.


    And that's probably why the wrapper idea went wrong -- I don't do it
    that way myself.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Nov 8, 2011
    #7
    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. Lord0
    Replies:
    1
    Views:
    573
    Thomas Weidenfeller
    Apr 19, 2006
  2. Rajarshi Guha
    Replies:
    0
    Views:
    358
    Rajarshi Guha
    Apr 7, 2004
  3. chiara
    Replies:
    6
    Views:
    478
    Barry Schwarz
    Oct 6, 2005
  4. 2Barter.net
    Replies:
    0
    Views:
    372
    2Barter.net
    Dec 13, 2006
  5. mbm

    [socket] test socket descriptor state

    mbm, Sep 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    428
    CBFalconer
    Sep 27, 2007
Loading...

Share This Page