tru64-unix-managers (formerly alpha-osf-managers) (1994-2012)
 help / color / mirror / Atom feed
From: Mark Burrell <mark () adam ! ac ! uk>
To: tru64-unix-managers
Subject: Sockets : I'm a PERL dummy...
Date: Fri, 28 Nov 1997 14:24:12 +0000	[thread overview]
Message-ID: <96078619210747.tru64-unix-managers.marc@archive.invalid> (raw)

Hi everyone,

I have what is probably a very simple qestion - how do you
get two way communication over sockets using PERL?

I just need a simple client and server program (written in 
PERL) to send messages to each other.  So far I can send
messages from the server to the client, and I can send 
messages from the client to the server - but I can't do both.

I wrote a couple of c programs to test my dumbness, and I have 
the protocol sussed because the c programs worked....

For the PERL programs I'm basing them on examples from 
'Programming PERL'.  In fact here they are (btw, I know that
I'm not using a multithreading server - but I don't want to,
I want something nice and simple that my poor dumb brain
can understand, and show to other poor dumb brains...).

The server :-

#!/usr/bin/perl -Tw

require 5.002;
use strict;
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }

    use Socket;
    use Carp;

    my $line;
    my $port = 8850;
    my $proto = getprotobyname('tcp');

    socket(Server, PF_INET, SOCK_STREAM, $proto) || die "SERVER: socket:
$!";
    setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, 1) || die "SERVER:
setsockopt: $!";

    bind(Server, sockaddr_in($port, INADDR_ANY)) || die "SERVER: bind:
$!";
    listen(Server,SOMAXCONN) || die "SERVER: listen: $!";
    print "SERVER: Server Start [$port]\n";

    my ($paddr, $inline, $n);

    $SIG{CHLD} = \&REAPER;

    for (;;) {

       $paddr = accept(Client,Server);
       my($port,$iaddr) = sockaddr_in($paddr);
       my $name = gethostbyaddr($iaddr,AF_INET);

       send Client, "SERVER TO CLIENT: [$name]\n", 0;

       $n = read(Client, $line, 4096);
       print "[$n][$line]\n";

       send Client, "SERVER TO CLIENT: Message 2\n", 0;

       close Client;
    }

and here is the client :-

#!/usr/bin/perl -w
require 5.002;
use strict;
use Socket;
my ($remote, $port, $iaddr, $paddr, $proto, $line, $n);

$remote  = 'localhost';
$port    = 8850;


$iaddr   = inet_aton($remote) || die "no host: $remote";
$paddr   = sockaddr_in($port, $iaddr);
$proto   = getprotobyname('tcp');

socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";

connect(SOCK, $paddr) || die "connect: $!";

$n = read(SOCK, $line, 4096);
print "[$n][$line]\n";

send SOCK, "This is a test", 0;

$n = read(SOCK, $line, 4096);
print "[$n][$line]\n";

close (SOCK) || die "close: $!";
exit;


As you can see, currently I'm tring to send :-
1. A message from server to client
2. A message from client to server
3. A mesage from server to client again.

Now, as I said before, if I make all the communication
one way then everything works fine - but the programs
above (with two way communication) hangs.

I'm assuming I'm doing something dumb (after all the
c programs worked - honest!) but what?

As usual, any help (even simple working examples) is much 
appreciated!

Mark.

-- 
Mark S. Burrell                        ADAM Technical Officer
-------------------------------------------------------------
Historical and Critical Studies Dept.   Tel:+44(0)191 2273704
University of Northumbria              mailto:mark@adam.ac.uk
Newcastle upon Tyne, NE1 8ST, UK      http://adam.ac.uk/~mark
-------------------------------------------------------------

                 reply	other threads:[~1997-11-28 14:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox