200 likes | 303 Views
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
E N D
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 • 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 • URLConnections • RMI, JNDI, and CORBA
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
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
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 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
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.
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.
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.
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 • All methods must declare throws java.rmi.RemoteException • Extends java.rmi.Remote
Remote Class Implementation • Implements the interface • Extends java.rmi.server.RemoteObject, usually by extending its subclass java.rmi.server.UnicastRemoteObject
_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.)
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
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.
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 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.
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?