UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2003-05-21 11:54 AM

PTR
Member
Registered: 2003-05-21
Posts: 2

Re: accept returns 0

I'm trying to create a super-simple  client-server. When I call 'accept' in the server it returns a zero. Formally it isn't an error, but when I later calls 'recv' with the accept return value as it's first parametr, it returns -1 and errno=38, which means 'not a socket descriptor'. What is the problem and how can I fix it??

Offline

#2 2003-05-21 10:41 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: accept returns 0

More input... need more input

Please post more details, the problematic parts of your code, the OS and other information you might find important would be enough to help you out with this

Offline

#3 2003-05-22 08:02 AM

PTR
Member
Registered: 2003-05-21
Posts: 2

Re: accept returns 0

:lol:  I found the bug :). It was, as always, very simple and had nothing to do with what I thought it was. Thanks.

Offline

#4 2006-07-21 09:51 PM

remance
Member
Registered: 2006-07-21
Posts: 2

Re: accept returns 0

HELP!!!
I know that this posting is very, very old...but I am having the same exact problem:  accept() returning a zero.

What was the bug?  (cause it's here now)

Thanks.

Bob

Offline

#5 2006-07-22 08:55 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: accept returns 0

By any chance are you closing stdin...?

Its just a thought but... if you are then that means file handle ( number ) 0 becomes available for use... so um...  it would be perfectly legitimate for a call to accept to return a value of 0...

If that isn't what is occurring... then I would put forth that your computer is possessed by evil spirits and the appropriate action(s) should be taken immediately...

Darn - it seems part of my message was cut off somehow... continuing... lol

Ok... so that is a likely cause and though its something simple its easy to miss...

If it isn't that...

Are you doing anything strange like creating a listening socket... then forking off children and having more than one of the children doing accept calls on it?  Now, that shouldn't be returning you a 0 on error and in fact on the systems that don't like it, it should cause a fault of some sort... but if you have that fault handled ( unintentionally ) by a custom signal handler that doesn't do anything the 'result' of accept could appear to be 0...

Hope that helps... ;-)


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2006-07-24 01:41 PM

remance
Member
Registered: 2006-07-21
Posts: 2

Re: accept returns 0

Michael,

Thanks for your timely and creative reply.  Turns out that that pesky "missing parens problem" inside a rather deep "if" question is still lurking.  A careful inspection of the expression (after about four hours of being sure that I messed something up with the sockets interface) found the problem.   Basic stuff.

I'm all set, thanks.

Bob

Offline

#7 2015-02-03 04:54 PM

Jean-Luc
Guest

Re: accept returns 0

Hello,

Just for fun.
I got exactly the same problem and after 4 hours of reading my code, I finally found the problem.

I coded
  if( sock=accept(server_sock,(struct sockaddr*)&client_add,&len) < 0 )
instead of
  if( (sock=accept(server_sock,(struct sockaddr*)&client_add,&len)) < 0 )

As the < has a higher priority with my compiler (g++) sock was always 0 instead of having the good file descriptor number returned by accept.

Grrrr !

#8 2015-02-03 09:30 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: accept returns 0

Yep, that type of bug bites a lot of people...

It's not just that compiler, BTW...  Any compiler that conforms to the C/C++ standards will behave the same...  But, if you're lucky, your compiler might warn you about such shady-looking assignments within if statements...  GCC (and presumably g++ as well) will do so if you compile with "-Wall", which is always a good idea...

Offline

#9 2015-02-04 11:10 AM

Jean-Luc
Guest

Re: accept returns 0

Hello,

In fact, I ported a small server which running well on windows. It seems that Visual C++ and gcc does not compile this ambiguous code in the same manner.
You're right, comipiling the buggy code with -Wall option gives:
main.cpp:52: warning: suggest parentheses around assignment used as truth value

#10 2015-02-04 12:31 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: accept returns 0

Well, I suppose I shouldn't be too surprised that a Microsoft compiler would violate the standards...  But, direct assignment is definitely lower precedence than less-than...

Offline

Board footer

Powered by FluxBB