PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Noms de domaine > comp.protocols.tcp-ip > How can we know if a socket descriptor is already allocated ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.protocols.tcp-ip TCP and IP network protocols.

How can we know if a socket descriptor is already allocated ?

Réponse
 
LinkBack Outils de la discussion
Vieux 08/05/2007, 06h10   #1
soj
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How can we know if a socket descriptor is already allocated ?

Hi Friends,

I've a question,

I just want to know if a socket descriptor (for eg descriptor 3) is
already allocated or not ?
Because based on that information I need to close a particular socket.

Kernel would be allocating only free socket descriptors whenever we
creates a socket using socket() system call. How it does that
checking?

Any information would be highly appreciated.

Thanks,
Sojin

  Réponse avec citation
Vieux 08/05/2007, 06h37   #2
robertwessel2@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can we know if a socket descriptor is already allocated ?

On May 8, 12:10 am, soj <jul....@gmail.com> wrote:
> Hi Friends,
>
> I've a question,
>
> I just want to know if a socket descriptor (for eg descriptor 3) is
> already allocated or not ?
> Because based on that information I need to close a particular socket.
>
> Kernel would be allocating only free socket descriptors whenever we
> creates a socket using socket() system call. How it does that
> checking?



The kernel has some sort of internal magic way of tracking open
descriptors. Your specific OS might provide you with some API that
allows you to determine if a specific descriptor is valid. You might
get away with doing something like a getsockopt() against the
descriptor in question, although what exactly that will do with an
invalid descriptor is going to be system specific.

Anyway, there's almost no reason why you'd actually want to do this,
requests like this usually start with faulty application logic. If
you need to track the state of a socket, you need to do so in your
application. The problem is that socket handles can be reused, and
you have no way of knowing if "descriptor 3" isn't being used
(validly) by some other part of your application. That descriptor
could be reused from your own code on a new connection, or perhaps
some other part of your application (or a library you use) might have
opened it.

In short knowing that "descriptor 3" is open, tells you almost nothing
of value, because there is no way to determine *why* it's open, or by
what part of your program. This gets even worse in threaded
applications. If you think you need to know this, you almost
certainly have a bug in your application.

  Réponse avec citation
Vieux 08/05/2007, 08h16   #3
soj
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can we know if a socket descriptor is already allocated ?

On May 8, 10:37 am, "robertwess...@yahoo.com"
<robertwess...@yahoo.com> wrote:
> On May 8, 12:10 am, soj <jul....@gmail.com> wrote:
>
> > Hi Friends,

>
> > I've a question,

>
> > I just want to know if a socket descriptor (for eg descriptor 3) is
> > already allocated or not ?
> > Because based on that information I need to close a particular socket.

>
> > Kernel would be allocating only free socket descriptors whenever we
> > creates a socket using socket() system call. How it does that
> > checking?

>
> The kernel has some sort of internal magic way of tracking open
> descriptors. Your specific OS might provide you with some API that
> allows you to determine if a specific descriptor is valid. You might
> get away with doing something like a getsockopt() against the
> descriptor in question, although what exactly that will do with an
> invalid descriptor is going to be system specific.
>
> Anyway, there's almost no reason why you'd actually want to do this,
> requests like this usually start with faulty application logic. If
> you need to track the state of a socket, you need to do so in your
> application. The problem is that socket handles can be reused, and
> you have no way of knowing if "descriptor 3" isn't being used
> (validly) by some other part of your application. That descriptor
> could be reused from your own code on a new connection, or perhaps
> some other part of your application (or a library you use) might have
> opened it.
>
> In short knowing that "descriptor 3" is open, tells you almost nothing
> of value, because there is no way to determine *why* it's open, or by
> what part of your program. This gets even worse in threaded
> applications. If you think you need to know this, you almost
> certainly have a bug in your application.


Thanks for your comments,

I'm using linux 2.6 kernel, for my protocol stacks.
The application doesnt want a STDIN and closes that. But lower layer
stack is reusing (by socket or accept system calls) the descriptor "0"
and application is trying to close it again.
Any way I got ride of this situation with a simple logic in
application (by checking the type of the descriptor).

Thanks a lot
Sojin



  Réponse avec citation
Vieux 08/05/2007, 14h09   #4
Roy Smith
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can we know if a socket descriptor is already allocated ?

"robertwessel2@yahoo.com" <robertwessel2@yahoo.com> wrote:
> In short knowing that "descriptor 3" is open, tells you almost nothing
> of value, because there is no way to determine *why* it's open, or by
> what part of your program.


It's funny that this came up today, because I was struggling with this
exact issue yesterday.

I've written a C++ class which manages a socket. Part of the specification
is that the socket is closed when the object is destroyed. I was looking
for a way to write a unit test which verifies that a given socket
descriptor is valid, then destroys the object under test, then verifies
that the descriptor was closed. The best I came up with was to just close
the descriptor myself; it I got EBADF, that meant it was already closed
(and the test passed).

I'll admit, however, that while that worked in the specific case of a
(single-threaded) unit test, it might not be a good plan in a more general
environment.
  Réponse avec citation
Vieux 08/05/2007, 17h56   #5
Vernon Schryver
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How can we know if a socket descriptor is already allocated ?

In article <1178608568.998717.231860@e65g2000hsc.googlegroups .com>,
soj <jul.sfx@gmail.com> wrote:

>I'm using linux 2.6 kernel, for my protocol stacks.
>The application doesnt want a STDIN and closes that. But lower layer
>stack is reusing (by socket or accept system calls) the descriptor "0"
>and application is trying to close it again.


The standard way of handling this old familiar problem is something like:

fd = open("/dev/null", O_RDONLY)
dup2(fd, 0);

(of course, with error checking on system calls,
and probably "STDIN_FILENO" instead of "0")

Reading source is a good thing. For example, if you read the source
for an implementation of the libc daemon() function, you'll probably
find similar work on stdout and stderr.


Vernon Schryver vjs@rhyolite.com
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h47.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10869 seconds with 13 queries