[comp.lang.perl] A perl novice problem

weg@convx1.ccit.arizona.edu (Eythan Weg) (06/13/91)

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";

lijewski@theory.TC.Cornell.EDU (Mike Lijewski) (06/13/91)

In article <WEG.91Jun12194458@convx1.convx1.ccit.arizona.edu> weg@convx1.ccit.arizona.edu (Eythan Weg) writes:
>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

The code in the book is a good deal more complicated than it needs to
be in order to simply illustrate the use of sockets.  Below is a shar
file of a simple interative client-server pair using UNIX domain
sockets.  Hope it helps.

Mike Lijewski  (H)607/272-0238 (W)607/254-8686
Cornell National Supercomputer Facility
ARPA: mjlx@eagle.tc.cornell.edu  BITNET: mjlx@cornellf.bitnet
SMAIL:  25 Renwick Heights Road, Ithaca, NY  14850

#!/bin/sh
# This is a shell archive.  Remove anything before this line,
# then feed it into a shell via "sh file" or similar. To overwrite
# existing files, type "sh file -c".
#
# Wrapped by mjlx on Thu Jun 13  9:00:46 EDT 1991
#
# Contents: client.pl server.pl
#
PATH=/bin:/usr/bin:/usr/ucb; export PATH;
if test -f 'client.pl' -a "${1}" != "-c" ; then
  echo Will not clobber existing file 'client.pl'
else
  echo Extracting "client.pl" \(379 bytes\)
  sed 's/^X//' > client.pl <<'END_OF_FILE client.pl'
X#!/usr/local/sys/bin/perl
X
X$AF_UNIX     = 1;    # from <sys/socket.h>
X$SOCK_STREAM = 1;    # from <sys/socket.h>
X$node = '/tmp/.server-unix';
X$sockaddr = 'S a17 x91';
X
X$this = pack($sockaddr, $AF_UNIX, $node);
X
Xsocket(S, $AF_UNIX, $SOCK_STREAM, 0) || die "socket: $!\n";
X
Xconnect(S, $this) || die "connect: $!\n";
X
Xwhile(<S>) { print ; } # print what server sends to us
X
Xexit 0;
END_OF_FILE client.pl
  if test 379 -ne `wc -c <'client.pl'`; then
    echo 'client.pl' unpacked with wrong size!
  fi
fi
chmod 700 client.pl
if test -f 'server.pl' -a "${1}" != "-c" ; then
  echo Will not clobber existing file 'server.pl'
else
  echo Extracting "server.pl" \(759 bytes\)
  sed 's/^X//' > server.pl <<'END_OF_FILE server.pl'
X#!/usr/local/sys/bin/perl
X
X$AF_UNIX     = 1;    # from <sys/socket.h>
X$SOCK_STREAM = 1;    # from <sys/socket.h>
X$node = '/tmp/.server-unix';
X$sockaddr = 'S a17 x91';
X$this = pack($sockaddr, $AF_UNIX, $node);
X$line = "this is a line from the server\n";
X
X$SIG{'PIPE'}   = 'ignore';
X
X(unlink $node || die "$node exists: $!\n") if -e $node;
X
Xsocket(S, $AF_UNIX, $SOCK_STREAM, 0) || die "socket: $!\n";
X
Xbind(S, $this) || (unlink($node), die "bind: $!");
X
Xchmod 0644, $node || (unlink($node), die "chmod of $node failed");
X
Xlisten(S, 5)   || (unlink($node), die "listen: $!");
X
Xfor(;;) {
X
X    accept(NS, S) || (unlink($node), die "accept: $!");
X
X    syswrite(NS, "$line", length($line)); # write line to client
X
X    close NS;
X}
Xexit 0;
X
Xsub catcher { close NS; }
END_OF_FILE server.pl
  if test 759 -ne `wc -c <'server.pl'`; then
    echo 'server.pl' unpacked with wrong size!
  fi
fi
chmod 700 server.pl
exit 0
-- 
Mike Lijewski  (H)607/272-0238 (W)607/254-8686
Cornell National Supercomputer Facility
ARPA: mjlx@eagle.tc.cornell.edu  BITNET: mjlx@cornellf.bitnet
SMAIL:  25 Renwick Heights Road, Ithaca, NY  14850