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!

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.

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?

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.

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.

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.