Advanced Java Class Network Programming Network Protocols Overview - - PowerPoint PPT Presentation

advanced java class
SMART_READER_LITE
LIVE PREVIEW

Advanced Java Class Network Programming Network Protocols Overview - - PowerPoint PPT Presentation

Advanced Java Class Network Programming Network Protocols Overview Levels of Abstraction HTTP protocol: spoken by Web Servers and Web Clients TCP/IP: identifies all computers on the internet, facilitates sending info between them


slide-1
SLIDE 1

Advanced Java Class

Network Programming

slide-2
SLIDE 2

Network Protocols Overview

  • Levels of Abstraction

– HTTP protocol: spoken by Web Servers and Web Clients – TCP/IP: identifies all computers on the internet, facilitates sending info between them – Hardware: gives very local information on how to move packets from one box to the next on a particular type of network

slide-3
SLIDE 3

Network Programming in Java

  • Package java.net
  • Sockets

– TCP/IP – Datagram

  • URLConnections
  • RMI, JNDI, and CORBA
slide-4
SLIDE 4

Sockets vs. URLConnections

  • Sockets

– Supplies hardware and TCP/IP layers – You can talk any protocol over this connection

  • URLConnection subclasses are specific

to a particular protocol

– HttpURLConnection, for example

slide-5
SLIDE 5

URLConnections

  • Returned by url.getConnection();
  • Provides an InputStream to read the content of

that url

  • Provides an OutputStream to write to the url (i.e.

for POST request – parameters are in body, not in the query string)

  • You can query a URLConnection for meta-data,

such as date last modified

  • Good for one exchange – for more, use sockets
  • Safe connections
slide-6
SLIDE 6

Sockets

  • TCP/IP Socket

– Maintains connection for sustained dialog – More overhead – Reliable – no packet lost without notification

  • Datagram Socket

– Does not maintain a connection – Very fast – almost no overhead – Unreliable – can lose packets unknowningly

slide-7
SLIDE 7

Using a TCP/IP Socket

  • Server-side:

– Make a new SocketServer, & call accept on it. – This tells it to wait for a client socket to try to connect, then returns a Socket that is connected to the client socket – You can then ask that connected Socket for an InputStream and an Output Stream, which you can use to send/receive information from the

  • ther side of the connection
slide-8
SLIDE 8

Using a TCP/IP Socket

  • Client-side

– Just make a new Socket with the host and the matching port # – Get the InputStream and/or the OutputStream – Communicate to your heart’s content.

slide-9
SLIDE 9

Using a Datagram Socket

  • To receive a packet (server-side)
  • Call new DatagramSocket( port );
  • create a DatagramPacket with an empty byte[]

buffer

  • call receive on the DatagramSocket with the

packet as an argument.

  • The received information will be stored in the

DatagramPacket you created.

  • You can then use DatagramPacket’s access

methods to get the data transferred.

slide-10
SLIDE 10

Using a Datagram Socket

  • To send a packet (client-side)
  • Call new DatagramSocket()
  • create a DatagramPacket complete with data

(as a byte[]), length, address, and port.

  • Call send on your DatagramSocket with the

packet as an argument.

slide-11
SLIDE 11

RMI – Remote Method Invocation

  • Call methods on objects that have been

instantiated on another machine’s JVM.

  • Example use case:

– Software allows student to do homework at home (with sporadic connection) or on campus. – Both servers can show the student the homework – Only central server can grade homework – So local server can show the student the homework, collect answers, then send it to the remote server for grading.

  • Class: can you think of any other use cases?
slide-12
SLIDE 12
slide-13
SLIDE 13

Remote Interface

  • Defines the methods that can be used by

client-side code

  • All methods must declare throws

java.rmi.RemoteException

  • Extends java.rmi.Remote
slide-14
SLIDE 14

Remote Class Implementation

  • Implements the interface
  • Extends java.rmi.server.RemoteObject,

usually by extending its subclass java.rmi.server.UnicastRemoteObject

slide-15
SLIDE 15

_Stub and _Skel

  • generated by rmic, a command-line

compiler

  • rmic examples.network.StudentImpl
  • (The stub files will automatically be

downloaded from the server to the client as needed.)

slide-16
SLIDE 16

RMI Server Class

  • Checks that SercurityManager is set
  • Instantiates and registers RMI Objects with

java.rmi.Naming:

– Naming.rebind(object_label, RMI_ObjectImpl);

  • The program rmiregistry must be running.
  • java -Djava.rmi.server.codebase=

file:CODE_BASE_PATH SERVER_CLASS_NAME

slide-17
SLIDE 17

Client Class

  • Asks naming service for object (stub) and casts to

the interface type

– Naming.lookup(“//” + HOST_HAME + “/” +

  • bject_label)
  • Calls methods on that stub as if it were really the
  • bject

– Assessor methods – Setter methods – Any methods at all that are in the interface

  • Must handle (catch/throw) RemoteExceptions.
slide-18
SLIDE 18

Java Naming and Directory Interface (JNDI)

  • RMI Registry is nice, but not very

scalable.

  • JNDI provides for the implementation of

scalable naming and directory services

  • Read Chapter 14 for more information if

you’re interested

slide-19
SLIDE 19

Common Object Request Broker Architecture (CORBA)

  • Like RMI, only for mixing Objects from multiple

programming languages

  • Used to be that RMI and CORBA couldn’t communicate

– CORBA uses Internet Inter-ORB Protocol (IIOP) – RMI uses Java Remote Method Protocol (JRMP)

  • As of Java 1.3, support for RMI-IIOP interoperability

exists – a.k.a. RMI API – Especially useful with Enterprise JavaBeans (EJBs) – CORBA interfaces are defined with Interface Definition Language (IDL), but you can use an SDK tool called idlj to create the interfaces, stub, and skeleton classes than correspond to the IDL.

slide-20
SLIDE 20

Small Group Work

  • Work in groups of three to six people
  • Write this short example of RMI

– Card Class modifications – Deck class modifications – Deck Interface – Player Class Modifications – What commands must be run, and in what

  • rder?