Unix Programming - Socket allready opened ?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > Socket allready opened ?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Socket allready opened ?
Christophe

2004-01-23, 5:05 pm

Hello...

I'm actualy programing socket in C. I've got a proc that initialise
my socket. In that proc I'd like to set up a control to know if my
socket allready exist, and in that case I'll close it (is there
anything else ?) and call again socket(AF_INET, SOCK_STREAM, 0);

Anyone could help me to find a way to know if my sock is already
opened ?

Thx a lot

Chris
Barry Margolin

2004-01-23, 5:06 pm

In article <3fd47aec.2240156@news.oleane.fr>,
chocman@libertysurf.fr (Christophe) wrote:
quote:

> Hello...
>
> I'm actualy programing socket in C. I've got a proc that initialise
> my socket. In that proc I'd like to set up a control to know if my
> socket allready exist, and in that case I'll close it (is there
> anything else ?) and call again socket(AF_INET, SOCK_STREAM, 0);
>
> Anyone could help me to find a way to know if my sock is already
> opened ?



Just close it without checking. If it's already closed this won't have
any effect.

--
Barry Margolin, barmar@alum.mit.edu
Woburn, MA
Barry Margolin

2004-01-23, 5:06 pm

In article <3fd47aec.2240156@news.oleane.fr>,
chocman@libertysurf.fr (Christophe) wrote:
quote:

> Hello...
>
> I'm actualy programing socket in C. I've got a proc that initialise
> my socket. In that proc I'd like to set up a control to know if my
> socket allready exist, and in that case I'll close it (is there
> anything else ?) and call again socket(AF_INET, SOCK_STREAM, 0);
>
> Anyone could help me to find a way to know if my sock is already
> opened ?



Just close it without checking. If it's already closed this won't have
any effect.

--
Barry Margolin, barmar@alum.mit.edu
Woburn, MA
Joseph Dionne

2004-01-23, 5:06 pm

Try getsockopt(...SO_ERROR...) for UNIX

If it is still opened, return will be zero, otherwise -1.

If you are working with the boot virus, formerly know as "Windows", use
ioctlsocket(...FIONREAD...). It will return SOCKET_ERROR if the socket
handle is closed, by either end, or zero, a connected socket.

WARNING: don't use getsockopt(), before using ioctlsocket() to verify
the socket is still "valid" first on Windows. I found winsock to segv
when used on a socket that is no longer connected, no matter which side
disconnected first.

Barry Margolin wrote:
quote:

> In article <3fd47aec.2240156@news.oleane.fr>,
> chocman@libertysurf.fr (Christophe) wrote:
>
>
>
>
> Just close it without checking. If it's already closed this won't have
> any effect.
>



Joseph Dionne

2004-01-23, 5:06 pm

Try getsockopt(...SO_ERROR...) for UNIX

If it is still opened, return will be zero, otherwise -1.

If you are working with the boot virus, formerly know as "Windows", use
ioctlsocket(...FIONREAD...). It will return SOCKET_ERROR if the socket
handle is closed, by either end, or zero, a connected socket.

WARNING: don't use getsockopt(), before using ioctlsocket() to verify
the socket is still "valid" first on Windows. I found winsock to segv
when used on a socket that is no longer connected, no matter which side
disconnected first.

Barry Margolin wrote:
quote:

> In article <3fd47aec.2240156@news.oleane.fr>,
> chocman@libertysurf.fr (Christophe) wrote:
>
>
>
>
> Just close it without checking. If it's already closed this won't have
> any effect.
>



David Schwartz

2004-01-23, 5:06 pm


"Christophe" <chocman@libertysurf.fr> wrote in message
news:3fd47aec.2240156@news.oleane.fr...
quote:

> I'm actualy programing socket in C. I've got a proc that initialise
> my socket. In that proc I'd like to set up a control to know if my
> socket allready exist, and in that case I'll close it (is there
> anything else ?) and call again socket(AF_INET, SOCK_STREAM, 0);
>
> Anyone could help me to find a way to know if my sock is already
> opened ?



What socket? Or, to put it another way, how would you know which of the
open sockets on the machine is your socket?

DS


David Schwartz

2004-01-23, 5:06 pm


"Christophe" <chocman@libertysurf.fr> wrote in message
news:3fd47aec.2240156@news.oleane.fr...
quote:

> I'm actualy programing socket in C. I've got a proc that initialise
> my socket. In that proc I'd like to set up a control to know if my
> socket allready exist, and in that case I'll close it (is there
> anything else ?) and call again socket(AF_INET, SOCK_STREAM, 0);
>
> Anyone could help me to find a way to know if my sock is already
> opened ?



What socket? Or, to put it another way, how would you know which of the
open sockets on the machine is your socket?

DS


David Schwartz

2004-01-23, 5:06 pm


"Barry Margolin" <barmar@alum.mit.edu> wrote in message
news:barmar-6B0248.12330108122003@netnews.attbi.com...
quote:

> In article <3fd47aec.2240156@news.oleane.fr>,
> chocman@libertysurf.fr (Christophe) wrote:


quote:

[QUOTE][color=darkred]
[QUOTE][color=darkred]
> Just close it without checking. If it's already closed this won't have
> any effect.



Except that it could close some random file/socket that you weren't
aware of.

DS


David Schwartz

2004-01-23, 5:06 pm


"Barry Margolin" <barmar@alum.mit.edu> wrote in message
news:barmar-6B0248.12330108122003@netnews.attbi.com...
quote:

> In article <3fd47aec.2240156@news.oleane.fr>,
> chocman@libertysurf.fr (Christophe) wrote:


quote:

[QUOTE][color=darkred]
[QUOTE][color=darkred]
> Just close it without checking. If it's already closed this won't have
> any effect.



Except that it could close some random file/socket that you weren't
aware of.

DS


those who know me have no need of my name

2004-01-23, 5:06 pm

in comp.unix.programmer i read:
quote:

>Anyone could help me to find a way to know if my sock is already
>opened ?



try to use it.

--
a signature
Joseph Dionne

2004-01-23, 5:06 pm

Why didn't I think of that.

those who know me have no need of my name wrote:
quote:

> in comp.unix.programmer i read:
>
>
>
>
> try to use it.
>



those who know me have no need of my name

2004-01-23, 5:06 pm

in comp.unix.programmer i read:
quote:

>Anyone could help me to find a way to know if my sock is already
>opened ?



try to use it.

--
a signature
Joseph Dionne

2004-01-23, 5:06 pm

Why didn't I think of that.

those who know me have no need of my name wrote:
quote:

> in comp.unix.programmer i read:
>
>
>
>
> try to use it.
>



Barry Margolin

2004-01-23, 5:06 pm

In article <br2ga0$r64$1@nntp.webmaster.com>,
"David Schwartz" <davids@webmaster.com> wrote:
quote:

> "Barry Margolin" <barmar@alum.mit.edu> wrote in message
> news:barmar-6B0248.12330108122003@netnews.attbi.com...
>
>
>
>
>
> Except that it could close some random file/socket that you weren't
> aware of.



I assumed it was in some variable that the procedure knows about.
Otherwise, what socket is it supposed to check when it checks whether
the socket already exists?

--
Barry Margolin, barmar@alum.mit.edu
Woburn, MA
Barry Margolin

2004-01-23, 5:06 pm

In article <br2ga0$r64$1@nntp.webmaster.com>,
"David Schwartz" <davids@webmaster.com> wrote:
quote:

> "Barry Margolin" <barmar@alum.mit.edu> wrote in message
> news:barmar-6B0248.12330108122003@netnews.attbi.com...
>
>
>
>
>
> Except that it could close some random file/socket that you weren't
> aware of.



I assumed it was in some variable that the procedure knows about.
Otherwise, what socket is it supposed to check when it checks whether
the socket already exists?

--
Barry Margolin, barmar@alum.mit.edu
Woburn, MA
Christophe

2004-01-23, 5:06 pm

Thank you...
I'd forgot to says that I'm using Unix (HP), so your first idea was
helpfull.
I just have to find what's a size_t structure, and that will be ok.

Thx again
Chris

Le Mon, 08 Dec 2003 17:47:13 GMT, Joseph Dionne
<jdionne@sdcnov1.sdcweb.net> écrit:
quote:

>Try getsockopt(...SO_ERROR...) for UNIX
>
>If it is still opened, return will be zero, otherwise -1.
>
>If you are working with the boot virus, formerly know as "Windows", use
>ioctlsocket(...FIONREAD...). It will return SOCKET_ERROR if the socket
>handle is closed, by either end, or zero, a connected socket.
>
>WARNING: don't use getsockopt(), before using ioctlsocket() to verify
>the socket is still "valid" first on Windows. I found winsock to segv
>when used on a socket that is no longer connected, no matter which side
>disconnected first.
>
>Barry Margolin wrote:
>



Christophe

2004-01-23, 5:06 pm

Thank you...
I'd forgot to says that I'm using Unix (HP), so your first idea was
helpfull.
I just have to find what's a size_t structure, and that will be ok.

Thx again
Chris

Le Mon, 08 Dec 2003 17:47:13 GMT, Joseph Dionne
<jdionne@sdcnov1.sdcweb.net> écrit:
quote:

>Try getsockopt(...SO_ERROR...) for UNIX
>
>If it is still opened, return will be zero, otherwise -1.
>
>If you are working with the boot virus, formerly know as "Windows", use
>ioctlsocket(...FIONREAD...). It will return SOCKET_ERROR if the socket
>handle is closed, by either end, or zero, a connected socket.
>
>WARNING: don't use getsockopt(), before using ioctlsocket() to verify
>the socket is still "valid" first on Windows. I found winsock to segv
>when used on a socket that is no longer connected, no matter which side
>disconnected first.
>
>Barry Margolin wrote:
>



Barry Margolin

