An Introduction to Sockets, RPC and RMI - PowerPoint PPT Presentation

1 / 63
About This Presentation
Title:

An Introduction to Sockets, RPC and RMI

Description:

Sockets, RPC and RMI Edward.Karavakis_at_brunel.ac.uk. 2. Transport Control Protocol (TCP) ... Sockets, RPC and RMI Edward.Karavakis_at_brunel.ac.uk. 18. TCP Server ... – PowerPoint PPT presentation

Number of Views:365
Avg rating:3.0/5.0
Slides: 64
Provided by: Edwa172
Category:

less

Transcript and Presenter's Notes

Title: An Introduction to Sockets, RPC and RMI


1
An Introduction to Sockets, RPC and RMI
  • Edward Karavakis

2
Outline
  • Transport Control Protocol (TCP)
  • User Datagram Protocol (UDP)
  • Network Sockets
  • External Data Representation (XDR)
  • Java Object Serialisation
  • Stub and Skeleton
  • Remote Procedure Call (RPC)
  • Java Remote Method Invocation (RMI)

3
TCP
  • TCP (Transport Control Protocol) is a
    connection-oriented protocol that provides a
    reliable flow of data between two computers.

Application (http, ftp, telnet, ssh)
Transport (TCP)
Network (IP)
Data Link
Physical
4
UDP
  • UDP (User Datagram Protocol) is a protocol that
    sends independent packets of data, called
    datagrams, from one computer to another and
    theres no guarantee about arrival.
  • there is no handshaking
  • transmitted data may be received out of order,
    lost, or duplicated.

Application (dns, snmp, tftp)
Transport (UDP)
Network (IP)
Data Link
Physical
5
Network Ports
  • An IP address can be used to uniquely identify a
    network computer.
  • A Port is used to uniquely identify a network
    process.

TCP or UDP
TCP or UDP
Port
App
Port
Port
App
Port
Port
App
Port
Port
App
Port
Client
Server
6
Network Ports (cont.)
  • Each computer has 65,536 TCP ports and 65,536 UDP
    ports.
  • TCP Ports are separate from the UDP ports.
  • A socket can be bound to TCP port 20 at the same
    time as a datagram socket is bound to UDP port
    20.
  • Port numbers below 1024 have pre-defined
    meanings, e.g.
  • ftp 21/tcp
  • ssh 22/tcp
  • telnet 23/tcp
  • smtp 25/tcp

7
Network Sockets
  • A Socket is a host-local, application created/
    released, OS-controlled interface (a door) into
    which application process can both send and
    receive messages to/from another (remote or
    local) application process
  • Socket API
  • First introduced in BSD4.1 UNIX, 1981
  • Sockets are explicitly managed by application
  • Client/ Server model
  • Two types of transport service via socket API
  • Unreliable, block-oriented (UDP)
  • Reliable, stream-oriented (TCP)

8
Network Sockets (cont.)
  • A Socket is an end-point of a bidirectional
    communication link that is mapped to a computer
    process communicating on an Internet
    Protocol-based network, such as the Internet.
  • A Socket is an interface between an application
    process or thread (child process) and the TCP/ IP
    protocol stack provided by the operating system.
  • A Socket is identified by the operating system
    as a unique combination of the following
  • The combination of an IP address and a port
    number is referred to as a socket.

Protocol (TCP, UDP)
Local IP Address (Source Address)
Local Port Number (Source Port)
Remote IP Address (Destination Address)
Remote Port Number (Destination Port)
9
TCP Socket
  • TCP provides reliable, in-order delivery of a
    stream of bytes.
  • TCP divides messages into smaller pieces, called
    segments, to be sent to the destination address.
  • It is also responsible for controlling the size
    and rate at which messages are exchanged between
    the server and the client.

10
TCP Connection Establishment
  • To establish a connection, TCP uses a three-way
    handshake. Before a client attempts to connect
    with a server, the server must first bind to a
    port to open it up for connections this is
    called a passive open. Once the passive open is
    established, a client may initiate an active
    open.

11
TCP Connection Establishment
  • To establish a connection, the three-way
    handshake occurs
  • The active open is performed by the client
    sending a SYN to the server.
  • In response, the server replies with a SYN-ACK.
  • Finally the client sends an ACK back to the
    server.
  • At this point, both the client and server have
    received an acknowledgment of the connection.

12
TCP Connection Establishment example
  • 1. The initiating host (client) sends a
    synchronisation packet (SYN flag set to 1) to
    initiate a connection. It sets the packet's
    sequence number to a random value x.
  • 2. The other host receives the packet, records
    the sequence number x from the client, and
    replies with an acknowledgment and
    synchronisation (SYN-ACK). The Acknowledgment is
    a 32-bit field in TCP segment header and it
    contains the next sequence number that this host
    is expecting to receive (x 1). The host also
    initiates a return session. This includes a TCP
    segment with its own initial Sequence Number of
    value y.
  • 3. The initiating host responds with the next
    Sequence Number (x 1) and a simple
    Acknowledgment Number value of y 1, which is
    the Sequence Number value of the other host 1.

13
UDP Socket
  • Using UDP, programmes on networked computers can
    send short messages known as datagrams to one
    another.
  • UDP does not guarantee reliability or ordering in
    the way that TCP does.
  • Datagrams may arrive out of order, appear
    duplicated, or go missing without notice.
  • Avoiding the overhead of checking whether every
    packet actually arrived makes UDP faster and more
    efficient, for applications that do not need
    guaranteed delivery.
  • Time-sensitive applications often use UDP because
    dropped packets are preferable to delayed
    packets.
  • Unlike TCP, UDP is compatible with broadcasting
    (sending to all on local network) and
    multicasting (send to all subscribers).

14
Java API for Internet addresses
  • Class InetAddress
  • uses DNS (Domain Name System)
  • InetAddress aC InetAddress.getByName(spoiltspac
    e.co.uk)
  • throws UnknownHostException
  • encapsulates detail of IP address (4 bytes
    for IPv4 and 16 bytes for IPv6)

15
TCP Socket in Java
  • java.net.Socket
  • Used to create a socket connection for
    communication
  • public Socket(String host, int port) throws
    UnknownHostException, IOException
  • public Socket(InetAddress address, int port)
    throws IOException
  • The java.net.ServerSocket class represents a
    server socket
  • A ServerSocket object is constructed on a
    particular local port
  • Then it calls accept() to listen for incoming
    connections
  • Then accept() returns a java.net.Socket object
    that performs the actual communication with the
    client
  • public ServerSocket(int port) throws IOException

16
TCP Client/ Server Example
  • TCP Client
  • makes connection, sends a request and receives
    a reply
  • TCP Server
  • makes a connection for each client and then
    echoes the clients request

17
TCP Client Example
18
TCP Server Example
19
TCP Server Example (cont.)
20
UDP Socket in Java
  • java.net.DatagramSocket
  • public DatagramSocket() creates a socket for
    sending data
  • public DatagramSocket(int port) creates a socket
    for receiving data
  • java.net.DatagramPacket for sending or receiving
    data
  • public DatagramPacket(byte data, int length)
    packaging data for receiving
  • public DatagramPacket(byte data, int length,
    InetAddress iaddr, int iport) packaging data for
    sending

21
UDP Client/ Server Example
  • UDP Client sends a message and gets a reply
  • UDP Server repeatedly receives a request and
    sends it back to the client

22
UDP Client Example
23
UDP Server Example
24
Datagram Communication Java API
25
Datagram Communication Java API
26
Datagram Communication Java API
27
Datagram Communication Java API
28
Datagram Communication Java API
29
Datagram Communication Java API
30
Datagram Communication Java API
31
Datagram Communication Java API
32
Stream Communication Java API
33
Stream Communication Java API
34
Stream Communication Java API
35
Stream Communication Java API
36
Before Studying RPC and RMI
  • External Data Representation
  • Java Object Serialisation
  • Stubs and Skeletons

37
Data Representation in RPC
  • eXternal Data Representation (XDR) standard is a
    set of routines that enable programmers to
    describe arbitrary data structures in a
    system-independent way.
  • Does not depend on machine languages,
    manufacturers, operating systems or
    architectures.
  • RPC uses XDR to establish uniform representations
    for data types in order to transfer message data
    between computers.

38
External Data Representation
  • XDR allows data to be wrapped in an architecture
    independent manner so data can be transferred
    between heterogeneous computer systems.
  • Converting from the local representation to XDR
    is called encoding.
  • Converting from XDR to the local representation
    is called decoding.
  • XDR is implemented as a software library of
    functions that is portable between different
    operating systems and is also independent of the
    transport layer.

39
External Data Representation (cont.)
  • XDR is Symmetric Data Conversion both client and
    server convert to / from some standard
    representation.
  • XDR libraries are based on a stream paradigm.
  • The process of converting local (host) data to
    XDR also puts the data in the XDR stream.
  • When extracting an item from an XDR stream,
    conversion is done to the local (host)
    representation.
  • XDR Streams can be attached to a file, pipe,
    socket or memory.
  • Individual data items are added to the stream one
    at a time.
  • Individual data items are removed from the stream
    one at a time.

