#!/usr/bin/perl -w -s ################### # server1 - stripped down serverDemo ################### use strict; use IO::Socket; my $localport = 3003; # Socket that this server listens on. print STDOUT "SERVER: starting serverDemo \n"; my $sockListen = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $localport, Listen => 5, Reuse => 1, ) or die "SERVER: Couldn't listen at port $localport; $@"; print STDOUT "SERVER: accepting clients at port $localport. \n"; $sockListen->autoflush(1); # write everything out immediately. $sockListen->blocking(1); # wait for the other end when accepting # The INET->accept() call, below, waits (i.e. "blocks") for a new connection. # Once we get one, we deal with it, close it, and wait for the next. # (This simple server model assumes we can deal with requests quickly.) while ( my $sockClient = $sockListen->accept ) { $sockClient->autoflush(1); # write everything out immediately. $sockClient->blocking(1); # wait for the other end when reading # send out initial greeting my $toClient = "Hi. Server time is " . scalar localtime(); print STDOUT "SERVER: sending '$toClient'\n"; $sockClient->print($toClient."\n"); $sockClient->blocking(1); # wait for the other end when reading # read and echo any lines from the client while ( my $fromClient = $sockClient->getline ) { chomp($fromClient); print STDOUT "SERVER: receieved '$fromClient'\n"; # output it } # close this connection down print STDOUT "SERVER: client closed connection.\n"; close($sockClient); } # We never get here, so the sever can only be shut down manually.