Newsgroups: comp.lang.perl
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!batcomputer!theory.TC.Cornell.EDU!lijewski
From: lijewski@theory.TC.Cornell.EDU (Mike Lijewski)
Subject: Re: A perl novice problem
Message-ID: <1991Jun13.130503.25657@batcomputer.tn.cornell.edu>
Sender: news@batcomputer.tn.cornell.edu
Nntp-Posting-Host: theory.tc.cornell.edu
Organization: Cornell National Supercomputer Facility
References: <WEG.91Jun12194458@convx1.convx1.ccit.arizona.edu>
Distribution: comp
Date: Thu, 13 Jun 1991 13:05:03 GMT

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
