- XML Communication Client Example

This section provides a tutorial example on how write an XML communication client program to connect to a server, send and receive XML messages.

Now let's see a real client program,

#- Copyright (c) 1999 by Dr. Herong Yang,
   use Socket;
   use XML::Simple;
   $ref = $xs->XMLin("game_1.xml");
   $ref = &send($ref);
   $gid = $ref->{s}->[0]->{i}->[0];
   $ref = $xs->XMLin("game_2.xml");
   $ref->{c}->[0]->{i}->[0] = $gid;
   $ref->{c}->[0]->{n}->[0] = "39";
   $ref = &send($ref);
   $ref = $xs->XMLin("game_2.xml");
   $ref->{c}->[0]->{i}->[0] = "-1";
   $ref->{c}->[0]->{n}->[0] = "29";
   $ref = &send($ref);
sub init {
   open(LOG,">> GameClient.log");
   select(LOG); $|=1;
   open(STDERR, ">&LOG") || die "Die: Setting STDERR to log file";
   open(REC,">> GameClient.rec");
   select(REC); $|=1;
   $xs = new XML::Simple(keeproot => 1, forcearray => 1, 
      searchpath => ".");
   $host = "localhost";
   $port = "8080";
sub send {
   my ($ref) = @_;
#   bind(SOCK, pack_sockaddr_in("1024", INADDR_ANY)); # no need
   $sAddress = pack_sockaddr_in($port,inet_aton($host));
   connect(SOCK, $sAddress) || die "Die: Connecting to $host $port";
   open(STDIN, "+<&SOCK") || die "Die: Setting socket STDIN";
   open(STDOUT, "+>&SOCK") || die "Die: Setting socket STDOUT";
   select(STDOUT); $|=1;
   my $xml = $xs->XMLout($ref);
   print STDOUT $xml;
   print REC "$xml\n";
   my ($tag,$fin,$msg);
   while (<STDIN>) {
      /<(\w+)>/ && ($tag=$1) unless $tag;
      /<\/$tag>/ && ($fin=$tag) if $tag;
      $msg = $msg.$_;
      last if $fin;
   print REC "$msg\n";
   return $xs->XMLin($msg);

This client program is also simple. It reads some XML message files prepared a game, and send them over to the server one at a time. There are two XML message files:

1. game_1.xml:


2. game_2.xml:


If you run the client program, you should get the following in the GameClient.rec recording file:


  <m>Welcome to Game Server!
I have a number between 0 and 99 for you to guess.</m>


  <m>Your guess is too high.
 Please make another quess.</m>


  <m>Sorry. Your game ID doesn't exist.</m>

This clearly shows you a simple conversation between the client and server with XML messages.

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

XML Communication Model

 XML Messages over Socket Connections - XML Communication Server Example - XML Communication Client Example

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server Module for Building Web Pages

 LWP::UserAgent and Web Site Testing


 PDF Printing Version