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

I have been messing with network programming and sockets recently(trying to learn anything I possible can). As a simple introduction I wrote Server and Client sockets.

I am having trouble with getting these sockets to work from two different machines. They run correctly when I use 'localhost' but if I try to plug in an IP, they fail on the connect attempt. There is one more thing I am going to try( the 'inet_aton' function ) but I figured I'd get the ball rolling. Here is the code I am using, I'll point out my areas of trouble with comments.

Server Socket first.

use IO::Socket; $sock = new IO::Socket::INET( LocalAddr => 'localhost', #if i was to +plug an IP in as the value for LocalAddr, wouldn't work LocalPort => 1200, Proto => 'tcp', Listen => 10, Reuse => 1 ); die $! unless $sock; while ( $new_sock = $sock->accept() ) { $clnt = $sock->sockhost(); print "New connection from $clnt...\n"; while ( $buf = <$new_sock> ) { print $buf; } print "Client disconnected...\n"; } close( $sock );
Now the client socket:
use IO::Socket; $sock = new IO::Socket::INET( PeerAddr => 'localhost', # an IP here a +nd the script fails to connect PeerPort => 1200, Proto => 'tcp' ); die "Socket could not be created: $!\n" unless $sock; $msg = <STDIN>; while ( $msg !~ m/^\n$/ ) { $inp = $msg; undef( $msg ); print $sock "MSG: $inp"; $sock->flush(); $msg = <STDIN>; } close( $sock );
Thanks in advance for any help you guys can give. - kel -

Replies are listed 'Best First'.
(jeffa) Re: IO::Socket
by jeffa (Bishop) on Feb 07, 2001 at 19:51 UTC
    Number 1 - use strict!!

    That being said - your constuctor for your server socket needs a little tweaking - you don't need to specify LocalAddr. Try this instead:

    my $sock = new IO::Socket::INET( LocalPort => 1200, Reuse => 1, Listen => 10, Type => SOCK_STREAM );

    UPDATE: I used an IP address for the PeerAddr attribute/key in the client and everything worked fine. I tried localhost, 127.0.0.1, my IP address, and my host name, short and fully qualified and had no problems.

    Jeff

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
      And in the client, where is the IP of the remote host specified? I tried to put in the PeerAddr attribute but it didn't work. - kel -
        Your server, by definition listens on the local machine. By giving it a hostname or IP address to listen on, you are telling it that it needs to use its "ears" on that particular hostname/IP, and only listen for connections with that as their destination. Thus, if you use "localhost", only clients on the local machine will be able to connect to the server process, and even then only if they are making their connection to "localhost" or 127.0.0.1.

        All machines with an IP stack usually have at least two IP addresses associated with it: The normal IP address that you use to make connections to the machine from other hosts, and a loopback address 127.0.0.1, which by definition refers to the "local host". The LocalAddr option does not mean "listen for connections coming from clients on the given host", it means "listen for connections from clients connecting *to* the given host, which is this machine". It will fail if the hostname or IP address you give it is not a valid IP address for the host the server is running on.

        So generally, omit the LocalAddr option unless you have an explicit need to only listen on a single interface, ignoring connection attempts being made to other interfaces on the same host. Omitting it is the same as using a LocalAddr of 0.0.0.0, or a wildcard IP, meaning it'll accept connections on all local interfaces, which is usually what you want.

        Does this make sense? I'd also check out the documentation for bind, as this is how LocalAddr is implemented. That might give you some more detail.

Re: IO::Socket
by kschwab (Vicar) on Feb 07, 2001 at 19:57 UTC
    Usually, folks don't bind the server to a specific IP address. I would drop the LocalAddr option in your server. If it's not specified, the server will listen on all of the interfaces on the machine. (Including the lo localhost interface you are listening on now).

    If you really do want to listen on a specific IP address, make sure it's one that's valid and already assigned to one the host's interfaces.

    If this is a unix box, "netstat -ni" should give you a list of valid addresses for the host.