Advanced Java Class Network Programming Network Protocols Overview

  • Slides: 20
Download presentation
Advanced Java Class Network Programming

Advanced Java Class Network Programming

Network Protocols Overview • Levels of Abstraction – HTTP protocol: spoken by Web Servers

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

Network Programming in Java • Package java. net • Sockets – TCP/IP – Datagram

Network Programming in Java • Package java. net • Sockets – TCP/IP – Datagram • URLConnections • RMI, JNDI, and CORBA

Sockets vs. URLConnections • Sockets – Supplies hardware and TCP/IP layers – You can

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 – Http. URLConnection, for example

URLConnections • Returned by url. get. Connection(); • Provides an Input. Stream to read

URLConnections • Returned by url. get. Connection(); • Provides an Input. Stream to read the content of that url • Provides an Output. Stream 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

Sockets • TCP/IP Socket – Maintains connection for sustained dialog – More overhead –

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

Using a TCP/IP Socket • Server-side: – Make a new Socket. Server, & call

Using a TCP/IP Socket • Server-side: – Make a new Socket. Server, & 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 Input. Stream and an Output Stream, which you can use to send/receive information from the other side of the connection

Using a TCP/IP Socket • Client-side – Just make a new Socket with the

Using a TCP/IP Socket • Client-side – Just make a new Socket with the host and the matching port # – Get the Input. Stream and/or the Output. Stream – Communicate to your heart’s content.

Using a Datagram Socket o To receive a packet (server-side) o Call new Datagram.

Using a Datagram Socket o To receive a packet (server-side) o Call new Datagram. Socket( port ); o create a Datagram. Packet with an empty byte[] buffer o call receive on the Datagram. Socket with the packet as an argument. o The received information will be stored in the Datagram. Packet you created. o You can then use Datagram. Packet’s access methods to get the data transferred.

Using a Datagram Socket o To send a packet (client-side) o Call new Datagram.

Using a Datagram Socket o To send a packet (client-side) o Call new Datagram. Socket() o create a Datagram. Packet complete with data (as a byte[]), length, address, and port. o Call send on your Datagram. Socket with the packet as an argument.

RMI – Remote Method Invocation • • Call methods on objects that have been

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?

Remote Interface • Defines the methods that can be used by client-side code •

Remote Interface • Defines the methods that can be used by client-side code • All methods must declare throws java. rmi. Remote. Exception • Extends java. rmi. Remote

Remote Class Implementation • Implements the interface • Extends java. rmi. server. Remote. Object,

Remote Class Implementation • Implements the interface • Extends java. rmi. server. Remote. Object, usually by extending its subclass java. rmi. server. Unicast. Remote. Object

_Stub and _Skel • generated by rmic, a command-line compiler • rmic examples. network.

_Stub and _Skel • generated by rmic, a command-line compiler • rmic examples. network. Student. Impl • (The stub files will automatically be downloaded from the server to the client as needed. )

RMI Server Class • Checks that Sercurity. Manager is set • Instantiates and registers

RMI Server Class • Checks that Sercurity. Manager is set • Instantiates and registers RMI Objects with java. rmi. Naming: – Naming. rebind(object_label, RMI_Object. Impl); • The program rmiregistry must be running. • java -Djava. rmi. server. codebase= file: CODE_BASE_PATH SERVER_CLASS_NAME

Client Class • Asks naming service for object (stub) and casts to the interface

Client Class • Asks naming service for object (stub) and casts to the interface type – Naming. lookup(“//” + HOST_HAME + “/” + object_label) • Calls methods on that stub as if it were really the object – Assessor methods – Setter methods – Any methods at all that are in the interface • Must handle (catch/throw) Remote. Exceptions.

Java Naming and Directory Interface (JNDI) • RMI Registry is nice, but not very

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

Common Object Request Broker Architecture (CORBA) • Like RMI, only for mixing Objects from

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 Java. Beans (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.

Small Group Work • Work in groups of three to six people • Write

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 order?