advanced java class
play

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


  1. Advanced Java Class Network Programming

  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

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

  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

  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

  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

  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 other side of the connection

  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.

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

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

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

  12. 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

  13. Remote Class Implementation • Implements the interface • Extends java.rmi.server.RemoteObject, usually by extending its subclass java.rmi.server.UnicastRemoteObject

  14. _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.)

  15. 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

  16. 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) RemoteExceptions.

  17. 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

  18. 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.

  19. 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?

Recommend


More recommend