40
Java Object Serialisation
41
Java Object Serialisation
42
Java Object Serialisation
43
Stub and Skeleton
  • The Stub implements the same Remote interface
    that the server implements.
  • Hence, the client can call the same functions/
    methods in the Stub that it wants to call in the
    server.
  • The functions/ methods in the stub are filled
    with network-related code and not the actual
    implementation of the required function.

Network
Client
Stub
Skeleton
Server
Sockets, RPC and RMI Edward.Karavakis_at_brunel.ac.uk
43
44
RPC Introduction
  • RPC (Remote Procedure Call) is a standard
    middleware technology that enables cross-network
    function calls.
  • Allows a client to call a remote procedure as if
    the procedure was running local.
  • Hides the underlying networking technologies.
  • Masks any differences in data representations.
  • A standard mechanism for distributing processing
    at a high level.
  • Easier to use and more powerful than sockets.

45
RPC Fundamentals
  • RPC allows a program to cause a function to
    execute in another address space (commonly on
    another computer on a shared network) without the
    programmer explicitly coding the details for this
    remote interaction.
  • The programmer would write essentially the same
    code whether the function is local or remote.

46
A function call of C programming lang.
int main() int a 10 int b 20 result
add_numbers(a,b)
int add_numbers (int a, int b) return ab
3
2
1
4
Process A
47
A Remote Procedure Call
int main() int a 10 int b 20 result
add_numbers(a,b)
int add_numbers (int a, int b) return ab
3
2
1
4
Process A
Process B
48
RPC Development Process
  • Write an IDL (Interface Definition Language)
    description of the function.
  • arguments, return value and data types
  • Compile the IDL description into
    language-specific binding.
  • Write the code for both sides of the interaction.

49
RPC IDL
  • A declarative language that defines remote
    procedures
  • Syntax similar to C

Client Stub
Interface definition in IDL
IDL Compiler
Header File
Server Stub
50
RPC Application Development
C Compiler
Client Executable
Client Source
Client Stub
Interface definition in IDL
IDL Compiler
RPC Runtime Library
Header File
Server Stub
C Compiler
Server Executable
Server Source
51
How RPC Works
  • RPC Control Flow
  • The client application invokes a function call
  • The client stub encodes/ marshalls and transmits
    the call as a message
  • The server stub (skeleton) receives the message
    and decodes/ unmarshalls the call
  • The server stub call the specified function
    implementation
  • Stubs
  • Generated by an RPC compiler to call an RPC
    run-time library that implements the RPC protocol

Programme
Remote Procedure
1
5
9
RPC Interface
RPC Interface
Client Stub
Server Stub
6
8
2
4
RPC Runtime
RPC Runtime
7a
7b
3a
3b
Network
Client
Server
52
Data Flow in RPC
RPC IDL Interface
RPC Compiler
Client
Server
Client Stub
Server Stub
RPC Runtime
TCP/IP
Code implemented by programmer
Code supplied by RPC package
Code generated by RPC Compiler
53
RPC Example
  • Write the interface definition file (IDL) which
    declares RPC functions and related types.
  • Run the RPC compiler, i.e. PowerRPC, to generate
    client/ server stubs from the IDL file.
  • Code the server implementation according to the
    RPC functions declared in the interface.
  • Code the client that calls these RPC functions.

54
A simple RPC Example A sample IDL
  • Compile the IDL powerRPC quote.idl
  • Five files will be generated
  • quote.h - Header to be included by the client and
    server.
  • quote_svc.c - Server stub, it calls the server
    implementation of getQuote() function.
  • quote_cln.c Client stub, it defines the
    getQuote() function for the client, this function
    is the interface to the servers getQuote()
    function.
  • quote_xdr.c XDR routines used by both the
    client and server to encode and decode the
    arguments and return values.
  • quote.mak A makefile template.
  • Typedef struct
  • char Test8
  • int a
  • int b
  • double c
  • strQuote
  • interface quote
  • int getQuote ()
  • Then, we write the Server, in our example
    quote_servimp.c, compile it and run it
  • edward_at_spoiltspace.co.uk cc -o quoteServer
    quote_svc.c quote_xdr.c quote_servimp.c -lpwrpc
  • edward_at_spoiltspace.co.uk ./quoteServer
  • Then, we write the Client, in our example
    quote_client.c, compile it and run it
  • edward_at_spoiltspace.co.uk cc -o quoteClient
    quote_cln.c quote_xdr.c quote_client.c -lpwrpc
  • edward_at_spoiltspace.co.uk ./quoteClient