2004-01-23, 5:07 pm

In article <3fd5caa9.18765890@news.oleane.fr>,
chocman@libertysurf.fr (Christophe) wrote:
quote:

> Thank you...
> I'd forgot to says that I'm using Unix (HP), so your first idea was
> helpfull.
> I just have to find what's a size_t structure, and that will be ok.



size_t isn't a structure, it's an integer type. It's declared in one of
the standard headers.

--
Barry Margolin, barmar@alum.mit.edu
Woburn, MA
David Schwartz

2004-01-23, 5:07 pm


"Barry Margolin" <barmar@alum.mit.edu> wrote in message
news:barmar-CB9439.01144809122003@netnews.attbi.com...
quote:

have[QUOTE][color=darkred]
[QUOTE][color=darkred]
[QUOTE][color=darkred]
> I assumed it was in some variable that the procedure knows about.
> Otherwise, what socket is it supposed to check when it checks whether
> the socket already exists?



If the socket doesn't exist, any value you pass to 'close' runs the risk
of closing some other descriptor.

DS


Barry Margolin

2004-01-23, 5:07 pm

In article <3fd5caa9.18765890@news.oleane.fr>,
chocman@libertysurf.fr (Christophe) wrote:
quote:

> Thank you...
> I'd forgot to says that I'm using Unix (HP), so your first idea was
> helpfull.
> I just have to find what's a size_t structure, and that will be ok.



size_t isn't a structure, it's an integer type. It's declared in one of
the standard headers.

--
Barry Margolin, barmar@alum.mit.edu
Woburn, MA
David Schwartz

2004-01-23, 5:07 pm


"Barry Margolin" <barmar@alum.mit.edu> wrote in message
news:barmar-CB9439.01144809122003@netnews.attbi.com...
quote:

have[QUOTE][color=darkred]
[QUOTE][color=darkred]
[QUOTE][color=darkred]
> I assumed it was in some variable that the procedure knows about.
> Otherwise, what socket is it supposed to check when it checks whether
> the socket already exists?



If the socket doesn't exist, any value you pass to 'close' runs the risk
of closing some other descriptor.

DS


Barry Margolin

2004-01-23, 5:07 pm

In article <3fd47aec.2240156@news.oleane.fr>,
chocman@libertysurf.fr (Christophe) wrote:
quote:

> Hello...
>
> I'm actualy programing socket in C. I've got a proc that initialise
> my socket. In that proc I'd like to set up a control to know if my
> socket allready exist, and in that case I'll close it (is there
> anything else ?) and call again socket(AF_INET, SOCK_STREAM, 0);
>
> Anyone could help me to find a way to know if my sock is already
> opened ?



Just close it without checking. If it's already closed this won't have
any effect.

--
Barry Margolin, barmar@alum.mit.edu
Woburn, MA
Joseph Dionne

2004-01-23, 5:07 pm

Try getsockopt(...SO_ERROR...) for UNIX

If it is still opened, return will be zero, otherwise -1.

If you are working with the boot virus, formerly know as "Windows", use
ioctlsocket(...FIONREAD...). It will return SOCKET_ERROR if the socket
handle is closed, by either end, or zero, a connected socket.

WARNING: don't use getsockopt(), before using ioctlsocket() to verify
the socket is still "valid" first on Windows. I found winsock to segv
when used on a socket that is no longer connected, no matter which side
disconnected first.

Barry Margolin wrote:
quote:

> In article <3fd47aec.2240156@news.oleane.fr>,
> chocman@libertysurf.fr (Christophe) wrote:
>
>
>
>
> Just close it without checking. If it's already closed this won't have
> any effect.
>



David Schwartz

2004-01-23, 5:07 pm


"Christophe" <chocman@libertysurf.fr> wrote in message
news:3fd47aec.2240156@news.oleane.fr...
quote:

> I'm actualy programing socket in C. I've got a proc that initialise
> my socket. In that proc I'd like to set up a control to know if my
> socket allready exist, and in that case I'll close it (is there
> anything else ?) and call again socket(AF_INET, SOCK_STREAM, 0);
>
> Anyone could help me to find a way to know if my sock is already
> opened ?



What socket? Or, to put it another way, how would you know which of the
open sockets on the machine is your socket?

DS


David Schwartz

2004-01-23, 5:07 pm


"Barry Margolin" <barmar@alum.mit.edu> wrote in message
news:barmar-6B0248.12330108122003@netnews.attbi.com...
quote:

> In article <3fd47aec.2240156@news.oleane.fr>,
> chocman@libertysurf.fr (Christophe) wrote:


quote:

[QUOTE][color=darkred]
[QUOTE][color=darkred]
> Just close it without checking. If it's already closed this won't have
> any effect.



Except that it could close some random file/socket that you weren't
aware of.

DS


those who know me have no need of my name

2004-01-23, 5:07 pm

in comp.unix.programmer i read:
quote:

>Anyone could help me to find a way to know if my sock is already
>opened ?



try to use it.

--
a signature
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com