Tutorial Addendum on Perl - Allotment B - XML in Applicant and Server Advice

 31 December 18:00   




This affiliate describes:



  • Different XML applicant and server advice models.



  • XML and Atrium Advice Archetype - GameServer.pl



  • XML and Atrium Advice Archetype - GameClient.pl






XML Applicant and Server Advice Model



Once we understand how to plan XML strings and hashes, we can now attending at how


XML technology can be acclimated in applicant and server communication.



The alotof archaic way of using XML technology in applicant and server communication


is to address a applicant program and a server program to forward and accept XML messages


with TCP atrium advice over Internet. The afterward diagram illustrates how this works:



Client Arrangement Server System





XML | Internet | XML


Client Prog.<--->TCP|<-------------->|TCP<--->Server Prog.




To apprentice added about TCP atrium communication, see the "Socket Communication"


chapter of this book.



XML with Atrium Advice Archetype - GameServer.pl



In the afterward example, I wrote a simple applicant and


server abject bold application. It s a simple game, in which the server


accepts new clients, and holds a amount for the applicant to guess.



The advice is exchanged amid the applicant and server in plain


XML format, and delivered with apparent TCP advice protocol.



Here is the server program, GameServer.pl:




#- GameServer.pl


#- Absorb (c) 1999 by Dr. Yang




use Socket;


use XML::Simple;


$xs = new XML::Simple(keeproot => 1, forcearray => 1);


$gameID = 0;


$number = 0;


$response = "<s><i>0</i><m>hello</m></s>";






sub runServer {


open(LOG,">> GameServer.log");


select(LOG); $|=1;


open(STDERR, ">&LOG") || die "Die: Ambience STDERR to log file";


open(REC,">> GameServer.rec");


select(REC); $|=1;




bind(SOCK, pack_sockaddr_in("8080", INADDR_ANY));


listen(SOCK, 1); # can alone yield one applicant at a time


book LOG localtime().": Alert to anchorage 8080


for (;;) {


$cAddress = accept(NEWSOCK,SOCK) || die "Error: Accepting: $!";


($cPort, $cHost) = unpack_sockaddr_in($cAddress);


$cHostName = inet_ntoa($cHost);


book LOG localtime().": Affiliated with $cHostName at $cPort


open(STDIN, "+<&NEWSOCK") || die "Die: Ambience STDIN to socket";


open(STDOUT, "+>&NEWSOCK") || die "Die: Ambience STDOUT to socket";


select(STDOUT); $|=1;












sub serve {


my ($tag,$fin,$msg);


while (<STDIN>) {


/<(w+)>/ && ($tag=$1) unless $tag;


/</$tag>/ && ($fin=$tag) if $tag;


$msg = $msg.$_;


endure if $fin;




book REC "$msg


my $ref = $xs->XMLin($msg);


if (exists($ref->{c}->->{i})) {


my $gid = $ref->{c}->->{i}->;


my $num = $ref->{c}->->{n}->;


if ($gid == $gameID) {


$msg = &oldGame($gid,$num);


} abroad {


$msg = &invalidGame($gid,$num);




} abroad {


$msg = &newGame;




book STDOUT $msg;


book REC "$msg




sub oldGame {


my ($gid,$num) = @_;


my $ref = $xs->XMLin($response);


my $msg;


if ($num == $number) {


$msg = "Congratulations!


."I accept addition amount amid 0 and 99 for you to guess.";




$number = rand(100);


} elsif ($num > $number) {


$msg = "Your assumption is too high.
Amuse create addition quess.";


} abroad {


$msg = "Your assumption is too low.
Amuse create addition quess.";




$ref->{s}->->{i}-> = $gameID;


$ref->{s}->->{m}-> = $msg;


acknowledgment $xs->XMLout($ref);




sub newGame {




$number = rand(100);


my $ref = $xs->XMLin($response);


$ref->{s}->->{m}-> = "Welcome to Bold Server!


."I accept a amount amid 0 and 99 for you to guess.";


$ref->{s}->->{i}-> = $gameID;


acknowledgment $xs->XMLout($ref);




sub invalidGame {


my ($sid,$num) = @_;


my $ref = $xs->XMLin($response);


$ref->{s}->->{m}-> = "Sorry. Your bold ID doesn t exist.";


acknowledgment $xs->XMLout($ref);







Tags: simple, program, example, client, server, communication, setting, response, print

 server, communication, client, socket, gameid, gameserver, print, stdout, xmlin, stdin, response, example, simple, return, xmlout, select, program, setting, newsock, , client and, socket communication, ref {s}, server communication, xmlout ref, xmlin response, ref {c}, communication example, msg &, gid num, socket communication example, msg your guess, gameid number rand, tcp socket communication, communication example gameserver,

Share Tutorial Addendum on Perl - Allotment B - XML in Applicant and Server Advice:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Article In : Computers & Technology  -  perl