[asterisk-dev] How to terminate allocated socket resource when channel hangs up!

Kai Hoerner kai at ciphron.de
Tue Nov 24 11:53:37 CST 2009


Hi,

if you wait for a blocking socket to return, there's no chance to exit 
prematurely.

use non-blocking sockets and implement the timeout on your own.  loop to 
read/select from the non-blocking socket and dont forget to include 
calls to usleep() in you loop, so it doesn't eat 100% cpu.  in that 
loop, you can check for hangup and exit the application.


Best Regards,

Kai Hoerner



ast guy schrieb:
> Hi,
>  In my asterisk application app_myApplication() code, I have to bind a 
> socket to a dynamic port number, so that I can send/receive some 
> strings. I am able to get it working by below code.
>
> Now the problem is; say the read timeout of socket is 60 secs and user 
> hangs up the channel in 30 seconds, Call is disconnected but still 
> app_myApplication() remains for that channel and when 60 seconds have 
> passed. Then normal application follow is executed and channel closes 
> gracefully.
>
> Is there any way that I can terminate the socket along with channel 
> hangup event ?
>
> ///------------------------------ CODE 
> -----------------------------------------
>     int sockfd, new_fd;  // listen on sock_fd
>     struct sockaddr_in my_addr;    // my address information
>     struct sockaddr_in their_addr; // connector's address information
>     socklen_t sin_size;
>
>     struct timeval tv;
>
>     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
>         return -1;
>     }
>    
>     my_addr.sin_family = AF_INET;         // host byte order
>     my_addr.sin_port = htons(atoi(port));     // short, network byte order
>     my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
>     memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
>
>     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) {
>         return -1;
>     }
>
>     if (listen(sockfd, BACKLOG) == -1) {
>         return -1;
>     }
>
> // HERE I am setting the socket Read time out
>
>     tv.tv_sec = 60;
>     tv.tv_usec = 0;
>
>     if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv) == 
> -1) {
>         return -1;
>     }
>
> ///------------------------------ CODE 
> -----------------------------------------
>
>
> /ag
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> --Bandwidth and Colocation Provided by http://www.api-digital.com--
>
> asterisk-dev mailing list
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-dev




More information about the asterisk-dev mailing list