Professional Documents
Culture Documents
7 Study of DNS
Objective: To understand system calls for client server communication using socket
programming.
1. Multiple clients at a time read string from its standard input and write
the line to the server.
2. The server read a line from its network input and echoes the line back to
the clients.
3. The clients read the echoed line and print it on its standard output.
Theory:
#include <sys/types.h>
#include <sys/socket.h>
int socket(int family, int type, int protocol);
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *peer,int *addrlen);
accept takes the first connection request on the queue and creates
another socket with the same properties as sockfd. If there are no
connection request pending, this call blocks the caller until on arrives.
The peer and addrlen arguments are used to return the address of
the connected peer process (the client).
The system call returns up to three values : an integer return code
that is either an error indication or a new socket descriptor, the address of
the client process(peer), and the size of this address(addrlen).
6.read System call
Data is read from buffer
int read(int sockfd,char *buff,unsigned int nbytes);
If the read is successful , the number of bytes read is returned- this can be
less than the nbytes that was requested. If the end of buffer is encountered,
zero is returned. If an error is encountered, -1 is returned.
#include <sys/types.h>
#include <sys/socket.h>
http:/beej.us/guide/bgnet/
Experiment No 2
Title: Implementation of RPC mechanism using RPCGEN utility.
Theory:
RPCGEN is an interface generator pre-compiler for Sun Microsystems RPC. It
uses an interface definition file to create client and server stubs in C.
RPCGEN creates stubs based on information contained within an IDL file. This
file is written in a language called RPCL - remote procedure call language. This language
closely mimics C in style.
An RPC specification contains a number of definitions. These definitions are used
by RPCGEN to create a header file for use by both the client and server, and client and
server stubs.
RCPL Definitions: Constant, Enumeration, Struct, Union, and TypeDef,
Program.
Remote Procedure Call (RPC) is a protocol that one program can use to request a
service from a program located in another computer in a network without having to
understand network details. (A procedure call is also sometimes known as a function call
or a subroutine call.) RPC uses the client/server model. The requesting program is a
client and the service-providing program is the server. Like a regular or local procedure
call, an RPC is a synchronous operation requiring the requesting program to be
suspended until the results of the remote procedure are returned.
When program statements that use RPC are compiled into an executable program,
a stub is included in the compiled code that acts as the representative of the remote
procedure code. When the program is run and the procedure call is issued, the stub
receives the request and forwards it to a client runtime program in the local computer.
The client runtime program has the knowledge of how to address the remote computer
and server application and sends the message across the network that requests the remote
procedure. Similarly, the server includes a runtime program and stub that interface with
the remote procedure itself. Results are returned the same way.
Remote procedure calls (RPC) extend the capabilities of conventional procedure
calls across a network and are essential in the development of distributed systems.
They can be used both for data exchange in distributed file and database systems
and for harnessing the power of multiple processors.
Linux distributions provide an RPC version derived from the RPC facility
developed by the Open Network Computing (ONC) group at Sun Microsystems.
1. The caller program must prepare any input parameters to be passed to the RPC.
Note that the caller and the callee may be running completely different hardware, and
that certain data types may be represented differently from one machine architecture
to the next. Because of this, the caller cannot simply feed raw data to the remote
procedure.
2. The calling program must somehow pass its data to the remote host which will
execute the RPC. In local procedure calls, the target address is simply a machine
address on the local processor. With RPC, the target procedure has a machine address
combined with a network address.
3. The RPC receives and operates on any input parameters and passes the result back
to the caller.
4. The calling program receives the RPC result and continues execution.
One such standard is the ONC external data representation (XDR). XDR is
essentially a collection of C functions and macros that enable conversion from machine
specific data representations to the corresponding standard representations and vice versa.
It contains primitives for simple data types such as int, float and string and provides the
capability to define and transport more complex ones such as records, arrays of arbitrary
element type and pointer bound structures such as linked lists.
Client Server
Client Code Server
Data Code Data
XDR Filter
2 XDR Filter 3
1
The protocol is the transport mechanism (typically TCP or UDP) which is used to
move the data between hosts. This, of course, is the part that needs to be common to both
host computers. For either host computer, the local-address/process pair defines the
endpoint on the host computer running that process. The foreign-address/process pair
refers to the endpoint at the opposite end of the connection.
Breaking this down further, the term address refers to the network address
assigned to the host. This would typically be an Internet Protocol (IP) address. The term
process refers not to an actual process identifier (such as a Unix PID) but to some integer
identifier required to transport the data to the correct process once it has arrived at the
correct host computer. This is generally referred to as a port. The reason port numbers
are used is that it is not practical for a process running on a remote host to know the PID
of a particular server. Standard port numbers are assigned to well known services such as
TELNET (port 23) and FTP (port 21).
Portmapper
Procedure Call Dataflow
Registration
Data
Flow
Client
Process
Server
Process
Program no.
Port no
Program no.
The RPC application then waits for and accepts connections at that port number. Prior to
calling the remote procedure, the caller also contacts portmap in order to obtain the
corresponding port number being used by the application whose procedures it needs to
call. The network connection provides the means for the caller to reach the correct
program on the remote host. The correct procedure is reached through the use of a
dispatch table in the RPC program. The same registration process that establishes the port
number also creates the dispatch table. The dispatch table is indexed by procedure
number and contains the addresses of all the XDR filter routines as well as the addresses
of the actual procedures.
References: 1.http://docs.freebsd.org
2.http://h30097.www3.hp.com
Experiment No. 3
Aim: ATM Banking Transaction: Application should have facility to withdraw, deposit
money, transfer money from one account to other account and check the balance of
account
RMI - The Java Remote Method Invocation (RMI) system allows an object
running in one Java virtual machine to invoke methods on an object running in another
Java virtual machine. RMI provides for remote communication between programs written
in the Java programming language.
RMI is a mechanism for communicating (only) between two machines running Java
Virtual Machines. When Java code on machine A needs a service or a method,
respectively, of (remote Java object) objB on machine B it starts a remote method
invocation. It does this the same way as invoking a local (Java) object's method.The
whole communication behind the remote invocation is done by the RMI mechanism.
The stub object unmarshals the return value or exception from the server. This
value becomes the return value of the remote method invocation. Or, if the remote
method threw an exception, the stub (object) rethrows it in the process space of the caller.
Example
To get a better feeling how to deal with RMI, let's give a little example:
Let's assume there is a remote object belonging to the class Product offering the method
getDescription(), whose return value is a String object. Let's further assume this method
is invoked by a remote machine (not very surprising). The following figure shows what is
going on in order to handle this situation, on client side as well as on server side.
Simple Client/Server Application using RMI:
In Remote Calculator Service server receives a request from client, processes it, and
returns a result. The request specifies two numbers. The server adds these together and
returns the sum. Below are the steps to implement Remote Calculator Service (Addition).
Application contains four source files. First file, IMath.java , defines remote
interface that is provided by the server. It contains one method that accepts two integer
arguments and returns their sum.
All remote interfaces must extend the Remote Interface , which is part of java.rmi.
Remote defines no members. Its purpose is simply to indicate that an interfaces uses
remote methods. All Remote methods can throw a RemoteException.
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
{ }
{ return arg1+arg2; }
Third source file, Server.java , contains main program for the server machine.Iits primary
function is to update the RMI registry on that machine.This is done by using the rebind()
method of Naming class(found in java.rmi). That method associates a name with an
object reference. The first argument to the rebind() method is a string that names the
server as “rmimathcalc”.Its second arguemtn is a reference to an instance of
AddServerImpl.
import java.rmi.Naming;
Naming.rebind ("rmimathcalc",svr);
The fourth source file, Client.java , implements the client side of this distributed
application. Client.java requires three command line arguments. The first is IP address or
name of server machine.The second and third arguments are the two numbers that are to
be summed.
The program then invokes the lookup() method of the Naming class.This method
accepts one argument, the rmi URL which includes rmi protocol,IP address or name of
server will be given by the client in command line argument and the string
“rmimathcalc”.It returns reference to Imath.
import java.rmi.Naming;
int firstnumber=Integer.parseInt(args[1]);
int secondnumber=Integer.parseInt(args[2]);
2)to call the desired method on the real object lying on the server,
3)to capture the return value or exception of the call on the server,
4)to marshal this value,
5)to send a package consisting of the value in the marshalled form back to the stub on the
client, machine A.
To generate stubs and skeletons, you use tool called the RMI compiler,which is invoked
from the command line, as shown here
rmic ServerImpl
The Java SDK provides a program called rmiregistry, which executes on the server
machine. It maps names to object references.Then start the RMI Registry from command
line , as shown here.
start rmiregistry
When this command returns, you should see that a new window has been created.
You need to leave this window open until you are done experimenting with RMI
example.
Step Five:
The server code is started from the command line , as shown here
java Server
Step Six:
Client program requires three arguments :the name or IP address of the server
machine and two numbers that are to be summed together.
1.The RMI Server creates an instance of the 'Server Object' which extends
UnicastRemoteObject
3.The server registers the server object with the registry. This operation actually hands
the registry (Note: The RMI Registry is an RMI server itself) the client-side "stub" for
that server object. This stub contains the information needed to "call back" to the server
when it is invoked (such as the hostname/port of the server listening socket).
4.A client obtains this stub by calling the registry, which hands it the stub directly. (This
is also where the "codebase" comes in: If the server specified a "codebase" to use for
clients to obtain the classfile for the stub, this will be passed along to the client via the
registry. The client can then use the codebase to resolve the stub class - that is, to load the
stub classfile itself). That is all that the RMIRegistry really does: It holds onto remote
object stubs which it can hand off to clients when requested.
5.When the client issues a remote method invocation to the server, the stub class creates a
"RemoteCall" which basically
(a) opens a socket to the server on the port specified in the stub itself, and
(b) Sends the RMI header information as described in the RMI spec.
6.The stub class marshalls the arguments over the connection by using methods on
RemoteCall to obtain the output stream which basically returns a subclass of
ObjectOutputStream which knows how to deal with passing objects which extend
java.rmi.Remote, which serializes Java objects over the socket
7.The stub class calls RemoteCall.executeCall which causes the RMI to happen.
8.On the server side, when a client connects to the server socket, a new thread is forked to
deal with the incoming call. The original thread can continue listening to the original
socket so that additional calls from other clients can be made.
9.The server reads the header information and creates a RemoteCall of its own to deal
with unmarshalling the RMI arguments from the socket.
10.The server calls the "dispatch" method of the skeleton class (the server-side "stub"
generated by rmic), which calls the appropriate method on the object and pushes the
result back down the wire (using the same 'RemoteCall' interface which the client used to
marshall the arguments). If the server object threw an exception then the server catches
this and marshalls that down the wire instead of the return value.
11.Back on the client side, the return value of the RMI is unmarshalled (using the
RemoteCall created in step 5 above) and returned from the stub back to the client code
itself. If an exception was thrown from the server that's unmarshalled and re-thrown from
the stub.
Reference : 1.http://java.sun.com/j2se/1.5.0/docs/guide/rmi/index.html
2.The Complete Reference JAVA 2 –Herbert Schildt
Experiment No 4
Aim: Synchronize clocks on hosts connected to the network with NTP server.
Synchronization of Logical clocks using Lamports algorithm.
Theory:
Most people assume that computer clocks in servers, workstations and network
devices are inherently accurate. This is incorrect. Most of these clocks are set by hand to
within a minute or two of actual time and are rarely checked after that. Many of these
clocks are maintained by a battery-backed, clock-calendar device that may drift as much
as a second per day. Having any sort of meaningful time synchronization is impossible if
such clocks are allowed to run on their own.
NTP:
NTP stands for Network Time Protocol, and it is an Internet protocol used to
synchronize the clocks of computers to some time reference. NTP is an Internet standard
protocol originally developed by Professor David L. Mills at the University of Delaware.
NTP is widely used to synchronize the time on computers on the internet. NTP
provides the ability to access time services, organize the time synchronization subnet and
adjust the local clock in each participating subnet computer. Typically, NTP provides
accuracies of between 1 and 50 milliseconds depending on the time source and network
paths. Network Time Protocol can be utilized to synchronize the time on computers
across a network. A time server is utilized to obtain the correct time from a time source
and adjust the local time in each participating computer. The time source used by the time
server is extremely important as this forms the basis of all time updates across the
network.
SNTP:
SNTP (Simple Network Time Protocol) is basically NTP, but lacks some internal
algorithms that are not needed for all types of servers.
• NTP needs a reference clock that defines the true time. All clocks are set towards
that true time. (It will not just make all systems agree on some time, but will make
them agree upon the true time as defined by some standard.)
• NTP is a fault-tolerant protocol that will automatically select the best of several
available time sources to synchronize to. Multiple candidates can be combined to
minimize the accumulated error. Temporarily or permanently insane time sources
will be detected and avoided.
• Having available several time sources, NTP can select the best candidates to build
its estimate of the current time. The protocol is highly accurate, using a resolution
of less than a nanosecond (about 2^-32 seconds).
• For formal reasons NTP will also maintain estimates for the accuracy of the local
time
NTP is readily available for most popular UNIX operating systems. Among these
are: AIX, HP-UX, Irix, Linux, SCO UNIX, OSF/1, Solaris, System V.4.
There’s also a supported implementation for VMS: "UCX (the VMS TCP stack)
has a full implementation of NTP built-in. As of v5 it's pretty much vanilla NTP, prior to
that the command syntax and control file formats were proprietary."
If you are worried with compatibility issues, older version clients can generally
talk to newer version servers automatically, but the other direction requires manual
interference.
There is an implementation of an NTP client and server available for free. The software is
available as C source and it runs on most UNIX compatible operating systems. The
software consists of the following components:
ntpd
A daemon process that is both, client and server.
ntpdate
A utility to set the time once, similar to the popular rdate command.
ntpq, ntpdc
Monitoring and control programs that communicate via UDP with ntpd.
ntptrace
A utility to back-trace the current system time, starting from the local server.
Microsoft Windows
• Select a NTP server, using net time /setsntp:ntp-server. However, only the domain
controller that holds the PDC FSMO (Primary Domain Controller Flexible Single
Master Operation) role can query an external time source to set the time.
• Start the W32Time service with net start W32Time. You can also set the start
option of the Windows Time Synchronization Service W32Time to automatic, so
the service will start when Windows/2000/2003 starts.
Problem for NTP to go out on the Internet to get time from a public Internet Time
Server. But, this approach is prone to problems because:
• The source of time is beyond your firewall. This means there must be a "hole"
left open in the firewall (UDP port 123) to allow packets containing the time
information through.
• Time accuracy degrades when using an Internet Time Server because of
asymmetrical latency (delays between when the time packets leave the time
source and when they arrive at your network).
• External agencies (e.g. universities) who provide Public Domain Time Servers are
not obliged to continue service or guarantee availability and accuracy.
Theory:
It is sufficient that all machines agree on the same time. It is not essential that this
time also aggress with the real time as announced on the radio every house. Such clocks
are considered as logical clocks.
To synchronize logical clocks, Lamport defined a relation called happened
before. The expression a->b is read “a happens before b” and means that all processes
agree that first event first event a occurs, then afterward, event b occurs. The happens
before relation can be observed directly in two situations.
1. If a and b are events in the same process, and a occurs before b , the a->b is
true.
2. If a is the event of a message being sent by one process, and b is the event of
the message being received by another process, then a->b is also true. A
message cannot be received before it is sent or event at the same time it is
sent, since it takes a finite, nonzero amount of time to arrive.
Theory:
Many distributed algorithms require one process to act as coordinator, initiator, or
to perform some other special role. In the centralized mutual exclusion algorithm, one
process is elected as the coordinator. For instance, the process running on the machine
with the highest network address might be selected. Whenever a process wants to enter a
critical region, it sends a request message to the coordinator stating which critical region
it wants to enter and asking for permission. If no other process is currently in that region,
the coordinator sends back a reply granting permission.
If a process that was previously down comes back up, it holds an election. If it
happens to be the highest-numbered process currently running, it will win the election
and will take over the coordinator's job. Thus the biggest guy in town always wins, hence
the name "Bully Algorithm".
Ring Election Algorithm
This election algorithm is based on the use of a ring. We assume that the
processes are physically or logically ordered, so that each process knows who its
successor is. When any process notices that the coordinator is not functioning, it builds an
ELECTION message containing its own process number and sends the message to its
successor. If the successor is down, the sender skips over the successor and goes to the
next number along the ring, or the one after that, until a running process is located. At
each step, the sender adds its own process number to the list in the message.
Eventually, the message gets back to the process that started it all. That process
recognizes this event when it receives an incoming message containing its own process
number. At that point, the message type is changed to COORDINATOR and circulated
once again, this time to inform everyone else who the coordinator is (designated by the
list member with the highest number) and who the members of the new ring are. When
this message has circulated once, it is removed and everyone goes back to work.
Theory: The Network File System (NFS) is a client/server application that lets a
computer user view and optionally store and update file on a remote computer as though
they were on the user's own computer. So CODA is advanced file system.
So experiment should include basic architecture of NFS, introduction, features,
architecture of CODA.
References: 1. http://www.coda.cs.cmu.edu
2.Distributed Systems Principles and Paradigms – Low Price Edition
Andrew S. Tanenbaum
Experiment NO. 7