How to test if a given socket descriptor is valid?

J

jungleman

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.
 
V

Victor Bazarov

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
 
N

Noah Roberts

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.
 
J

Jorgen Grahn

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
 
W

Werner

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
 
W

Werner

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
 
J

Jorgen Grahn

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).


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]

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

/Jorgen
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top