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

Hi

I posted 2-3 days back with my xinetd service giving me error -

Address already in use at ./server.pl line 32.

I was suggested to use flag = REUSE in xinetd service and it did work fine. Now, the error has recreated and I don't the reason why. Need some pointers here.

my xinetd service

# default: on # description: Hello World socket server service sajan { socket_type = stream flags = REUSE wait = no user = root server = /tmp/server.pl log_on_success += USERID log_on_failure += USERID disable = no }
Part of my server.pl code -
#!/usr/bin/perl -w # server1.pl - a simple server use strict; use Socket; use IO::Socket; use constant TCP_PORT => 7890; my $port = TCP_PORT; my $proto = getprotobyname('tcp'); sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } # create a socket, make it reusable socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket failed : $ +!\n"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock faile +d : $! \n"; # grab a port on this machine my $paddr = sockaddr_in($port, INADDR_ANY); # bind to a port, then listen bind(SERVER, $paddr) or die "bind failed : $!"; listen(SERVER, SOMAXCONN) or die "listen failed : $!"; print "Server started on port $port \n";
i have entry in /etc/services as - sajan 7890/tcp

Need help to understand and resolve this. Thank you

Replies are listed 'Best First'.
Re: Address Bind issue reincarnation
by almut (Canon) on Jul 15, 2010 at 13:42 UTC

    I'm kind of surprised this has ever worked at all (maybe xinetd wasn't running, and you had started the script manually...?).

    Normally, when you run a service via (x)inetd, the service is expected to not do bind/listen itself (like a standalone daemon would do). Rather, it simply communicates via stdin/stdout, while the corresponding port/socket is handled by the super-daemon xinetd.

    So, as xinetd is already listening on port 7890, your script gets an "Address already in use" when it tries to do the same...

    See also inetd and this intro.

      Hi almut,

      Thanks a lot for your reply. I did reconfirm my daemon process and then realized I had used the daemon manually once using " /tmp/server.pl & ".

      Also, by the mean time i read in detail about xinetd and socket programming and came to knw what u mentioned abt communication via stdin/stdout. So, my error is very obvious.

      You had mentioned about inetd. I have started reading about it. But, will it help me make a daemon process for my script. I will be experimenting with that today and let you know.

        You had mentioned about inetd. I have started reading about it. But, will it help me make a daemon process for my script.

        Just to avoid misunderstandings: don't use inetd if you have xinetd.  Essentially, they serve the same purpose, but xinetd is the more modern, more secure incarnation. — I just linked to inetd, because the respective wikipedia page for xinetd didn't have the crucial bit of info about stdin/stdout...

        P.S.: in case you want to run your script persistently as a regular standalone daemon (not managed by xinetd, thus doing networking itself), you might be interested in daemontools.  In this case, you'd have to add an accept loop to your script, though.

Re: Address Bind issue reincarnation
by sierpinski (Chaplain) on Jul 15, 2010 at 12:52 UTC
    I have to start with the obvious...

    What does 'netstat -a |grep 7890' give you? Is it possible that another copy of the script is running, using that port? The 'address in use' error is pretty straightforward usually.

    On a side note, if you didn't already know, running a script (permanently) out of /tmp can be dangerous... some systems have a built-in cron job that deletes anything 7 days old (or less, depending on the system) that is in /tmp. I figure you're just testing now though, but wanted to mention nevertheless.

      Hi

      Thanks for the reply.

      The netstat command doesn't show me anythin but when i do 'netstat -ap | grep sajan'.. i get =>

      tcp 0 0 *:sajan *:* LISTEN 3002/xinetd

      I know that this service is already using this port, but whats the other alternative to use my script as a daemon.

      /tmp/server.pl & is an option.. but i wanted something better if possible.

      Thanks for telling me about /tmp.. I am currently just testing the script, so the final script won't be lying there. But, thanks for letting me know that