Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-05-2003, 06:42 AM   #1
LQ Newbie
Registered: Jul 2003
Location: Switzerland
Posts: 6

Rep: Reputation: 0
Question Problem with accept() socket call which doesn't return

Working with Red Hat 7.2, kernel 2.4.16, I'm programming sockets.
In a thread, I call the accept() function. In another thread, I close the socket. Normaly, accept() should return with an error. But it never returns !

Does anyone know this problem ?

Thank's for your help.
Old 08-05-2003, 12:41 PM   #2
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
It will return if either there's a client connecting to the socket, or if you use a non-blocking socket.
Old 08-06-2003, 02:12 AM   #3
LQ Newbie
Registered: Jul 2003
Location: Switzerland
Posts: 6

Original Poster
Rep: Reputation: 0
The case is when the server decides itself to stop. It wants to stop his listen thread (the one waiting on accept() call). With your first idea, if no client comes to connect, the accept call never returns, so the thread never ends. The second idea will probably be the solution, but I wanted to avoid it because it means that the thread must pull periodically (with a sleep time) on the socket instead of staying inactive. And this solution introduces a delay : the thread cannot react immediately on the client demand.
Thank's you for your message.
Old 08-06-2003, 06:57 AM   #4
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 18
What is called first, accept or close ? Are the threads synchronised, using a mutex or condition variable. They should be, because the order of accept and close is important.
Old 08-06-2003, 07:30 AM   #5
LQ Newbie
Registered: Jul 2003
Location: Switzerland
Posts: 6

Original Poster
Rep: Reputation: 0
Sequence is :
1- the server is first waiting on accept() in the listen thread.
2- at any time, the main thread can call close() to close the socket.

I even tried to make a call to select() before accept(). But it doesn't work too. The select() call remains blocked, as did accept().

I come to have a look at the man page of accept() : no return error is specified when the socket is suddenly closed. So it cannot work like this.

I should say that I work with two PC, one is Linux and the other is Windows, and I first wrote my program under Windows and it works. But the Windows accept() specifies a return error when the socket is canceled. I was too trustful when trying under Linux !


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to handle ECONNABORTED with accept system call? murugesan Solaris / OpenSolaris 3 06-07-2005 03:18 PM
C: Socket weirdness: select() -> fork() -> accept() maxfacta Programming 1 03-01-2005 02:33 PM
(c++) network socket programming: help with accept() Dark Carnival Programming 4 08-11-2004 05:22 PM
How can get values return from a system call quenn Programming 1 09-17-2003 12:05 AM
Problem with accept() call which doesn't return jph Linux - General 0 07-28-2003 05:34 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:11 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration