Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

accept returns zero as a file descriptor

794 views
Skip to first unread message

Ravindran Maravar

unread,
Mar 9, 2001, 11:09:30 PM3/9/01
to
Hello ,

I am trying out a simple server program in UNIX (HP_UX 11) which prints out
whatever is sent to it, I am able to create a socket ,bind it to a port and
listen on that port.However when a connection is requested the accept function
returns zero as a socket file descriptor,and recv() gives a error saying
"Socket operattion on a non socket descriptor"

Is 0 a valid socket file descritor? If it is why is recv returning a error?

Any help is appriciated

Thanks and regards
Ravi


CheolHo Choi

unread,
Mar 12, 2001, 5:48:12 AM3/12/01
to

Why don't you reference the accept() man page.

@ 0 is not a valid file descriptor.
0: standard input
1: standard output
2: standard error

"Ravindran Maravar" <Ravindra...@cwo.com.au> wrote in message
news:3AA9A8FA...@cwo.com.au...

Udo A. Steinberg

unread,
Mar 12, 2001, 5:51:25 AM3/12/01
to
CheolHo Choi wrote:
>
> Why don't you reference the accept() man page.
>
> @ 0 is not a valid file descriptor.
> 0: standard input
> 1: standard output
> 2: standard error

I don't know which manpage you're reading, but 0 is a perfectly
valid file descriptor and so are 1 and 2. They will become available
as soon as stdin, stdout and stderr have been closed.

-Udo.

Andrew Gierth

unread,
Mar 12, 2001, 9:22:32 AM3/12/01
to Ravindra...@cwo.com.au
>>>>> "Ravindran" == Ravindran Maravar <Ravindra...@cwo.com.au> writes:

Ravindran> Hello ,
Ravindran> I am trying out a simple server program in UNIX (HP_UX 11)
Ravindran> which prints out whatever is sent to it, I am able to
Ravindran> create a socket ,bind it to a port and listen on that
Ravindran> port.However when a connection is requested the accept
Ravindran> function returns zero as a socket file descriptor,and
Ravindran> recv() gives a error saying "Socket operattion on a non
Ravindran> socket descriptor"

Ravindran> Is 0 a valid socket file descritor? If it is why is recv
Ravindran> returning a error?

0 is a valid file descriptor, but it usually refers to stdin.

You didn't post your code, but my advanced network snooping devices
have enabled me to spot the error in your sources anyway: you have
some misplaced or missing parentheses around your accept() call.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
or <URL: http://www.whitefang.com/unix/>

Ravindran Maravar

unread,
Mar 10, 2001, 5:10:43 PM3/10/01
to
Andrew,
Looks like u got the wrong code buddy, I checked the parentheses and the syntax
its perfectly fine .Here is the snippet ...

if((newsfd = accept(sockfd,(struct sockaddr*)&cliAddr,&cliLen))< 0)
{
printf("Cannot accpet connection \n");
exit(1);
}

Any suggestions?

Andrew Gierth wrote:

> >>>>> "Ravindran" == Ravindran Maravar <Ravindra...@cwo.com.au> writes:
>
> Ravindran> Hello ,
> Ravindran> I am trying out a simple server program in UNIX (HP_UX 11)
> Ravindran> which prints out whatever is sent to it, I am able to
> Ravindran> create a socket ,bind it to a port and listen on that
> Ravindran> port.However when a connection is requested the accept
> Ravindran> function returns zero as a socket file descriptor,and
> Ravindran> recv() gives a error saying "Socket operattion on a non
> Ravindran> socket descriptor"
>
> Ravindran> Is 0 a valid socket file descritor? If it is why is recv
> Ravindran> returning a error?
>
> 0 is a valid file descriptor, but it usually refers to stdin.
>
> You didn't post your code, but my advanced network snooping devices
> have enabled me to spot the error in your sources anyway: you have

> some misplaced or missing around your accept() call.

Ravindran Maravar

unread,
Mar 10, 2001, 5:15:37 PM3/10/01
to
Udo,
Its right ,0 is a valid socket file descriptor..<0 is invalid ..Why does
recv() return a ENOTSOCK error? I tried using read() instead of recv() As
expected it waits for the input ...But with recv() it gives a ENOTSOCK error?

Any suggestions?

Barry Margolin

unread,
Mar 12, 2001, 6:12:32 PM3/12/01
to
In article <3AAAA789...@cwo.com.au>,

Ravindran Maravar <Ravindra...@cwo.com.au> wrote:
>Udo,
>Its right ,0 is a valid socket file descriptor..<0 is invalid ..Why does
>recv() return a ENOTSOCK error? I tried using read() instead of recv() As
>expected it waits for the input ...But with recv() it gives a ENOTSOCK error?

Does accept() still return 0 if you ensure that FD 0 is in use before
calling accept()? It's a good idea for a daemon to open /dev/null on FD's
0, 1, and 2, just in case some function tries to access stdin, stdout, or
stderr; you don't want that function using the socket when it does this.

If 0 is in use and accept() still returns 0, something is really broken.

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Ravindran Maravar

unread,
Mar 10, 2001, 7:01:47 PM3/10/01
to
Barry,

I think this could be the solution .I havent tried it out as yet .I will try it out
today and let u guys know abt it......Thanks for the response

Ravi

0 new messages