bv has asked for the wisdom of the Perl Monks concerning the following question:

Hey all. I'm trying to code up a simple within-the-machine chat server using IO::Socket::UNIX. My problem (I wish there were only one!) is this: When I Ctrl-C the server, the FIFOs related to the sockets are not cleaned up, so running it again gives me

Problem: Address already in use at chatserv.pl line 8;

I am trapping SIGINT and calling IO::Socket::shutdown(3) on the sockets. Do I need to actually unlink the FIFOs, too? Here's an example code:

#!/usr/bin/perl use strict; use warnings; use threads; use IO::Socket::UNIX; my $serv = IO::Socket::UNIX0>new( Type => SOCK_STREAM, Local => '/var/tmp/chat/serv', Listen => 1, ) or die "Problem: $!"; my $msgthr = threads->create('messenger'); $SIG{INT} = sub { $serv->shutdown(3); #this line gives a "uninitialized value" error: print STDERR "Serv status:", $serv->connected(),"\n"; #similar handler in &messenger tries to shut down #another socket, this one of type SOCK_DGRAM $msgthread->kill('TERM')->detach(); exit(0) }
print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

Replies are listed 'Best First'.
Re: How to shut down UNIX sockets?
by ig (Vicar) on Sep 01, 2009 at 19:31 UTC

    You are getting the error because the socket file exists when you restart the server. You can unlink it in the server after shutting down the socket. You might also unlink it before setting up the socket, in case any previous process left it behind.

      Thanks for the response. So the module doesn't provide any method to unlink the socket file automatically? Bummer.

      print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

        I don't see any method for automatically unlinking the socket file. This is consistent with the underlying system calls.

Re: How to shut down UNIX sockets?
by hnd (Scribe) on Sep 01, 2009 at 19:06 UTC
    i think what you want to do is: send one message and wait for the reply then again reply to reply received previously

    you can use I/O multiplexing um.... that is to say you can use IO::Select module's can_read() and can_write() functions..... they check whether the socket is ready for reading or writing..... better than forking... check out the documentation of IO::Select here

    =====================================================
    i'am worst at what do best and for this gift i fell blessed...
    i found it hard it's hard to find well whatever
    NEVERMIND

      Yes, that's what I want to do generally, and I'm having a fair amount of trouble, but I'm not ready to ask for help yet. The problem I asked about was specifically whether IO::Socket::UNIX provides any clean way to exit such that it can start again without failing with EADDRINUSE. I could write the code to unlink the FIFOs if they exist, but then I have to worry about concurrency and so forth.

      print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))
        you can look at one of my meditations on chat server....
        and for cleaning the sockets, IO::Select provides the select() function that can specify what you want your socket to do....

        "keep it simple.... stupid :D"
        =====================================================
        i'am worst at what do best and for this gift i fell blessed...
        i found it hard it's hard to find well whatever
        NEVERMIND