954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to stop accept call?

Hi, i have question about accept call interrupting. As you can see I have a loop which is controlled by boolean not_ended. I set this var to 0 (false) in a separate function.

I would like to know how should I manage to interrupt this accept loop properly. When I set the not_ended I need to interrupt the accept call somehow. Directly closing socket causes:

Problem with accept call
: Bad file descriptor
int sd, not_ended = 1;

while (not_ended) {
      sd = accept(socket, (struct sockaddr*) &from_serv, &addrlen_serv);

      /* threading stuff */

}


Thanks in advance!

yuri1969
Light Poster
29 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

Here's an example of using select() which can solve your problem

http://linux.die.net/man/2/select_tut

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Thanks for link. But I don't understand how using a select instead of accept can help me to finish using socket when I send a signal to do so.

yuri1969
Light Poster
29 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 
Thanks for link. But I don't understand how using a select instead of accept can help me to finish using socket when I send a signal to do so.

Could you explain this in more detail..."can help me to finish using socket when I send a signal to do so". The listening socket?

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 
Could you explain this in more detail..."can help me to finish using socket when I send a signal to do so". The listening socket?


I apologize myself. I already have written a server which accepts connections from client apps and execute various commands according to the received message.

So I would like to shut the server down by sending a message with closing command.

I figured out that I need to break the loop with accept to stop accepting new connections -> listening socket, before the server shut down.

yuri1969
Light Poster
29 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

dig

yuri1969
Light Poster
29 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

Closing the listening socket is a right way to go. Then accept() returns -1 and sets errno to EBADF, as you already observed. You just need some more logic in the "threading stuff" to analyze what have actually happened. For example, test not_ended: if it is false, you know for sure that the error is intended, and that the shutdown is in progress; otherwise you may want to do whatever recovery is necessary, and restart the listener.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 
Closing the listening socket is a right way to go. Then accept() returns -1 and sets errno to EBADF, as you already observed. You just need some more logic in the "threading stuff" to analyze what have actually happened. For example, test not_ended: if it is false, you know for sure that the error is intended, and that the shutdown is in progress; otherwise you may want to do whatever recovery is necessary, and restart the listener.


Thanks, I'll try :)

yuri1969
Light Poster
29 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: