Don't assume sh and true.
[p5sagit/p5-mst-13.2.git] / pod / perlipc.pod
1 =head1 NAME
2
3 perlipc - Perl interprocess communication (signals, fifos, pipes, safe subprocesses, sockets, and semaphores)
4
5 =head1 DESCRIPTION
6
7 The basic IPC facilities of Perl are built out of the good old Unix
8 signals, named pipes, pipe opens, the Berkeley socket routines, and SysV
9 IPC calls.  Each is used in slightly different situations.
10
11 =head1 Signals
12
13 Perl uses a simple signal handling model: the %SIG hash contains names
14 or references of user-installed signal handlers.  These handlers will
15 be called with an argument which is the name of the signal that
16 triggered it.  A signal may be generated intentionally from a
17 particular keyboard sequence like control-C or control-Z, sent to you
18 from another process, or triggered automatically by the kernel when
19 special events transpire, like a child process exiting, your process
20 running out of stack space, or hitting file size limit.
21
22 For example, to trap an interrupt signal, set up a handler like this:
23
24     sub catch_zap {
25         my $signame = shift;
26         $shucks++;
27         die "Somebody sent me a SIG$signame";
28     }
29     $SIG{INT} = 'catch_zap';  # could fail in modules
30     $SIG{INT} = \&catch_zap;  # best strategy
31
32 Prior to Perl 5.7.3 it was necessary to do as little as you possibly
33 could in your handler; notice how all we do is set a global variable
34 and then raise an exception.  That's because on most systems,
35 libraries are not re-entrant; particularly, memory allocation and I/O
36 routines are not.  That meant that doing nearly I<anything> in your
37 handler could in theory trigger a memory fault and subsequent core
38 dump - see L<Deferred Signals> below.
39
40 The names of the signals are the ones listed out by C<kill -l> on your
41 system, or you can retrieve them from the Config module.  Set up an
42 @signame list indexed by number to get the name and a %signo table
43 indexed by name to get the number:
44
45     use Config;
46     defined $Config{sig_name} || die "No sigs?";
47     foreach $name (split(' ', $Config{sig_name})) {
48         $signo{$name} = $i;
49         $signame[$i] = $name;
50         $i++;
51     }
52
53 So to check whether signal 17 and SIGALRM were the same, do just this:
54
55     print "signal #17 = $signame[17]\n";
56     if ($signo{ALRM}) {
57         print "SIGALRM is $signo{ALRM}\n";
58     }
59
60 You may also choose to assign the strings C<'IGNORE'> or C<'DEFAULT'> as
61 the handler, in which case Perl will try to discard the signal or do the
62 default thing.
63
64 On most Unix platforms, the C<CHLD> (sometimes also known as C<CLD>) signal
65 has special behavior with respect to a value of C<'IGNORE'>.
66 Setting C<$SIG{CHLD}> to C<'IGNORE'> on such a platform has the effect of
67 not creating zombie processes when the parent process fails to C<wait()>
68 on its child processes (i.e. child processes are automatically reaped).
69 Calling C<wait()> with C<$SIG{CHLD}> set to C<'IGNORE'> usually returns
70 C<-1> on such platforms.
71
72 Some signals can be neither trapped nor ignored, such as
73 the KILL and STOP (but not the TSTP) signals.  One strategy for
74 temporarily ignoring signals is to use a local() statement, which will be
75 automatically restored once your block is exited.  (Remember that local()
76 values are "inherited" by functions called from within that block.)
77
78     sub precious {
79         local $SIG{INT} = 'IGNORE';
80         &more_functions;
81     }
82     sub more_functions {
83         # interrupts still ignored, for now...
84     }
85
86 Sending a signal to a negative process ID means that you send the signal
87 to the entire Unix process-group.  This code sends a hang-up signal to all
88 processes in the current process group (and sets $SIG{HUP} to IGNORE so
89 it doesn't kill itself):
90
91     {
92         local $SIG{HUP} = 'IGNORE';
93         kill HUP => -$$;
94         # snazzy writing of: kill('HUP', -$$)
95     }
96
97 Another interesting signal to send is signal number zero.  This doesn't
98 actually affect another process, but instead checks whether it's alive
99 or has changed its UID.
100
101     unless (kill 0 => $kid_pid) {
102         warn "something wicked happened to $kid_pid";
103     }
104
105 You might also want to employ anonymous functions for simple signal
106 handlers:
107
108     $SIG{INT} = sub { die "\nOutta here!\n" };
109
110 But that will be problematic for the more complicated handlers that need
111 to reinstall themselves.  Because Perl's signal mechanism is currently
112 based on the signal(3) function from the C library, you may sometimes be so
113 misfortunate as to run on systems where that function is "broken", that
114 is, it behaves in the old unreliable SysV way rather than the newer, more
115 reasonable BSD and POSIX fashion.  So you'll see defensive people writing
116 signal handlers like this:
117
118     sub REAPER {
119         $waitedpid = wait;
120         # loathe sysV: it makes us not only reinstate
121         # the handler, but place it after the wait
122         $SIG{CHLD} = \&REAPER;
123     }
124     $SIG{CHLD} = \&REAPER;
125     # now do something that forks...
126
127 or better still:
128
129     use POSIX ":sys_wait_h";
130     sub REAPER {
131         my $child;
132         # If a second child dies while in the signal handler caused by the
133         # first death, we won't get another signal. So must loop here else
134         # we will leave the unreaped child as a zombie. And the next time
135         # two children die we get another zombie. And so on.
136         while (($child = waitpid(-1,WNOHANG)) > 0) {
137             $Kid_Status{$child} = $?;
138         }
139         $SIG{CHLD} = \&REAPER;  # still loathe sysV
140     }
141     $SIG{CHLD} = \&REAPER;
142     # do something that forks...
143
144 Signal handling is also used for timeouts in Unix,   While safely
145 protected within an C<eval{}> block, you set a signal handler to trap
146 alarm signals and then schedule to have one delivered to you in some
147 number of seconds.  Then try your blocking operation, clearing the alarm
148 when it's done but not before you've exited your C<eval{}> block.  If it
149 goes off, you'll use die() to jump out of the block, much as you might
150 using longjmp() or throw() in other languages.
151
152 Here's an example:
153
154     eval {
155         local $SIG{ALRM} = sub { die "alarm clock restart" };
156         alarm 10;
157         flock(FH, 2);   # blocking write lock
158         alarm 0;
159     };
160     if ($@ and $@ !~ /alarm clock restart/) { die }
161
162 If the operation being timed out is system() or qx(), this technique
163 is liable to generate zombies.    If this matters to you, you'll
164 need to do your own fork() and exec(), and kill the errant child process.
165
166 For more complex signal handling, you might see the standard POSIX
167 module.  Lamentably, this is almost entirely undocumented, but
168 the F<t/lib/posix.t> file from the Perl source distribution has some
169 examples in it.
170
171 =head1 Named Pipes
172
173 A named pipe (often referred to as a FIFO) is an old Unix IPC
174 mechanism for processes communicating on the same machine.  It works
175 just like a regular, connected anonymous pipes, except that the
176 processes rendezvous using a filename and don't have to be related.
177
178 To create a named pipe, use the Unix command mknod(1) or on some
179 systems, mkfifo(1).  These may not be in your normal path.
180
181     # system return val is backwards, so && not ||
182     #
183     $ENV{PATH} .= ":/etc:/usr/etc";
184     if  (      system('mknod',  $path, 'p')
185             && system('mkfifo', $path) )
186     {
187         die "mk{nod,fifo} $path failed";
188     }
189
190
191 A fifo is convenient when you want to connect a process to an unrelated
192 one.  When you open a fifo, the program will block until there's something
193 on the other end.
194
195 For example, let's say you'd like to have your F<.signature> file be a
196 named pipe that has a Perl program on the other end.  Now every time any
197 program (like a mailer, news reader, finger program, etc.) tries to read
198 from that file, the reading program will block and your program will
199 supply the new signature.  We'll use the pipe-checking file test B<-p>
200 to find out whether anyone (or anything) has accidentally removed our fifo.
201
202     chdir; # go home
203     $FIFO = '.signature';
204     $ENV{PATH} .= ":/etc:/usr/games";
205
206     while (1) {
207         unless (-p $FIFO) {
208             unlink $FIFO;
209             system('mknod', $FIFO, 'p')
210                 && die "can't mknod $FIFO: $!";
211         }
212
213         # next line blocks until there's a reader
214         open (FIFO, "> $FIFO") || die "can't write $FIFO: $!";
215         print FIFO "John Smith (smith\@host.org)\n", `fortune -s`;
216         close FIFO;
217         sleep 2;    # to avoid dup signals
218     }
219
220 =head2 Deferred Signals
221
222 In Perls before Perl 5.7.3 by installing Perl code to deal with
223 signals, you were exposing yourself to danger from two things.  First,
224 few system library functions are re-entrant.  If the signal interrupts
225 while Perl is executing one function (like malloc(3) or printf(3)),
226 and your signal handler then calls the same function again, you could
227 get unpredictable behavior--often, a core dump.  Second, Perl isn't
228 itself re-entrant at the lowest levels.  If the signal interrupts Perl
229 while Perl is changing its own internal data structures, similarly
230 unpredictable behaviour may result.
231
232 There were two things you could do, knowing this: be paranoid or be
233 pragmatic.  The paranoid approach was to do as little as possible in your
234 signal handler.  Set an existing integer variable that already has a
235 value, and return.  This doesn't help you if you're in a slow system call,
236 which will just restart.  That means you have to C<die> to longjump(3) out
237 of the handler.  Even this is a little cavalier for the true paranoiac,
238 who avoids C<die> in a handler because the system I<is> out to get you.
239 The pragmatic approach was to say ``I know the risks, but prefer the
240 convenience'', and to do anything you wanted in your signal handler,
241 and be prepared to clean up core dumps now and again.
242
243 In Perl 5.7.3 and later to avoid these problems signals are
244 "deferred"-- that is when the signal is delivered to the process by
245 the system (to the C code that implements Perl) a flag is set, and the
246 handler returns immediately. Then at strategic "safe" points in the
247 Perl interpreter (e.g. when it is about to execute a new opcode) the
248 flags are checked and the Perl level handler from %SIG is
249 executed. The "deferred" scheme allows much more flexibility in the
250 coding of signal handler as we know Perl interpreter is in a safe
251 state, and that we are not in a system library function when the
252 handler is called.  However the implementation does differ from
253 previous Perls in the following ways:
254
255 =over 4
256
257 =item Long running opcodes
258
259 As Perl interpreter only looks at the signal flags when it about to
260 execute a new opcode if a signal arrives during a long running opcode
261 (e.g. a regular expression operation on a very large string) then
262 signal will not be seen until operation completes.
263
264 =item Interrupting IO
265
266 When a signal is delivered (e.g. INT control-C) the operating system
267 breaks into IO operations like C<read> (used to implement Perls
268 E<lt>E<gt> operator). On older Perls the handler was called
269 immediately (and as C<read> is not "unsafe" this worked well). With
270 the "deferred" scheme the handler is not called immediately, and if
271 Perl is using system's C<stdio> library that library may re-start the
272 C<read> without returning to Perl and giving it a chance to call the
273 %SIG handler. If this happens on your system the solution is to use
274 C<:perlio> layer to do IO - at least on those handles which you want
275 to be able to break into with signals. (The C<:perlio> layer checks
276 the signal flags and calls %SIG handlers before resuming IO operation.)
277
278 Note that the default in Perl 5.7.3 and later is to automatically use
279 the C<:perlio> layer.
280
281 =item Signals as "faults"
282
283 Certain signals e.g. SEGV, ILL, BUS are generated as a result of
284 virtual memory or other "faults". These are normally fatal and there
285 is little a Perl-level handler can do with them. (In particular the
286 old signal scheme was particularly unsafe in such cases.)  However if
287 a %SIG handler is set the new scheme simply sets a flag and returns as
288 described above. This may cause the operating system to try the
289 offending machine instruction again and - as nothing has changed - it
290 will generate the signal again. The result of this is a rather odd
291 "loop". In future Perl's signal mechanism may be changed to avoid this
292 - perhaps by simply disallowing %SIG handlers on signals of that
293 type. Until then the work-round is not to set a %SIG handler on those
294 signals. (Which signals they are is operating system dependant.)
295
296 =item Signals triggered by operating system state
297
298 On some operating systems certain signal handlers are supposed to "do
299 something" before returning. One example can be CHLD or CLD which
300 indicates a child process has completed. On some operating systems the
301 signal handler is expected to C<wait> for the completed child
302 process. On such systems the deferred signal scheme will not work for
303 those signals (it does not do the C<wait>). Again the failure will
304 look like a loop as the operating system will re-issue the signal as
305 there are un-waited-for completed child processes.
306
307 =back 4
308
309 =head1 Using open() for IPC
310
311 Perl's basic open() statement can also be used for unidirectional
312 interprocess communication by either appending or prepending a pipe
313 symbol to the second argument to open().  Here's how to start
314 something up in a child process you intend to write to:
315
316     open(SPOOLER, "| cat -v | lpr -h 2>/dev/null")
317                     || die "can't fork: $!";
318     local $SIG{PIPE} = sub { die "spooler pipe broke" };
319     print SPOOLER "stuff\n";
320     close SPOOLER || die "bad spool: $! $?";
321
322 And here's how to start up a child process you intend to read from:
323
324     open(STATUS, "netstat -an 2>&1 |")
325                     || die "can't fork: $!";
326     while (<STATUS>) {
327         next if /^(tcp|udp)/;
328         print;
329     }
330     close STATUS || die "bad netstat: $! $?";
331
332 If one can be sure that a particular program is a Perl script that is
333 expecting filenames in @ARGV, the clever programmer can write something
334 like this:
335
336     % program f1 "cmd1|" - f2 "cmd2|" f3 < tmpfile
337
338 and irrespective of which shell it's called from, the Perl program will
339 read from the file F<f1>, the process F<cmd1>, standard input (F<tmpfile>
340 in this case), the F<f2> file, the F<cmd2> command, and finally the F<f3>
341 file.  Pretty nifty, eh?
342
343 You might notice that you could use backticks for much the
344 same effect as opening a pipe for reading:
345
346     print grep { !/^(tcp|udp)/ } `netstat -an 2>&1`;
347     die "bad netstat" if $?;
348
349 While this is true on the surface, it's much more efficient to process the
350 file one line or record at a time because then you don't have to read the
351 whole thing into memory at once.  It also gives you finer control of the
352 whole process, letting you to kill off the child process early if you'd
353 like.
354
355 Be careful to check both the open() and the close() return values.  If
356 you're I<writing> to a pipe, you should also trap SIGPIPE.  Otherwise,
357 think of what happens when you start up a pipe to a command that doesn't
358 exist: the open() will in all likelihood succeed (it only reflects the
359 fork()'s success), but then your output will fail--spectacularly.  Perl
360 can't know whether the command worked because your command is actually
361 running in a separate process whose exec() might have failed.  Therefore,
362 while readers of bogus commands return just a quick end of file, writers
363 to bogus command will trigger a signal they'd better be prepared to
364 handle.  Consider:
365
366     open(FH, "|bogus")  or die "can't fork: $!";
367     print FH "bang\n"   or die "can't write: $!";
368     close FH            or die "can't close: $!";
369
370 That won't blow up until the close, and it will blow up with a SIGPIPE.
371 To catch it, you could use this:
372
373     $SIG{PIPE} = 'IGNORE';
374     open(FH, "|bogus")  or die "can't fork: $!";
375     print FH "bang\n"   or die "can't write: $!";
376     close FH            or die "can't close: status=$?";
377
378 =head2 Filehandles
379
380 Both the main process and any child processes it forks share the same
381 STDIN, STDOUT, and STDERR filehandles.  If both processes try to access
382 them at once, strange things can happen.  You may also want to close
383 or reopen the filehandles for the child.  You can get around this by
384 opening your pipe with open(), but on some systems this means that the
385 child process cannot outlive the parent.
386
387 =head2 Background Processes
388
389 You can run a command in the background with:
390
391     system("cmd &");
392
393 The command's STDOUT and STDERR (and possibly STDIN, depending on your
394 shell) will be the same as the parent's.  You won't need to catch
395 SIGCHLD because of the double-fork taking place (see below for more
396 details).
397
398 =head2 Complete Dissociation of Child from Parent
399
400 In some cases (starting server processes, for instance) you'll want to
401 completely dissociate the child process from the parent.  This is
402 often called daemonization.  A well behaved daemon will also chdir()
403 to the root directory (so it doesn't prevent unmounting the filesystem
404 containing the directory from which it was launched) and redirect its
405 standard file descriptors from and to F</dev/null> (so that random
406 output doesn't wind up on the user's terminal).
407
408     use POSIX 'setsid';
409
410     sub daemonize {
411         chdir '/'               or die "Can't chdir to /: $!";
412         open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
413         open STDOUT, '>/dev/null'
414                                 or die "Can't write to /dev/null: $!";
415         defined(my $pid = fork) or die "Can't fork: $!";
416         exit if $pid;
417         setsid                  or die "Can't start a new session: $!";
418         open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
419     }
420
421 The fork() has to come before the setsid() to ensure that you aren't a
422 process group leader (the setsid() will fail if you are).  If your
423 system doesn't have the setsid() function, open F</dev/tty> and use the
424 C<TIOCNOTTY> ioctl() on it instead.  See L<tty(4)> for details.
425
426 Non-Unix users should check their Your_OS::Process module for other
427 solutions.
428
429 =head2 Safe Pipe Opens
430
431 Another interesting approach to IPC is making your single program go
432 multiprocess and communicate between (or even amongst) yourselves.  The
433 open() function will accept a file argument of either C<"-|"> or C<"|-">
434 to do a very interesting thing: it forks a child connected to the
435 filehandle you've opened.  The child is running the same program as the
436 parent.  This is useful for safely opening a file when running under an
437 assumed UID or GID, for example.  If you open a pipe I<to> minus, you can
438 write to the filehandle you opened and your kid will find it in his
439 STDIN.  If you open a pipe I<from> minus, you can read from the filehandle
440 you opened whatever your kid writes to his STDOUT.
441
442     use English '-no_match_vars';
443     my $sleep_count = 0;
444
445     do {
446         $pid = open(KID_TO_WRITE, "|-");
447         unless (defined $pid) {
448             warn "cannot fork: $!";
449             die "bailing out" if $sleep_count++ > 6;
450             sleep 10;
451         }
452     } until defined $pid;
453
454     if ($pid) {  # parent
455         print KID_TO_WRITE @some_data;
456         close(KID_TO_WRITE) || warn "kid exited $?";
457     } else {     # child
458         ($EUID, $EGID) = ($UID, $GID); # suid progs only
459         open (FILE, "> /safe/file")
460             || die "can't open /safe/file: $!";
461         while (<STDIN>) {
462             print FILE; # child's STDIN is parent's KID
463         }
464         exit;  # don't forget this
465     }
466
467 Another common use for this construct is when you need to execute
468 something without the shell's interference.  With system(), it's
469 straightforward, but you can't use a pipe open or backticks safely.
470 That's because there's no way to stop the shell from getting its hands on
471 your arguments.   Instead, use lower-level control to call exec() directly.
472
473 Here's a safe backtick or pipe open for read:
474
475     # add error processing as above
476     $pid = open(KID_TO_READ, "-|");
477
478     if ($pid) {   # parent
479         while (<KID_TO_READ>) {
480             # do something interesting
481         }
482         close(KID_TO_READ) || warn "kid exited $?";
483
484     } else {      # child
485         ($EUID, $EGID) = ($UID, $GID); # suid only
486         exec($program, @options, @args)
487             || die "can't exec program: $!";
488         # NOTREACHED
489     }
490
491
492 And here's a safe pipe open for writing:
493
494     # add error processing as above
495     $pid = open(KID_TO_WRITE, "|-");
496     $SIG{ALRM} = sub { die "whoops, $program pipe broke" };
497
498     if ($pid) {  # parent
499         for (@data) {
500             print KID_TO_WRITE;
501         }
502         close(KID_TO_WRITE) || warn "kid exited $?";
503
504     } else {     # child
505         ($EUID, $EGID) = ($UID, $GID);
506         exec($program, @options, @args)
507             || die "can't exec program: $!";
508         # NOTREACHED
509     }
510
511 Note that these operations are full Unix forks, which means they may not be
512 correctly implemented on alien systems.  Additionally, these are not true
513 multithreading.  If you'd like to learn more about threading, see the
514 F<modules> file mentioned below in the SEE ALSO section.
515
516 =head2 Bidirectional Communication with Another Process
517
518 While this works reasonably well for unidirectional communication, what
519 about bidirectional communication?  The obvious thing you'd like to do
520 doesn't actually work:
521
522     open(PROG_FOR_READING_AND_WRITING, "| some program |")
523
524 and if you forget to use the C<use warnings> pragma or the B<-w> flag,
525 then you'll miss out entirely on the diagnostic message:
526
527     Can't do bidirectional pipe at -e line 1.
528
529 If you really want to, you can use the standard open2() library function
530 to catch both ends.  There's also an open3() for tridirectional I/O so you
531 can also catch your child's STDERR, but doing so would then require an
532 awkward select() loop and wouldn't allow you to use normal Perl input
533 operations.
534
535 If you look at its source, you'll see that open2() uses low-level
536 primitives like Unix pipe() and exec() calls to create all the connections.
537 While it might have been slightly more efficient by using socketpair(), it
538 would have then been even less portable than it already is.  The open2()
539 and open3() functions are  unlikely to work anywhere except on a Unix
540 system or some other one purporting to be POSIX compliant.
541
542 Here's an example of using open2():
543
544     use FileHandle;
545     use IPC::Open2;
546     $pid = open2(*Reader, *Writer, "cat -u -n" );
547     print Writer "stuff\n";
548     $got = <Reader>;
549
550 The problem with this is that Unix buffering is really going to
551 ruin your day.  Even though your C<Writer> filehandle is auto-flushed,
552 and the process on the other end will get your data in a timely manner,
553 you can't usually do anything to force it to give it back to you
554 in a similarly quick fashion.  In this case, we could, because we
555 gave I<cat> a B<-u> flag to make it unbuffered.  But very few Unix
556 commands are designed to operate over pipes, so this seldom works
557 unless you yourself wrote the program on the other end of the
558 double-ended pipe.
559
560 A solution to this is the nonstandard F<Comm.pl> library.  It uses
561 pseudo-ttys to make your program behave more reasonably:
562
563     require 'Comm.pl';
564     $ph = open_proc('cat -n');
565     for (1..10) {
566         print $ph "a line\n";
567         print "got back ", scalar <$ph>;
568     }
569
570 This way you don't have to have control over the source code of the
571 program you're using.  The F<Comm> library also has expect()
572 and interact() functions.  Find the library (and we hope its
573 successor F<IPC::Chat>) at your nearest CPAN archive as detailed
574 in the SEE ALSO section below.
575
576 The newer Expect.pm module from CPAN also addresses this kind of thing.
577 This module requires two other modules from CPAN: IO::Pty and IO::Stty.
578 It sets up a pseudo-terminal to interact with programs that insist on
579 using talking to the terminal device driver.  If your system is
580 amongst those supported, this may be your best bet.
581
582 =head2 Bidirectional Communication with Yourself
583
584 If you want, you may make low-level pipe() and fork()
585 to stitch this together by hand.  This example only
586 talks to itself, but you could reopen the appropriate
587 handles to STDIN and STDOUT and call other processes.
588
589     #!/usr/bin/perl -w
590     # pipe1 - bidirectional communication using two pipe pairs
591     #         designed for the socketpair-challenged
592     use IO::Handle;     # thousands of lines just for autoflush :-(
593     pipe(PARENT_RDR, CHILD_WTR);                # XXX: failure?
594     pipe(CHILD_RDR,  PARENT_WTR);               # XXX: failure?
595     CHILD_WTR->autoflush(1);
596     PARENT_WTR->autoflush(1);
597
598     if ($pid = fork) {
599         close PARENT_RDR; close PARENT_WTR;
600         print CHILD_WTR "Parent Pid $$ is sending this\n";
601         chomp($line = <CHILD_RDR>);
602         print "Parent Pid $$ just read this: `$line'\n";
603         close CHILD_RDR; close CHILD_WTR;
604         waitpid($pid,0);
605     } else {
606         die "cannot fork: $!" unless defined $pid;
607         close CHILD_RDR; close CHILD_WTR;
608         chomp($line = <PARENT_RDR>);
609         print "Child Pid $$ just read this: `$line'\n";
610         print PARENT_WTR "Child Pid $$ is sending this\n";
611         close PARENT_RDR; close PARENT_WTR;
612         exit;
613     }
614
615 But you don't actually have to make two pipe calls.  If you
616 have the socketpair() system call, it will do this all for you.
617
618     #!/usr/bin/perl -w
619     # pipe2 - bidirectional communication using socketpair
620     #   "the best ones always go both ways"
621
622     use Socket;
623     use IO::Handle;     # thousands of lines just for autoflush :-(
624     # We say AF_UNIX because although *_LOCAL is the
625     # POSIX 1003.1g form of the constant, many machines
626     # still don't have it.
627     socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
628                                 or  die "socketpair: $!";
629
630     CHILD->autoflush(1);
631     PARENT->autoflush(1);
632
633     if ($pid = fork) {
634         close PARENT;
635         print CHILD "Parent Pid $$ is sending this\n";
636         chomp($line = <CHILD>);
637         print "Parent Pid $$ just read this: `$line'\n";
638         close CHILD;
639         waitpid($pid,0);
640     } else {
641         die "cannot fork: $!" unless defined $pid;
642         close CHILD;
643         chomp($line = <PARENT>);
644         print "Child Pid $$ just read this: `$line'\n";
645         print PARENT "Child Pid $$ is sending this\n";
646         close PARENT;
647         exit;
648     }
649
650 =head1 Sockets: Client/Server Communication
651
652 While not limited to Unix-derived operating systems (e.g., WinSock on PCs
653 provides socket support, as do some VMS libraries), you may not have
654 sockets on your system, in which case this section probably isn't going to do
655 you much good.  With sockets, you can do both virtual circuits (i.e., TCP
656 streams) and datagrams (i.e., UDP packets).  You may be able to do even more
657 depending on your system.
658
659 The Perl function calls for dealing with sockets have the same names as
660 the corresponding system calls in C, but their arguments tend to differ
661 for two reasons: first, Perl filehandles work differently than C file
662 descriptors.  Second, Perl already knows the length of its strings, so you
663 don't need to pass that information.
664
665 One of the major problems with old socket code in Perl was that it used
666 hard-coded values for some of the constants, which severely hurt
667 portability.  If you ever see code that does anything like explicitly
668 setting C<$AF_INET = 2>, you know you're in for big trouble:  An
669 immeasurably superior approach is to use the C<Socket> module, which more
670 reliably grants access to various constants and functions you'll need.
671
672 If you're not writing a server/client for an existing protocol like
673 NNTP or SMTP, you should give some thought to how your server will
674 know when the client has finished talking, and vice-versa.  Most
675 protocols are based on one-line messages and responses (so one party
676 knows the other has finished when a "\n" is received) or multi-line
677 messages and responses that end with a period on an empty line
678 ("\n.\n" terminates a message/response).
679
680 =head2 Internet Line Terminators
681
682 The Internet line terminator is "\015\012".  Under ASCII variants of
683 Unix, that could usually be written as "\r\n", but under other systems,
684 "\r\n" might at times be "\015\015\012", "\012\012\015", or something
685 completely different.  The standards specify writing "\015\012" to be
686 conformant (be strict in what you provide), but they also recommend
687 accepting a lone "\012" on input (but be lenient in what you require).
688 We haven't always been very good about that in the code in this manpage,
689 but unless you're on a Mac, you'll probably be ok.
690
691 =head2 Internet TCP Clients and Servers
692
693 Use Internet-domain sockets when you want to do client-server
694 communication that might extend to machines outside of your own system.
695
696 Here's a sample TCP client using Internet-domain sockets:
697
698     #!/usr/bin/perl -w
699     use strict;
700     use Socket;
701     my ($remote,$port, $iaddr, $paddr, $proto, $line);
702
703     $remote  = shift || 'localhost';
704     $port    = shift || 2345;  # random port
705     if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
706     die "No port" unless $port;
707     $iaddr   = inet_aton($remote)               || die "no host: $remote";
708     $paddr   = sockaddr_in($port, $iaddr);
709
710     $proto   = getprotobyname('tcp');
711     socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";
712     connect(SOCK, $paddr)    || die "connect: $!";
713     while (defined($line = <SOCK>)) {
714         print $line;
715     }
716
717     close (SOCK)            || die "close: $!";
718     exit;
719
720 And here's a corresponding server to go along with it.  We'll
721 leave the address as INADDR_ANY so that the kernel can choose
722 the appropriate interface on multihomed hosts.  If you want sit
723 on a particular interface (like the external side of a gateway
724 or firewall machine), you should fill this in with your real address
725 instead.
726
727     #!/usr/bin/perl -Tw
728     use strict;
729     BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
730     use Socket;
731     use Carp;
732     my $EOL = "\015\012";
733
734     sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
735
736     my $port = shift || 2345;
737     my $proto = getprotobyname('tcp');
738
739     ($port) = $port =~ /^(\d+)$/                        or die "invalid port";
740
741     socket(Server, PF_INET, SOCK_STREAM, $proto)        || die "socket: $!";
742     setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,
743                                         pack("l", 1))   || die "setsockopt: $!";
744     bind(Server, sockaddr_in($port, INADDR_ANY))        || die "bind: $!";
745     listen(Server,SOMAXCONN)                            || die "listen: $!";
746
747     logmsg "server started on port $port";
748
749     my $paddr;
750
751     $SIG{CHLD} = \&REAPER;
752
753     for ( ; $paddr = accept(Client,Server); close Client) {
754         my($port,$iaddr) = sockaddr_in($paddr);
755         my $name = gethostbyaddr($iaddr,AF_INET);
756
757         logmsg "connection from $name [",
758                 inet_ntoa($iaddr), "]
759                 at port $port";
760
761         print Client "Hello there, $name, it's now ",
762                         scalar localtime, $EOL;
763     }
764
765 And here's a multithreaded version.  It's multithreaded in that
766 like most typical servers, it spawns (forks) a slave server to
767 handle the client request so that the master server can quickly
768 go back to service a new client.
769
770     #!/usr/bin/perl -Tw
771     use strict;
772     BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
773     use Socket;
774     use Carp;
775     my $EOL = "\015\012";
776
777     sub spawn;  # forward declaration
778     sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
779
780     my $port = shift || 2345;
781     my $proto = getprotobyname('tcp');
782
783     ($port) = $port =~ /^(\d+)$/                        or die "invalid port";
784
785     socket(Server, PF_INET, SOCK_STREAM, $proto)        || die "socket: $!";
786     setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,
787                                         pack("l", 1))   || die "setsockopt: $!";
788     bind(Server, sockaddr_in($port, INADDR_ANY))        || die "bind: $!";
789     listen(Server,SOMAXCONN)                            || die "listen: $!";
790
791     logmsg "server started on port $port";
792
793     my $waitedpid = 0;
794     my $paddr;
795
796     use POSIX ":sys_wait_h";
797     sub REAPER {
798         my $child;
799         while (($waitedpid = waitpid(-1,WNOHANG)) > 0) {
800             logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
801         }
802         $SIG{CHLD} = \&REAPER;  # loathe sysV
803     }
804
805     $SIG{CHLD} = \&REAPER;
806
807     for ( $waitedpid = 0;
808           ($paddr = accept(Client,Server)) || $waitedpid;
809           $waitedpid = 0, close Client)
810     {
811         next if $waitedpid and not $paddr;
812         my($port,$iaddr) = sockaddr_in($paddr);
813         my $name = gethostbyaddr($iaddr,AF_INET);
814
815         logmsg "connection from $name [",
816                 inet_ntoa($iaddr), "]
817                 at port $port";
818
819         spawn sub {
820             $|=1;
821             print "Hello there, $name, it's now ", scalar localtime, $EOL;
822             exec '/usr/games/fortune'           # XXX: `wrong' line terminators
823                 or confess "can't exec fortune: $!";
824         };
825
826     }
827
828     sub spawn {
829         my $coderef = shift;
830
831         unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
832             confess "usage: spawn CODEREF";
833         }
834
835         my $pid;
836         if (!defined($pid = fork)) {
837             logmsg "cannot fork: $!";
838             return;
839         } elsif ($pid) {
840             logmsg "begat $pid";
841             return; # I'm the parent
842         }
843         # else I'm the child -- go spawn
844
845         open(STDIN,  "<&Client")   || die "can't dup client to stdin";
846         open(STDOUT, ">&Client")   || die "can't dup client to stdout";
847         ## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
848         exit &$coderef();
849     }
850
851 This server takes the trouble to clone off a child version via fork() for
852 each incoming request.  That way it can handle many requests at once,
853 which you might not always want.  Even if you don't fork(), the listen()
854 will allow that many pending connections.  Forking servers have to be
855 particularly careful about cleaning up their dead children (called
856 "zombies" in Unix parlance), because otherwise you'll quickly fill up your
857 process table.
858
859 We suggest that you use the B<-T> flag to use taint checking (see L<perlsec>)
860 even if we aren't running setuid or setgid.  This is always a good idea
861 for servers and other programs run on behalf of someone else (like CGI
862 scripts), because it lessens the chances that people from the outside will
863 be able to compromise your system.
864
865 Let's look at another TCP client.  This one connects to the TCP "time"
866 service on a number of different machines and shows how far their clocks
867 differ from the system on which it's being run:
868
869     #!/usr/bin/perl  -w
870     use strict;
871     use Socket;
872
873     my $SECS_of_70_YEARS = 2208988800;
874     sub ctime { scalar localtime(shift) }
875
876     my $iaddr = gethostbyname('localhost');
877     my $proto = getprotobyname('tcp');
878     my $port = getservbyname('time', 'tcp');
879     my $paddr = sockaddr_in(0, $iaddr);
880     my($host);
881
882     $| = 1;
883     printf "%-24s %8s %s\n",  "localhost", 0, ctime(time());
884
885     foreach $host (@ARGV) {
886         printf "%-24s ", $host;
887         my $hisiaddr = inet_aton($host)     || die "unknown host";
888         my $hispaddr = sockaddr_in($port, $hisiaddr);
889         socket(SOCKET, PF_INET, SOCK_STREAM, $proto)   || die "socket: $!";
890         connect(SOCKET, $hispaddr)          || die "bind: $!";
891         my $rtime = '    ';
892         read(SOCKET, $rtime, 4);
893         close(SOCKET);
894         my $histime = unpack("N", $rtime) - $SECS_of_70_YEARS ;
895         printf "%8d %s\n", $histime - time, ctime($histime);
896     }
897
898 =head2 Unix-Domain TCP Clients and Servers
899
900 That's fine for Internet-domain clients and servers, but what about local
901 communications?  While you can use the same setup, sometimes you don't
902 want to.  Unix-domain sockets are local to the current host, and are often
903 used internally to implement pipes.  Unlike Internet domain sockets, Unix
904 domain sockets can show up in the file system with an ls(1) listing.
905
906     % ls -l /dev/log
907     srw-rw-rw-  1 root            0 Oct 31 07:23 /dev/log
908
909 You can test for these with Perl's B<-S> file test:
910
911     unless ( -S '/dev/log' ) {
912         die "something's wicked with the log system";
913     }
914
915 Here's a sample Unix-domain client:
916
917     #!/usr/bin/perl -w
918     use Socket;
919     use strict;
920     my ($rendezvous, $line);
921
922     $rendezvous = shift || '/tmp/catsock';
923     socket(SOCK, PF_UNIX, SOCK_STREAM, 0)       || die "socket: $!";
924     connect(SOCK, sockaddr_un($rendezvous))     || die "connect: $!";
925     while (defined($line = <SOCK>)) {
926         print $line;
927     }
928     exit;
929
930 And here's a corresponding server.  You don't have to worry about silly
931 network terminators here because Unix domain sockets are guaranteed
932 to be on the localhost, and thus everything works right.
933
934     #!/usr/bin/perl -Tw
935     use strict;
936     use Socket;
937     use Carp;
938
939     BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
940     sub spawn;  # forward declaration
941     sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
942
943     my $NAME = '/tmp/catsock';
944     my $uaddr = sockaddr_un($NAME);
945     my $proto = getprotobyname('tcp');
946
947     socket(Server,PF_UNIX,SOCK_STREAM,0)        || die "socket: $!";
948     unlink($NAME);
949     bind  (Server, $uaddr)                      || die "bind: $!";
950     listen(Server,SOMAXCONN)                    || die "listen: $!";
951
952     logmsg "server started on $NAME";
953
954     my $waitedpid;
955
956     use POSIX ":sys_wait_h";
957     sub REAPER {
958         my $child;
959         while (($waitedpid = waitpid(-1,WNOHANG)) > 0) {
960             logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
961         }
962         $SIG{CHLD} = \&REAPER;  # loathe sysV
963     }
964
965     $SIG{CHLD} = \&REAPER;
966
967
968     for ( $waitedpid = 0;
969           accept(Client,Server) || $waitedpid;
970           $waitedpid = 0, close Client)
971     {
972         next if $waitedpid;
973         logmsg "connection on $NAME";
974         spawn sub {
975             print "Hello there, it's now ", scalar localtime, "\n";
976             exec '/usr/games/fortune' or die "can't exec fortune: $!";
977         };
978     }
979
980     sub spawn {
981         my $coderef = shift;
982
983         unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
984             confess "usage: spawn CODEREF";
985         }
986
987         my $pid;
988         if (!defined($pid = fork)) {
989             logmsg "cannot fork: $!";
990             return;
991         } elsif ($pid) {
992             logmsg "begat $pid";
993             return; # I'm the parent
994         }
995         # else I'm the child -- go spawn
996
997         open(STDIN,  "<&Client")   || die "can't dup client to stdin";
998         open(STDOUT, ">&Client")   || die "can't dup client to stdout";
999         ## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
1000         exit &$coderef();
1001     }
1002
1003 As you see, it's remarkably similar to the Internet domain TCP server, so
1004 much so, in fact, that we've omitted several duplicate functions--spawn(),
1005 logmsg(), ctime(), and REAPER()--which are exactly the same as in the
1006 other server.
1007
1008 So why would you ever want to use a Unix domain socket instead of a
1009 simpler named pipe?  Because a named pipe doesn't give you sessions.  You
1010 can't tell one process's data from another's.  With socket programming,
1011 you get a separate session for each client: that's why accept() takes two
1012 arguments.
1013
1014 For example, let's say that you have a long running database server daemon
1015 that you want folks from the World Wide Web to be able to access, but only
1016 if they go through a CGI interface.  You'd have a small, simple CGI
1017 program that does whatever checks and logging you feel like, and then acts
1018 as a Unix-domain client and connects to your private server.
1019
1020 =head1 TCP Clients with IO::Socket
1021
1022 For those preferring a higher-level interface to socket programming, the
1023 IO::Socket module provides an object-oriented approach.  IO::Socket is
1024 included as part of the standard Perl distribution as of the 5.004
1025 release.  If you're running an earlier version of Perl, just fetch
1026 IO::Socket from CPAN, where you'll also find modules providing easy
1027 interfaces to the following systems: DNS, FTP, Ident (RFC 931), NIS and
1028 NISPlus, NNTP, Ping, POP3, SMTP, SNMP, SSLeay, Telnet, and Time--just
1029 to name a few.
1030
1031 =head2 A Simple Client
1032
1033 Here's a client that creates a TCP connection to the "daytime"
1034 service at port 13 of the host name "localhost" and prints out everything
1035 that the server there cares to provide.
1036
1037     #!/usr/bin/perl -w
1038     use IO::Socket;
1039     $remote = IO::Socket::INET->new(
1040                         Proto    => "tcp",
1041                         PeerAddr => "localhost",
1042                         PeerPort => "daytime(13)",
1043                     )
1044                   or die "cannot connect to daytime port at localhost";
1045     while ( <$remote> ) { print }
1046
1047 When you run this program, you should get something back that
1048 looks like this:
1049
1050     Wed May 14 08:40:46 MDT 1997
1051
1052 Here are what those parameters to the C<new> constructor mean:
1053
1054 =over 4
1055
1056 =item C<Proto>
1057
1058 This is which protocol to use.  In this case, the socket handle returned
1059 will be connected to a TCP socket, because we want a stream-oriented
1060 connection, that is, one that acts pretty much like a plain old file.
1061 Not all sockets are this of this type.  For example, the UDP protocol
1062 can be used to make a datagram socket, used for message-passing.
1063
1064 =item C<PeerAddr>
1065
1066 This is the name or Internet address of the remote host the server is
1067 running on.  We could have specified a longer name like C<"www.perl.com">,
1068 or an address like C<"204.148.40.9">.  For demonstration purposes, we've
1069 used the special hostname C<"localhost">, which should always mean the
1070 current machine you're running on.  The corresponding Internet address
1071 for localhost is C<"127.1">, if you'd rather use that.
1072
1073 =item C<PeerPort>
1074
1075 This is the service name or port number we'd like to connect to.
1076 We could have gotten away with using just C<"daytime"> on systems with a
1077 well-configured system services file,[FOOTNOTE: The system services file
1078 is in I</etc/services> under Unix] but just in case, we've specified the
1079 port number (13) in parentheses.  Using just the number would also have
1080 worked, but constant numbers make careful programmers nervous.
1081
1082 =back
1083
1084 Notice how the return value from the C<new> constructor is used as
1085 a filehandle in the C<while> loop?  That's what's called an indirect
1086 filehandle, a scalar variable containing a filehandle.  You can use
1087 it the same way you would a normal filehandle.  For example, you
1088 can read one line from it this way:
1089
1090     $line = <$handle>;
1091
1092 all remaining lines from is this way:
1093
1094     @lines = <$handle>;
1095
1096 and send a line of data to it this way:
1097
1098     print $handle "some data\n";
1099
1100 =head2 A Webget Client
1101
1102 Here's a simple client that takes a remote host to fetch a document
1103 from, and then a list of documents to get from that host.  This is a
1104 more interesting client than the previous one because it first sends
1105 something to the server before fetching the server's response.
1106
1107     #!/usr/bin/perl -w
1108     use IO::Socket;
1109     unless (@ARGV > 1) { die "usage: $0 host document ..." }
1110     $host = shift(@ARGV);
1111     $EOL = "\015\012";
1112     $BLANK = $EOL x 2;
1113     foreach $document ( @ARGV ) {
1114         $remote = IO::Socket::INET->new( Proto     => "tcp",
1115                                          PeerAddr  => $host,
1116                                          PeerPort  => "http(80)",
1117                                         );
1118         unless ($remote) { die "cannot connect to http daemon on $host" }
1119         $remote->autoflush(1);
1120         print $remote "GET $document HTTP/1.0" . $BLANK;
1121         while ( <$remote> ) { print }
1122         close $remote;
1123     }
1124
1125 The web server handing the "http" service, which is assumed to be at
1126 its standard port, number 80.  If the web server you're trying to
1127 connect to is at a different port (like 1080 or 8080), you should specify
1128 as the named-parameter pair, C<< PeerPort => 8080 >>.  The C<autoflush>
1129 method is used on the socket because otherwise the system would buffer
1130 up the output we sent it.  (If you're on a Mac, you'll also need to
1131 change every C<"\n"> in your code that sends data over the network to
1132 be a C<"\015\012"> instead.)
1133
1134 Connecting to the server is only the first part of the process: once you
1135 have the connection, you have to use the server's language.  Each server
1136 on the network has its own little command language that it expects as
1137 input.  The string that we send to the server starting with "GET" is in
1138 HTTP syntax.  In this case, we simply request each specified document.
1139 Yes, we really are making a new connection for each document, even though
1140 it's the same host.  That's the way you always used to have to speak HTTP.
1141 Recent versions of web browsers may request that the remote server leave
1142 the connection open a little while, but the server doesn't have to honor
1143 such a request.
1144
1145 Here's an example of running that program, which we'll call I<webget>:
1146
1147     % webget www.perl.com /guanaco.html
1148     HTTP/1.1 404 File Not Found
1149     Date: Thu, 08 May 1997 18:02:32 GMT
1150     Server: Apache/1.2b6
1151     Connection: close
1152     Content-type: text/html
1153
1154     <HEAD><TITLE>404 File Not Found</TITLE></HEAD>
1155     <BODY><H1>File Not Found</H1>
1156     The requested URL /guanaco.html was not found on this server.<P>
1157     </BODY>
1158
1159 Ok, so that's not very interesting, because it didn't find that
1160 particular document.  But a long response wouldn't have fit on this page.
1161
1162 For a more fully-featured version of this program, you should look to
1163 the I<lwp-request> program included with the LWP modules from CPAN.
1164
1165 =head2 Interactive Client with IO::Socket
1166
1167 Well, that's all fine if you want to send one command and get one answer,
1168 but what about setting up something fully interactive, somewhat like
1169 the way I<telnet> works?  That way you can type a line, get the answer,
1170 type a line, get the answer, etc.
1171
1172 This client is more complicated than the two we've done so far, but if
1173 you're on a system that supports the powerful C<fork> call, the solution
1174 isn't that rough.  Once you've made the connection to whatever service
1175 you'd like to chat with, call C<fork> to clone your process.  Each of
1176 these two identical process has a very simple job to do: the parent
1177 copies everything from the socket to standard output, while the child
1178 simultaneously copies everything from standard input to the socket.
1179 To accomplish the same thing using just one process would be I<much>
1180 harder, because it's easier to code two processes to do one thing than it
1181 is to code one process to do two things.  (This keep-it-simple principle
1182 a cornerstones of the Unix philosophy, and good software engineering as
1183 well, which is probably why it's spread to other systems.)
1184
1185 Here's the code:
1186
1187     #!/usr/bin/perl -w
1188     use strict;
1189     use IO::Socket;
1190     my ($host, $port, $kidpid, $handle, $line);
1191
1192     unless (@ARGV == 2) { die "usage: $0 host port" }
1193     ($host, $port) = @ARGV;
1194
1195     # create a tcp connection to the specified host and port
1196     $handle = IO::Socket::INET->new(Proto     => "tcp",
1197                                     PeerAddr  => $host,
1198                                     PeerPort  => $port)
1199            or die "can't connect to port $port on $host: $!";
1200
1201     $handle->autoflush(1);              # so output gets there right away
1202     print STDERR "[Connected to $host:$port]\n";
1203
1204     # split the program into two processes, identical twins
1205     die "can't fork: $!" unless defined($kidpid = fork());
1206
1207     # the if{} block runs only in the parent process
1208     if ($kidpid) {
1209         # copy the socket to standard output
1210         while (defined ($line = <$handle>)) {
1211             print STDOUT $line;
1212         }
1213         kill("TERM", $kidpid);                  # send SIGTERM to child
1214     }
1215     # the else{} block runs only in the child process
1216     else {
1217         # copy standard input to the socket
1218         while (defined ($line = <STDIN>)) {
1219             print $handle $line;
1220         }
1221     }
1222
1223 The C<kill> function in the parent's C<if> block is there to send a
1224 signal to our child process (current running in the C<else> block)
1225 as soon as the remote server has closed its end of the connection.
1226
1227 If the remote server sends data a byte at time, and you need that
1228 data immediately without waiting for a newline (which might not happen),
1229 you may wish to replace the C<while> loop in the parent with the
1230 following:
1231
1232     my $byte;
1233     while (sysread($handle, $byte, 1) == 1) {
1234         print STDOUT $byte;
1235     }
1236
1237 Making a system call for each byte you want to read is not very efficient
1238 (to put it mildly) but is the simplest to explain and works reasonably
1239 well.
1240
1241 =head1 TCP Servers with IO::Socket
1242
1243 As always, setting up a server is little bit more involved than running a client.
1244 The model is that the server creates a special kind of socket that
1245 does nothing but listen on a particular port for incoming connections.
1246 It does this by calling the C<< IO::Socket::INET->new() >> method with
1247 slightly different arguments than the client did.
1248
1249 =over 4
1250
1251 =item Proto
1252
1253 This is which protocol to use.  Like our clients, we'll
1254 still specify C<"tcp"> here.
1255
1256 =item LocalPort
1257
1258 We specify a local
1259 port in the C<LocalPort> argument, which we didn't do for the client.
1260 This is service name or port number for which you want to be the
1261 server. (Under Unix, ports under 1024 are restricted to the
1262 superuser.)  In our sample, we'll use port 9000, but you can use
1263 any port that's not currently in use on your system.  If you try
1264 to use one already in used, you'll get an "Address already in use"
1265 message.  Under Unix, the C<netstat -a> command will show
1266 which services current have servers.
1267
1268 =item Listen
1269
1270 The C<Listen> parameter is set to the maximum number of
1271 pending connections we can accept until we turn away incoming clients.
1272 Think of it as a call-waiting queue for your telephone.
1273 The low-level Socket module has a special symbol for the system maximum, which
1274 is SOMAXCONN.
1275
1276 =item Reuse
1277
1278 The C<Reuse> parameter is needed so that we restart our server
1279 manually without waiting a few minutes to allow system buffers to
1280 clear out.
1281
1282 =back
1283
1284 Once the generic server socket has been created using the parameters
1285 listed above, the server then waits for a new client to connect
1286 to it.  The server blocks in the C<accept> method, which eventually accepts a
1287 bidirectional connection from the remote client.  (Make sure to autoflush
1288 this handle to circumvent buffering.)
1289
1290 To add to user-friendliness, our server prompts the user for commands.
1291 Most servers don't do this.  Because of the prompt without a newline,
1292 you'll have to use the C<sysread> variant of the interactive client above.
1293
1294 This server accepts one of five different commands, sending output
1295 back to the client.  Note that unlike most network servers, this one
1296 only handles one incoming client at a time.  Multithreaded servers are
1297 covered in Chapter 6 of the Camel.
1298
1299 Here's the code.  We'll
1300
1301  #!/usr/bin/perl -w
1302  use IO::Socket;
1303  use Net::hostent;              # for OO version of gethostbyaddr
1304
1305  $PORT = 9000;                  # pick something not in use
1306
1307  $server = IO::Socket::INET->new( Proto     => 'tcp',
1308                                   LocalPort => $PORT,
1309                                   Listen    => SOMAXCONN,
1310                                   Reuse     => 1);
1311
1312  die "can't setup server" unless $server;
1313  print "[Server $0 accepting clients]\n";
1314
1315  while ($client = $server->accept()) {
1316    $client->autoflush(1);
1317    print $client "Welcome to $0; type help for command list.\n";
1318    $hostinfo = gethostbyaddr($client->peeraddr);
1319    printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost;
1320    print $client "Command? ";
1321    while ( <$client>) {
1322      next unless /\S/;       # blank line
1323      if    (/quit|exit/i)    { last;                                     }
1324      elsif (/date|time/i)    { printf $client "%s\n", scalar localtime;  }
1325      elsif (/who/i )         { print  $client `who 2>&1`;                }
1326      elsif (/cookie/i )      { print  $client `/usr/games/fortune 2>&1`; }
1327      elsif (/motd/i )        { print  $client `cat /etc/motd 2>&1`;      }
1328      else {
1329        print $client "Commands: quit date who cookie motd\n";
1330      }
1331    } continue {
1332       print $client "Command? ";
1333    }
1334    close $client;
1335  }
1336
1337 =head1 UDP: Message Passing
1338
1339 Another kind of client-server setup is one that uses not connections, but
1340 messages.  UDP communications involve much lower overhead but also provide
1341 less reliability, as there are no promises that messages will arrive at
1342 all, let alone in order and unmangled.  Still, UDP offers some advantages
1343 over TCP, including being able to "broadcast" or "multicast" to a whole
1344 bunch of destination hosts at once (usually on your local subnet).  If you
1345 find yourself overly concerned about reliability and start building checks
1346 into your message system, then you probably should use just TCP to start
1347 with.
1348
1349 Note that UDP datagrams are I<not> a bytestream and should not be treated
1350 as such. This makes using I/O mechanisms with internal buffering
1351 like stdio (i.e. print() and friends) especially cumbersome. Use syswrite(),
1352 or better send(), like in the example below.
1353
1354 Here's a UDP program similar to the sample Internet TCP client given
1355 earlier.  However, instead of checking one host at a time, the UDP version
1356 will check many of them asynchronously by simulating a multicast and then
1357 using select() to do a timed-out wait for I/O.  To do something similar
1358 with TCP, you'd have to use a different socket handle for each host.
1359
1360     #!/usr/bin/perl -w
1361     use strict;
1362     use Socket;
1363     use Sys::Hostname;
1364
1365     my ( $count, $hisiaddr, $hispaddr, $histime,
1366          $host, $iaddr, $paddr, $port, $proto,
1367          $rin, $rout, $rtime, $SECS_of_70_YEARS);
1368
1369     $SECS_of_70_YEARS      = 2208988800;
1370
1371     $iaddr = gethostbyname(hostname());
1372     $proto = getprotobyname('udp');
1373     $port = getservbyname('time', 'udp');
1374     $paddr = sockaddr_in(0, $iaddr); # 0 means let kernel pick
1375
1376     socket(SOCKET, PF_INET, SOCK_DGRAM, $proto)   || die "socket: $!";
1377     bind(SOCKET, $paddr)                          || die "bind: $!";
1378
1379     $| = 1;
1380     printf "%-12s %8s %s\n",  "localhost", 0, scalar localtime time;
1381     $count = 0;
1382     for $host (@ARGV) {
1383         $count++;
1384         $hisiaddr = inet_aton($host)    || die "unknown host";
1385         $hispaddr = sockaddr_in($port, $hisiaddr);
1386         defined(send(SOCKET, 0, 0, $hispaddr))    || die "send $host: $!";
1387     }
1388
1389     $rin = '';
1390     vec($rin, fileno(SOCKET), 1) = 1;
1391
1392     # timeout after 10.0 seconds
1393     while ($count && select($rout = $rin, undef, undef, 10.0)) {
1394         $rtime = '';
1395         ($hispaddr = recv(SOCKET, $rtime, 4, 0))        || die "recv: $!";
1396         ($port, $hisiaddr) = sockaddr_in($hispaddr);
1397         $host = gethostbyaddr($hisiaddr, AF_INET);
1398         $histime = unpack("N", $rtime) - $SECS_of_70_YEARS ;
1399         printf "%-12s ", $host;
1400         printf "%8d %s\n", $histime - time, scalar localtime($histime);
1401         $count--;
1402     }
1403
1404 Note that this example does not include any retries and may consequently
1405 fail to contact a reachable host. The most prominent reason for this
1406 is congestion of the queues on the sending host if the number of
1407 list of hosts to contact is sufficiently large.
1408
1409 =head1 SysV IPC
1410
1411 While System V IPC isn't so widely used as sockets, it still has some
1412 interesting uses.  You can't, however, effectively use SysV IPC or
1413 Berkeley mmap() to have shared memory so as to share a variable amongst
1414 several processes.  That's because Perl would reallocate your string when
1415 you weren't wanting it to.
1416
1417 Here's a small example showing shared memory usage.
1418
1419     use IPC::SysV qw(IPC_PRIVATE IPC_RMID S_IRWXU);
1420
1421     $size = 2000;
1422     $id = shmget(IPC_PRIVATE, $size, S_IRWXU) || die "$!";
1423     print "shm key $id\n";
1424
1425     $message = "Message #1";
1426     shmwrite($id, $message, 0, 60) || die "$!";
1427     print "wrote: '$message'\n";
1428     shmread($id, $buff, 0, 60) || die "$!";
1429     print "read : '$buff'\n";
1430
1431     # the buffer of shmread is zero-character end-padded.
1432     substr($buff, index($buff, "\0")) = '';
1433     print "un" unless $buff eq $message;
1434     print "swell\n";
1435
1436     print "deleting shm $id\n";
1437     shmctl($id, IPC_RMID, 0) || die "$!";
1438
1439 Here's an example of a semaphore:
1440
1441     use IPC::SysV qw(IPC_CREAT);
1442
1443     $IPC_KEY = 1234;
1444     $id = semget($IPC_KEY, 10, 0666 | IPC_CREAT ) || die "$!";
1445     print "shm key $id\n";
1446
1447 Put this code in a separate file to be run in more than one process.
1448 Call the file F<take>:
1449
1450     # create a semaphore
1451
1452     $IPC_KEY = 1234;
1453     $id = semget($IPC_KEY,  0 , 0 );
1454     die if !defined($id);
1455
1456     $semnum = 0;
1457     $semflag = 0;
1458
1459     # 'take' semaphore
1460     # wait for semaphore to be zero
1461     $semop = 0;
1462     $opstring1 = pack("s!s!s!", $semnum, $semop, $semflag);
1463
1464     # Increment the semaphore count
1465     $semop = 1;
1466     $opstring2 = pack("s!s!s!", $semnum, $semop,  $semflag);
1467     $opstring = $opstring1 . $opstring2;
1468
1469     semop($id,$opstring) || die "$!";
1470
1471 Put this code in a separate file to be run in more than one process.
1472 Call this file F<give>:
1473
1474     # 'give' the semaphore
1475     # run this in the original process and you will see
1476     # that the second process continues
1477
1478     $IPC_KEY = 1234;
1479     $id = semget($IPC_KEY, 0, 0);
1480     die if !defined($id);
1481
1482     $semnum = 0;
1483     $semflag = 0;
1484
1485     # Decrement the semaphore count
1486     $semop = -1;
1487     $opstring = pack("s!s!s!", $semnum, $semop, $semflag);
1488
1489     semop($id,$opstring) || die "$!";
1490
1491 The SysV IPC code above was written long ago, and it's definitely
1492 clunky looking.  For a more modern look, see the IPC::SysV module
1493 which is included with Perl starting from Perl 5.005.
1494
1495 A small example demonstrating SysV message queues:
1496
1497     use IPC::SysV qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU);
1498
1499     my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
1500
1501     my $sent = "message";
1502     my $type = 1234;
1503     my $rcvd;
1504     my $type_rcvd;
1505
1506     if (defined $id) {
1507         if (msgsnd($id, pack("l! a*", $type_sent, $sent), 0)) {
1508             if (msgrcv($id, $rcvd, 60, 0, 0)) {
1509                 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
1510                 if ($rcvd eq $sent) {
1511                     print "okay\n";
1512                 } else {
1513                     print "not okay\n";
1514                 }
1515             } else {
1516                 die "# msgrcv failed\n";
1517             }
1518         } else {
1519             die "# msgsnd failed\n";
1520         }
1521         msgctl($id, IPC_RMID, 0) || die "# msgctl failed: $!\n";
1522     } else {
1523         die "# msgget failed\n";
1524     }
1525
1526 =head1 NOTES
1527
1528 Most of these routines quietly but politely return C<undef> when they
1529 fail instead of causing your program to die right then and there due to
1530 an uncaught exception.  (Actually, some of the new I<Socket> conversion
1531 functions  croak() on bad arguments.)  It is therefore essential to
1532 check return values from these functions.  Always begin your socket
1533 programs this way for optimal success, and don't forget to add B<-T>
1534 taint checking flag to the #! line for servers:
1535
1536     #!/usr/bin/perl -Tw
1537     use strict;
1538     use sigtrap;
1539     use Socket;
1540
1541 =head1 BUGS
1542
1543 All these routines create system-specific portability problems.  As noted
1544 elsewhere, Perl is at the mercy of your C libraries for much of its system
1545 behaviour.  It's probably safest to assume broken SysV semantics for
1546 signals and to stick with simple TCP and UDP socket operations; e.g., don't
1547 try to pass open file descriptors over a local UDP datagram socket if you
1548 want your code to stand a chance of being portable.
1549
1550 As mentioned in the signals section, because few vendors provide C
1551 libraries that are safely re-entrant, the prudent programmer will do
1552 little else within a handler beyond setting a numeric variable that
1553 already exists; or, if locked into a slow (restarting) system call,
1554 using die() to raise an exception and longjmp(3) out.  In fact, even
1555 these may in some cases cause a core dump.  It's probably best to avoid
1556 signals except where they are absolutely inevitable.  This
1557 will be addressed in a future release of Perl.
1558
1559 =head1 AUTHOR
1560
1561 Tom Christiansen, with occasional vestiges of Larry Wall's original
1562 version and suggestions from the Perl Porters.
1563
1564 =head1 SEE ALSO
1565
1566 There's a lot more to networking than this, but this should get you
1567 started.
1568
1569 For intrepid programmers, the indispensable textbook is I<Unix Network
1570 Programming> by W. Richard Stevens (published by Addison-Wesley).  Note
1571 that most books on networking address networking from the perspective of
1572 a C programmer; translation to Perl is left as an exercise for the reader.
1573
1574 The IO::Socket(3) manpage describes the object library, and the Socket(3)
1575 manpage describes the low-level interface to sockets.  Besides the obvious
1576 functions in L<perlfunc>, you should also check out the F<modules> file
1577 at your nearest CPAN site.  (See L<perlmodlib> or best yet, the F<Perl
1578 FAQ> for a description of what CPAN is and where to get it.)
1579
1580 Section 5 of the F<modules> file is devoted to "Networking, Device Control
1581 (modems), and Interprocess Communication", and contains numerous unbundled
1582 modules numerous networking modules, Chat and Expect operations, CGI
1583 programming, DCE, FTP, IPC, NNTP, Proxy, Ptty, RPC, SNMP, SMTP, Telnet,
1584 Threads, and ToolTalk--just to name a few.