55
Java RMI Introduction
  • Remote Method Invocation (RMI) is an RPC-like
    system for the Java language.
  • Objects provide a natural granularity for the
    binding of functions
  • Allows a programme to hold a reference to an
    object on a remote system and to call the methods
    of that object.
  • Client Server architecture.
  • Server hosts the object.
  • Client hosts a small stub that accesses the
    object on the Server.
  • Differences with RPC
  • Java RMI is object oriented, RPC is not.
  • The client and the server code can be implemented
    with different languages and different operating
    systems using RPC.
  • The client and server code can run on different
    platforms using RMI but they have to be developed
    in Java.

56
RMI Architecture
  • Stub and Skeleton Layer
  • Intercepts method calls mode by the client to the
    interface reference variable and redirects these
    calls to a remote RMI service.
  • Remote Reference Layer
  • Intercepts and manages references made from
    clients to the remote service objects.
  • Transport Layer
  • TCP/ IP connections. UDP is not used.
  • Provides basic connectivity, as well as some
    firewall penetration techniques.

Client Programme
Server Programme
Stubs Skeletons
Stubs Skeletons
RMI System
Remote Reference Layer
Remote Reference Layer
Transport Layer
57
RMI Registry
  • How can the client know about the object details
    of the server? Using the RMI Registry
    java.rmi.Naming .
  • The Registry is like a service that keeps pairs
    in a hash table, i.e. public_name, Stub_object
  • We create a Stub Object on the server and
    register it with the RMI Registry so that the
    client can get the Stub Object from the Registry.

58
Naming Remote Objects
  • Clients find remote services by using a naming or
    directory service.
  • RMI can use many different directory services,
    such as the Java Naming and Directory Interface
    (JNDI).
  • RMI also includes the RMI Registry
  • Runs on each machine that hosts remote service
    objects and accepts queries and services on port
    1099.
  • RMI Registry is not required you may skip it
  • RMI URL
  • rmi//lthostgtltportgt/ltservice_namegt

59
RMI Example Remote Interface
  • We want to make the following method available to
    remote clients

Calc.java
public interface Calc extends Remote public
int add (int i, int j) throws RemoteException
60
RMI Example Server
  • The Remote class itself could have the following
    characteristics
  • Must implement a Remote interface.
  • Should extend the java.rmi.server.UnicastRemoteObj
    ect class.
  • Can have methods that are not in its Remote
    interface. These methods can only be invoked
    locally.

import java.rmi.server.UnicastRemoteObject import
java.rmi.RemoteException public class
CalcServer extends UnicastRemoteObject implements
Calc public CalcServer() throws
RemoteException super() public int add(int i,
int j) throws RemoteException return
ij public static void main(String args)
throws RemoteException CalcServer c new
CalcServer() Naming.rebind (rmi//localhost10
99/calc.c,c)
CalcServer.java
  • The RMI Naming Service registers a reference to
    the remote object (calc)
  • The reference is a network access point to the
    object.

61
RMI Example Client
  • RMI Naming Service returns a reference to the
    remote object, in our example calc, which is a
    network access point to the object.
  • This remote reference is used by the client-side
    Stub to invoke the remote method.

CalcClient.java
import java.rmi.Naming public class
CalcClient public static void main(String
args) throws Exception Calc
c(Calc)Naming.lookup(rmi//remotehost1099/calc
) System.out.println(c.add(5,3)
62
RMI Example Compiling and testing the programme
  • All the Remote interfaces and classes should be
    compiled using javac
  • edward_at_spoiltspace.co.uk javac .java
  • Use the RMI compiler to generate the stub
  • edward_at_spoiltspace.co.uk rmic CalcServer
  • Start the Object Registry
  • edward_at_spoiltspace.co.uk rmiregistry
  • Start Server
  • edward_at_spoiltspace.co.uk java CalcServer
  • Start Client
  • edward_at_spoiltspace.co.uk java CalcClient
  • What is the output result? How?

63
Data Flow in RMI
RMI Interface in Java
Naming Service
Naming Service
Class that implements a RMI Interface
Object
RMI Compiler (rmic)
Client
Server
JRMP
Client Stub
Server Skeleton
Java RMI Nameserver (rmiregistry)
TCP/IP
Code implemented by programmer
Code supplied by RMI package
Code generated by RMI Compiler
Write a Comment
User Comments (0)
About PowerShow.com