Problem with accept() socket call which doesn't return
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 !
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.
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.
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 !
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.