Perl Tutorials - Herong's Tutorial Examples - v6.03, by Herong Yang
GameClient.pl - 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, GameClient.pl:
#- GameClient.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.
use Socket;
use XML::Simple;
&init;
$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);
exit;
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) = @_;
socket(SOCK,PF_INET,SOCK_STREAM,'tcp');
# 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";
close(STDIN);
close(STDOUT);
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:
<c><m>Hello</m></c>
2. game_2.xml:
<c><i>0</i><n>0</n></c>
If you run the client program, you should get the following in the GameClient.rec recording file:
a<c> <m>Hello</m> </c> <s> <m>Welcome to Game Server! I have a number between 0 and 99 for you to guess.</m> <i>1</i> </s> <c> <n>39</n> <i>1</i> </c> <s> <m>Your guess is too high. Please make another guess.</m> <i>1</i> </s> <c> <n>29</n> <i>-1</i> </c> <s> <m>Sorry. Your game ID doesn't exist.</m> <i>0</i> </s>
This clearly shows you a simple conversation between the client and server with XML messages.
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
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 Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
XML Messages over Socket Connections
GameServer.pl - XML Communication Server Example
►GameClient.pl - 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
CGI.pm Module for Building Web Pages
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary