Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to test if a given socket descriptor is valid? (http://www.velocityreviews.com/forums/t805542-how-to-test-if-a-given-socket-descriptor-is-valid.html)

jungleman 11-04-2011 03:52 PM

How to test if a given socket descriptor is valid?
 
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.

Victor Bazarov 11-04-2011 04:23 PM

Re: How to test if a given socket descriptor is valid?
 
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

Noah Roberts 11-04-2011 06:55 PM

Re: How to test if a given socket descriptor is valid?
 
On Nov 4, 8:52*am, jungleman <ustcrevolution...@gmail.com> 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.

Jorgen Grahn 11-07-2011 11:27 PM

Re: How to test if a given socket descriptor is valid?
 
On Fri, 2011-11-04, Noah Roberts wrote:
> On Nov 4, 8:52*am, jungleman <ustcrevolution...@gmail.com> 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 .

Werner 11-08-2011 05:57 AM

Re: How to test if a given socket descriptor is valid?
 
On Nov 8, 1:27*am, Jorgen Grahn <grahn+n...@snipabacken.se> 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 11-08-2011 06:09 AM

Re: How to test if a given socket descriptor is valid?
 
On Nov 4, 5:52*pm, jungleman <ustcrevolution...@gmail.com> 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


Jorgen Grahn 11-08-2011 11:29 PM

Re: How to test if a given socket descriptor is valid?
 
On Tue, 2011-11-08, Werner wrote:
> On Nov 8, 1:27*am, Jorgen Grahn <grahn+n...@snipabacken.se> 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 .


All times are GMT. The time now is 05:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.