Bài giảng Java 2 - Trần Duy Thanh - Networking

Networking introduction

URL Class

InetAddress Class

Working with Socket

connection-based protocol

connectionless-oriented protocol

Remote Method Invocation (RMI)





pptx54 trang | Chuyên mục: Java | Chia sẻ: dkS00TYs | Ngày: 08/06/2015 | Lượt xem: 4669 | Lượt tải: 4download
Tóm tắt nội dung Bài giảng Java 2 - Trần Duy Thanh - Networking, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
cal connections, there is no means to identify the application to which it should be forwarded. Hence, data being sent and received on the physical connection are based on the concept of ports. Concept of a Port The physical connection is logically numbered within a range of 0 to 65535 called as Ports. The port numbers ranging from 0 to 1023 are reserved for well known services such as HTTP, FTP Your applications should not use any of the port numbers in this range. Data transmitted over the Internet is accompanied with the destination address and the port number. The destination address identifies the computer The port number identifies the application. Concept of a Port Introduction to URL A reference or an address to a resource on the internet. The resource can be an HTML page, an image or simply any file. URL form	protocol://resource Protocol Identifier Name of the protocol, which is used to fetch the resource from the Internet. Resource Name Address of the resource Separated from the protocol identifier with a colon and two forward slashes Resource format depends on the protocol being used. Introduction to URL	The URL class The URL class represents an URL object. It belong to java.net package. Introduction to URL	 Introduction to URL	URL example result The InetAddress Class The InetAddress class retrieve the host address/name. Represents an Internet Protocol (IP) address Provides methods to resolve host names to their IP addresses and vise versa. Implementing server	Socket programming The Java interface with a TCP connection is a socket. A socket is a Java object which has methods to connect, accept connections and transfer data. Core Networking is in java.net.* TCP Sockets come in two flavors : ServerSocket and Socket. Implementing server	The ServerSocket Class Represent the server side of the two-way communication. The ServerSocket has to bind to a specific port which should be free and available. If the port is being used by any other application, an exception is thrown. If the ServerSocket class is successful in binding to a port, it can then wait and listen for client request to establish connections Implementing server	The ServerSocket Class (cont.) Implementing server	Working with the ServerSocket class Once a ServerSocket instance is created, you can invoke the accept() method to listen for client request. The accept () method is a blocking method that is once invoked it will wait till a client requests for a connection. When a client requests for a connection, the accept() method creates a socket object of class Socket and returns it. This returned object represents a proxy of a client. To communicate with the client, you retrieve the Inputstream and Outputstream of this proxy socket object. Implementing server	Working with the Socket class Represent the connection between a client program and a server program. Represents the client side of the connection Implementing server	 Logic diagram to implements a server It gets a command from the client through an incoming data stream. It somehow fetches the information. It sends the information to the client through the outgoing data stream. Implementing server	 Example Implementing server	Serving Multiple clients One ServerSocket can accept multiple clients simultaneously. Use multithreading to handle them. One thread waits for “accept()”. Open a new thread for each connection. Implementing server	Serving Multiple clients example For connection-based protocol - Socket class Represent the connection between a client program and a server program. Represents the client side of the connection FPT- APTECH 24/43 Implementing client	 Logic diagram to implements a client Create a connection to the server Send request information to the server through the outgoing data stream. It receives the information from the server through the incoming data stream. Processing received information Implementing client	 Client example Data transmission recommendations To transmit text only data, we can use InputStream and OutputStream. To transmit binary data, we must turn the streams into DataInputStream and DataOutputStream. To transmit serialized objects, we would use ObjectInputStream and ObjectOutputStream instead. User Datagram Protocol (UDP)	introduction UDP can be used to send packets. The packets can be delivered in random order. It is up to the recipient to put the packets in order and to request retransmission of missing packets. UDP is most suited for applications where missing packets can be tolerated, for example, in audio or video streams... User Datagram Protocol (UDP)	Create Datagram Socket DatagramSocket socket = new 	DatagramSocket(port); The client uses a constructor that does not require a port number. This constructor just binds the DatagramSocket to any available local port. It doesn't matter what port the client is connected to because the DatagramPackets contain the addressing information. The server gets the port number from the DatagramPackets and send its response to that port. User Datagram Protocol (UDP)	The Datagram packet An independent, self-contained message sent over the network. Used to employ a connectionless packet delivery service. Packet delivery is not guaranteed Datagram packet structure Addressing information (IP) Port Sequence of bytes 30/43 User Datagram Protocol (UDP)	The DatagramPacket class User Datagram Protocol (UDP)	How to use DatagramSocket class Once a DatagramSocket instance is created, it can be used to send and receive data using packets. User Datagram Protocol (UDP)Processing logic - Client send Request to Server User Datagram Protocol (UDP)	Processing logic - Server receive Request from Client The getData() method to retrieve that data from the packet. User Datagram Protocol (UDP)	Processing logic - Server Sends Response  User Datagram Protocol (UDP)	Processing logic –Client receive response Remote Method Invocation The Roles of Client and Server	synchronized request/response model Remote Method call with proxies	new approach “The network is the computer”* Consider the following program organization: If the network is the computer, we ought to be able to put the two classes on different computers RMI is one technology that makes this possible SomeClass AnotherClass method call returned object computer 1 computer 2 What is RMI? RMI allows objects in one JVM to invoke methods of objects in another JVM. All in java.rmi package RMI Architecture RMI Server provides an RMI service. RMI Client invokes object methods of RMI service “rmiregistry” program Runs as a separate process Allows applications to register RMI services Allows obtain a reference to a named service. Stub object Resides on the client side Responsible for passing a message to a remote RMI service, waits for a response, and returns this response to the client. Stubs When client code wants to invoke a remote method on a remote object, it actually calls an ordinary method of the Java programming language that is encapsulated in a surrogate object called a stub. The stub packages the parameters used in the remote method into a block of bytes. This packaging uses a device-independent encoding for each parameter. The process of encoding the parameters is called parameter marshalling. The purpose of parameter marshalling is to convert the parameters into a format suitable for transport from one virtual machine to another. Parameters marshalling Implementing RMI application process Define RMI Service Interface Implement RMI Service Interface Create RMI Server program Create RMI Client program Running the RMI application #1/5 Define RMI Service Interface Your client program needs to manipulate server objects, but it doesn't actually have copies of them. Their capabilities are expressed in an interface that is shared between the client and server and so resides simultaneously on both machines. The interface characteristics: Must be public Must extend the interface java.rmi.Remote Every method in the interface must declare that it throws java.rmi.RemoteException (other exceptions may also be thrown) #2/5 Implementing RMI Service Interface On the server side, you must implement the class that actually carries out the methods advertised in the remote interface The class characteristics: Should extend java.rmi.server.UnicastRemoteObject Must implement a Remote interface Must have a default constructor. #3/5 Creating RMI Server program 	 The RMI server is responsible for: Instance object of a service implementation class Registering it with the RMI registry #3/5 Creating RMI Server program 	 The RMI server is responsible for: Instance object of a service implementation class Registering it with the RMI registry #4/5 Creating RMI Client program The client obtains an object reference to the remote interface by making a lookup in the RMI registry Then invoking methods of the service interface. #5/5 Running the RMI System Start Server Compile all java file as normal.cmd: javac *.java Start rmiregistry program.cmd: start rmiregistry Run server program.cmd: start java Calc_Server Start Client Create policy filecmd: client.policy Run clientcmd: java -Djava.security.policy=client.policy Calc_Client Security By default, the RMISecurityManager restricts all code in the program from establishing network connections. However, the program needs to make network connections To reach the RMI registry; and To contact the server objects To allow the client to connect to the RMI registry and the server object, you supply a policy file. Here is a policy file that allows an application to make any network connection to a port with port number of at least 1024: grant{ 	permission java.net.SocketPermission "*:1024-65535","connect"; }; Others Listing all bounded remote objects in registry Create your own RMIRegistry java.rmi.registry.LocateRegistry.createRegistry(1099); Summary Data exchange through network using Socket, UDP Remote method invocation END 55 

File đính kèm:

  • pptx3.Networking.pptx
Tài liệu liên quan