Introduction To Networking and RMI

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 67

Introduction to Networking and RMI

• The term network programming refers to


writing programs that execute across multiple
devices (computers), in which the devices are
all connected to each other using a network.
Network Programming introduction
OSI Model Layres
Layer Data unit Purpose

Softwares run at this level (browsers, instant


7. Application Data messaging softwares, e-mail softwares and so
forth).

Encryption and decryption of data take place in


6. Presentation Data this layer which is also known as the syntax
layer.

This layer allows a process to establish, manage


5. Session Data and terminate a dialog with a remote process
across the network.

This layer provides end-to-end communication.


4. Transport Segment It divides the data into smaller pieces and offers
flow control and optionally error recovery.
Layer Data unit Purpose

This layer provides routing and


logical addressing technologies
3. Network Packet
that allow packets to be transfered
across the network.

This layer offers physical


addressing, transmission error
2. Data link Frame
detection, synchronization and
flow control.

This layer transfers binary digits


1. Physical Bit through copper cables, fiber
optical cables or radio links.
IP protocol
• IP lower-level connection less protocol ,
means there is no continuing connection
between two system.
Protocols in java.net package
• The java.net package provides support for the
two common network protocols −
• TCP − TCP stands for Transmission Control
Protocol, which allows for reliable
communication between two applications. TCP is
typically used over the Internet Protocol, which is
referred to as TCP/IP.
• UDP − UDP stands for User Datagram Protocol, a
connection-less protocol that allows for packets
of data to be transmitted between applications.
Java InternetAddress Class
Socket Programming in Java
• Socket programming is a way of connecting
two nodes on a network to communicate with
each other.
• Socket identifies end point in a network.
• Java Socket programming can be connection-
oriented or connection-less.
• Socket and ServerSocket classes are used for
connection-oriented socket programming
• DatagramSocket and DatagramPacket classes
are used for connection-less socket
programming.
Through Network
• The java.net.Socket class represents a socket,
• The java.net.ServerSocket class provides a
mechanism for the server program to listen
for clients and establish connections with
them.
Important Methods of Socket Class
Important Methods of Server Socket
Class
Range Of port number
• Port numbers 0-1023 – Well known ports. These are
allocated to server services by the Internet Assigned
Numbers Authority (IANA
• Port number 80 : Webservices
• Port number 20,21: FTP(Data/control)
• Port number 23 :telnet
• Ports 1024-49151- Registered Port -These can be
registered for services with the IANA and should be
treated as semi-reserved. User written programs
should not use these ports.
• Ports 49152-65535– These are used by client
programs and you are free to use these in client
programs. When a Web browser connects to a web
server the browser will allocate itself a port in this
range. Also known as ephemeral ports.
Output
Read Write Bothside
Server Side
Multisocket
• Java Socket API support network
communication in 3 ways
• 1) One to one (Client -Server)
• 2)One to all (broadcast)
• 3)One to many (Multicast)
Multicast Sockets in java
• MulticastSocket class in Java.
• This class is used for sending and
receiving multicast IP packets.
• It extends DatagramSocket class and provides
additional functionality for joining groups.
• A message sent to the group IP address will be
received by all the clients who have joined the
group.
• for sending packets to the group, datagram
socket doesn’t have to join the group but for
receiving the packets addressed to the group,
it must join the group.
Constructors
Methods
8.allows for specification of network interface on which to leave the group.
• For multicasting we have to use some specific
address range .

• 224.0.0.0 to 239.255.255.255
• Some address are reserved address like :
224.0.0.0
Example Multicast socket Server
Multicast Socket client
Output
• In order to use multiple clients, just create a
new Java project and copy-paste the code of
the client, but change the output to Socket
2 instead of Socket 1.
• You will see that when the server runs, the
messages will be sent to both clients, and
both clients will print the same results (except
for the socket number part).
Output for multiple client
Basic RPC Model
RPC
• RPC is a protocol that one program can used
to request a service from a program located in
another computer on a network without
having to understand the network details.
• A procedure call is also called as function call
or subroutine call.
• RPC uses the client server model.
RPC Operations
Elements of RPC
• Client
• Client Stub
• RPC runtime (RPC communication package )
• Server Stub
• Server

• Stub : piece of code used in converting


parameters.
• Client :
– It is user process which initiate RPC.
– the client makes the perfectly normal call that
invoke a corresponding procedure in the client
stub .
• Client Stub:
– on receipt of the request it packs the
requirement into a message and ask the RPC
runtime to send it .
– On receipt of result it unpacks the result and
passes it to client
• RPC runtime :
– It handles the transmission of message between client
and server.
• Server stub:
– It unpack the call request and make a perfectly normal
call to invoke the appropriate procedure in server.
– on receipt of the result it packs the requirement into a
message and ask the RPC runtime to send it .
• Server :
– execute an appropriate procedure and returns the result
from a server stub.
RPC Execution
RPC Implementation
• RPC Messages :
– call/request
– reply
RMI
• RMI stands for Remote Method Invocation.
• It is a mechanism that allows an object
residing in one system (JVM) to access/invoke
an object running on another JVM.
• RMI is used to build distributed applications; it
provides remote communication between
Java programs.
• It is provided in the package java.rmi.
• It is a API that allows an object to invoke
another method that exists in separate
address space.
RMI Working
• Two intermediate objects are used for the
communication b/w client and server .
Working process
• Inside the server program, a remote object is
created and reference of that object is made
available for the client (using the registry).
• The client program requests the remote
objects on the server and tries to invoke its
methods.
Architecture of RMI Application
Working of RMI
• The following points summarize how an RMI
application works −
• When the client makes a call to the remote object, it is
received by the stub which eventually passes this
request to the RRL.
• When the client-side RRL receives the request, it
invokes a method called invoke() of the
object remoteRef. It passes the request to the RRL on
the server side.
• The RRL on the server side passes the request to the
Skeleton (proxy on the server) which finally invokes the
required object on the server.
• The result is passed all the way back to the client.
Marshalling and Unmarshalling
• In case of primitive type, the parameters are
put together and a header is attached to it. In
case the parameters are objects, then they are
serialized. This process is known
as marshalling.
• At the server side, the packed parameters are
unbundled and then the required method is
invoked. This process is known
as unmarshalling.
RMI Registry
• RMI registry is a namespace on which all server
cxnobjects are placed.
• Each time the server creates an object, it
registers this object with the RMIregistry
(using bind() or reBind() methods). These are
registered using a unique name known as bind
name.
• To invoke a remote object, the client needs a
reference of that object. At that time, the client
fetches the object from the registry using its bind
name (using lookup() method).
Goals of RMI
Following are the goals of RMI −
• To minimize the complexity of the application.
• To preserve type safety.
• Distributed garbage collection.
• Minimize the difference between working
with local and remote objects.

You might also like