Tutorial Addendum on Perl - Allotment B - Atrium Advice
| |
listen() - Sets a atrium handle to accept approach with the defined chain admeasurement of
incoming affiliation requests. This action is alone acclimated by the server application.
For example:
$queueSize = 5;
listen(SOCK, $queueSize);
accept() - Waits for admission affiliation requests on the defined atrium handle,
accepts the first affiliation request, creates a new atrium handle, one end apprenticed to the aforementioned
local abode as the defined atrium handle, the additional end apprenticed to the remote
address accustomed in the affiliation request, and allotment the abolish address.
If no error, the new atrium handle is accessible for data transmission.
This action is alone acclimated by the server application.
For example:
$address = accept(NEWSOCK,SOCK);
connect() - Sends a advice appeal to a limited abode that represents a anchorage
on a limited system. If the limited arrangement accustomed the request, the defined socket
handle will be apprenticed to the limited address. If no error, the defined atrium handle
is accessible for data transmission. This action is alone acclimated by the applicant application.
For example:
$domain = 2; # Internet domain
$host = pack( C4 , 216,109,118,67); # www.yahoo.com = 216.109.118.67
$port = 80;
$address = pack( S n a4 x8 , $domain, $port, $host);
connect(SOCK, $address);
The afterward diagram shows the accomplish complex in establishing an Internet
socket advice hotlink using the congenital functions:
Applicant Arrangement Server System
Internet Abode #a Internet Abode #b
Step Accessible Anchorage #p Accessible Anchorage #q
1 socket(SOCK,2,1,6);
2 bind(SOCK,#b+#q);
3 listen(SOCK,5);
4 accept(NEWSOCK,SOCK);
5 Socket(SOCK,2,1,6); (waiting)
6 bind(SOCK,#a+#p); (waiting)
7 connect(SOCK,#b+#q); (receiving request)
8 (estalishing the link) (establishing the link)
9 (SOCK is ready) (NEWSOCK is ready)
ReverseEchoer.pl - A Simple Atrium Server Application
The afterward program alleged ReverseEchoer.pl is a simple atrium server application,
which listens for a affiliation request. Already connected, it
reads what curve of argument from the applicant application, reverses the argument lines,
and echoes aback to the limited application:
#- ReverseEchoer.pl
#- Absorb (c) 1999 by Dr. Yang
#
$domain = 2; # Internet domain
$type = 1; # Sequenced, reliable, two-way connection, byte streams
$proto = 6; # Manual Ascendancy Agreement (TCP)
socket(SOCK,$domain,$type,$proto);
$host = pack( C4 , 0,0,0,0); # Bounded wildcard host id: 0.0.0.0
$port = 8888;
$address = pack( S n a4 x8 , $domain, $port, $host);
bind(SOCK, $address);
$queueSize = 5; # Chain up to 5 connections
listen(SOCK, $queueSize);
book STDOUT "Server host: ",join( . ,unpack( C4 , $host)),"
";
book STDOUT "Server port: $port
";
$cAddress = accept(NEWSOCK,SOCK);
($cDomain, $cPort, $cHost) = unpack( S n a4 x8 , $cAddress);
book STDOUT "Client host: ",join( . ,unpack( C4 , $cHost)),"
";
book STDOUT "Client port: $cPort
";
select(NEWSOCK); $| = 1; select(STDOUT);
book NEWSOCK "Welcome to About-face Answer Server.
";
while ($m=<NEWSOCK>) {
$m =~ s/
|
//g;
endure if ($m eq ".");
$m = reverse($m);
}
close(NEWSOCK);
exit;
Note that:
- The atrium is apprenticed to the bounded arrangement with the wildcard host abode 0.0.0.0,
which represents any IP abode on the bounded host system.
- Setting $| to 1 is to about-face on auto even to the absence book handle. In adjustment to
turn on auto even to NEWSOCK, I accept to use select() action to about-face absence file
handles.
- If a argument band accustomed alone contains a individual appearance . ,
the advice hotlink will be concluded by the close() call.
Run ReverseEchoer.pl, you will get the afterward achievement on the animate window:
Server host: 0.0.0.0
Server port: 8888
This tells us that the program is accessible to acquire appeal at abode 0.0.0.0
and anchorage 8888.
To analysis ReverseEchoer.pl, we can use an absolute applicant program alleged telnet
to admit the advice appeal and allocution to ReverseEchoer.pl. Accessible addition
command window and blazon in the afterward command:
telnet localhost 8888
Immediately, you will see added achievement on the animate window of ReverseEchoer:
Client host: 127.0.0.1
Client port: 1032
This tells us that the server atrium accustomed a affiliation request, and
a advice hotlink has be accustomed with the applicant application,
which is the telnet program active on the aforementioned machine. The applicant host, 127.0.0.1,
is the accepted IP abode for "localhost". The applicant port, 1032, was
picked up by the telnet program.
|
socket, address, client, newsock, server, request, domain, reverseechoer, application, communication, handle, connection, print, system, program, stdout, remote, ready, accept, specified, listen, internet, function, queuesize, following, local, bound, select, window, telnet, unpack, connect, transmission, example, received, , socket handle, print stdout, connection request, communication link, bind sock, client host, client application, socket sock, accept newsock, server application, application for, listen sock, specified socket, newsock sock, specified socket handle, accept newsock sock, simple socket server, print stdout server, host join unpack, print stdout client, domain port host, transmission this function, listen sock queuesize, new socket handle, data transmission this, incoming connection requests, |
Also see ...
In the telnet window, blazon in the afterward text: Fish, I adulation you and account you actual much.But I will annihilate you asleep afore this day ends..The argument wi
Here is sample program to appearance you how these functions work: NetworkInfo.pm Absorb (c) 1999 by Dr. Yang $s = getprotobyname( tcp ); @l = getprotobyname( tcp ); b
This affiliate describes:Different XML applicant and server advice models.XML and Atrium Advice Archetype GameServer.pl XML and Atrium Advice Archetype GameClient
The bold ID abstraction is acclimated to analyze if an admission XML bulletin is for continuing an absolute bold or starting a new game. If the serverreceives a applicant bulletin for the first time, it will accredit a new bold
This affiliate describes:What is XML RPC?XML RPC specifications.For added advice XSD and XML, see my additional book: " s addendum on XML Tec
From the XML schema, we can see that:A "methodCall" aspect will accept a appropriate "methodName" aspect and an optional"params" element.A "methodName" aspect specifies the name of t
This affiliate describes:Introduction to XML::Simple module.Example Perl programs to use XML::Simple options.Example Perl program to adapt the parsed XML hash.
"forcearray" Archetype XmlSimpleArray.plThe afterward program shows you how to use options, keeproot, searchpath, and forcearray: XmlSimpleArray.pl A
Here is the achievement of the program: Hash dump after suppressempty = :$VAR1 = { arrangement = } ] };XML achievement after suppressempty = :<syst
"keyattr" Archetype XmlSimpleKey.plkeyattr = list: Applies to XMLin() and XMLout() to name attributes, or sub elements as keys to be acclimated to promot the ancestor aspect froma