Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!rochester!crdgw1!uakari.primate.wisc.edu!sdd.hp.com!cs.utexas.edu!asuvax!ncar!noao!amethyst!math.arizona.edu!weg From: weg@convx1.ccit.arizona.edu (Eythan Weg) Newsgroups: comp.lang.perl Subject: A perl novice problem Message-ID: Date: 13 Jun 91 02:44:58 GMT Sender: news@amethyst.math.arizona.edu Distribution: comp Organization: University of Arizona, Economics Dept. Lines: 113 Hi There: To test my understanding of the mechanics of sockets I have been trying to see how communications through these work, but ran into the problem that neither client nor server can receive each other's messages. The client and server are form The Book with some (minor?) alterations. Can someone explain what's wrong? The programs follow. Thanks, Eythan ---------client #!/usr/bin/perl ($them,$port) = @ARGV; $port = 8492 unless $port; chop($them = `hostname`) unless $them; $AF_INET = 2; $SOCK_STREAM = 1; $sockaddr = 'S n a4 x8'; chop($hostname = `hostname`); ($name,$aliases,$proto) = getprotobyname('tcp'); ($name,$aliases,$port) = getservbyname($port,'tcp') unless $port =~ /^\d+$/;; ($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname); ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them); $this = pack($sockaddr, $AF_INET, 0, $thisaddr); $that = pack($sockaddr, $AF_INET, $port, $thataddr); # Make the socket filehandle. if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) { print "socket ok\n"; } else { die $!; } # Give the socket an address. if (bind(S, $this)) { print "bind ok\n"; } else { die $!; } # Call up the server. if (connect(S,$that)) { print "connect ok\n"; } else { die $!; } # Set socket to be command buffered. #select(S); $| = 1; select(STDOUT); send(S,"ready",0)|| die "cannot send\n"; print "sent\n"; recv(S,$data,10,0) || die "cannot receive\ $!\n"; print "$data\n"; -----------------server #!/usr/bin/perl ($port) = @ARGV; $port = 8492 unless $port; $AF_INET = 2; $SOCK_STREAM = 1; $sockaddr = 'S n a4 x8'; ($name, $aliases, $proto) = getprotobyname('tcp'); if ($port !~ /^\d+$/) { ($name, $aliases, $port) = getservbyport($port, 'tcp'); } print "Port = $port\n"; $this = pack($sockaddr, $AF_INET, $port, "\0\0\0\0"); select(NS); $| = 1; select(stdout); socket(S, $AF_INET, $SOCK_STREAM, $proto) || die "socket: $!"; bind(S,$this) || die "bind: $!"; listen(S,5) || die "connect: $!"; print "Listening for connection....\n"; ($addr = accept(NS,S)) || die $!; $in=''; vec($in,fileno(NS),1)=1; if (select($out=$in,undef,undef,undef)>0){ if (vec($out,fileno(NS),1)){ print "NS is ready\n"; recv(NS,$msg,10,0)|| die "cannot receive\n"; print "$msg\n"; } } send(NS,"hello",0) || die "cannot send\n"; print "message sent\n";