LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with accept() socket call which doesn't return (https://www.linuxquestions.org/questions/programming-9/problem-with-accept-socket-call-which-doesnt-return-79133/)

jph 08-05-2003 05:42 AM

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.

Hko 08-05-2003 11:41 AM

It will return if either there's a client connecting to the socket, or if you use a non-blocking socket.

jph 08-06-2003 01:12 AM

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.

DIYLinux 08-06-2003 05:57 AM

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.

jph 08-06-2003 06:30 AM

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 !


All times are GMT -5. The time now is 04:11 AM.