Java Network Program - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Java Network Program

Description:

public ServerSocket(int port) throws IOException ... public ServerSocket(int port, int queue, InetAddress addr) throws IOException. Methods ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 22
Provided by: ebizUa
Category:

less

Transcript and Presenter's Notes

Title: Java Network Program


1
Java Network Program
Network Basic java.net Package Socket
Programming RMI Programming
2
Network Basic
  • Stream Socket
  • Connection Service
  • Use Transmission Control Protocol (TCP)
  • Need Connection Time Before Communication
  • Datagram Socket
  • Connectionless Service
  • Use User Datagram Protocol (UDP)
  • Transmit Each Packet Independently
  • Dont Need Connection Setup
  • Need Complement of Reliability
  • Server and Client


3
Network Basic
  • Transmission Control Protocol(TCP)
  • Set up the Connection first, then Send Data
  • After Exchanging Data, Need the Procedure for
    Releasing Connection
  • Similar to Telephone Communication
  • High Reliability
  • High overload to release the connection -gt Not
    efficient for small data
  • User Datagram Protocol(UDP)
  • Without Connection, Split data into Packet with
    fixed length, Attach address, then transmit
    them.
  • Similar to Mail
  • Low Reliability
  • Port
  • Data Address IP Address(Computer) Port Number


4
Java Network Package
5
Java.Net Package Class Description
6
URL Class
  • Class URL represents a Uniform Resource Locator,
    a pointer to a "resource" on the World Wide Web.
  • A resource can be something as simple as a file
    or a directory, or it can be a reference to a
    more complicated object, such as a query to a
    database or to a search engine
  • Name of resource consists of host name, file
    name, port number, and reference
  • URL Objects
  • URL u-aizu new URL("http//www.u-aizu.ac.jp/")
  • URL location new URL(u-aizu, location/welcome.h
    tml")
  • URL software new URL(u-aizu, welcome/software.h
    tml")
  • URL hardware new URL(u-aizu, welcome/hardware.h
    tml")


7
Methods of URL Class
  • Methods of URL class
  • getProtocol() Return the protocol of URL Address
  • getHost() Return the host name of URL Address
  • getPort() Return the port number of URL Address
  • getFile() Return the file name of URL Address
  • getRef() Return the reference of URL Address
  • InputStream openStream() Return the stream
    connected to URL
  • URLConnection openConnection() Return the URL
    Connection object connected to this URL


8
URL Class
  • Communication Using URL Class
  • Can read contents like input stream to use
    openStream method
  • (Ex TestURLRead.java)
  • Upload HTML document using URL Class
  • Use showDocument() method of AppletContext
  • (Ex TestShowDocument.java)
  • URLConnection Class
  • Have many methods to communicate with URL on
    network
  • Open Connection() method
  • (Ex TestURLConnectionRead.java)


9
Socket Class
  • Give Low Level Communication Mechanism
  • Client-Server Application Program
  • Database Query/Reply
  • What is Socket?
  • Structure for TCP
  • End point for connecting entities
  • Using the Port
  • Exists between application program and port
  • Ex) FTP server and Client


10
Socket and ServerSocket Class
  • Socket and ServerSocket
  • To implement Connecting Client Side and Server
    Side
  • Platform Independently
  • Socket Class
  • Methods
  • InputStream getInputStream() Return Input Stream
    Socket Uses
  • OutputStream getOutputStream() Return Output
    Stream Which Socket Uses
  • Inetaddress getInetAddress() Return Internet
    Address Which Socket is Connected
  • public int getLocalPort() Return the Port Number
    Socket is Connected
  • public int getPort() Return the Remote Host
    Port Number
  • public InetAddress getLocalAddress() Return the
    Internet Address which Socket is Connected


11
Socket and ServerSocket Class
  • ServerSocket Class
  • Constructor
  • public ServerSocket(int port) throws IOException
  • public ServerSocket(int port, int queue) throws
    IOException
  • public ServerSocket(int port, int queue,
    InetAddress addr) throws IOException
  • Methods
  • public Socket accept() Accept the Connection
    Request
  • public void close() Close the ServerSocket
  • public Inetaddress getInetAddress() Return the
    Internet Address which Socket is connected
  • public int getSoTimeout()
  • public void setSoTimeout() Time out to
    Socket(millisecond)
  • (Ex) Server.java and Client.java


12
Remote Method Invocation
  • Composition of RMI Applications Distributed
    Objects
  • two separate programs a server and a client.
  • A typical server application creates some remote
    objects, makes references to them accessible, and
    waits for clients to invoke methods on these
    remote objects.
  • A typical client application gets a remote
    reference to one or more remote objects in the
    server and then invokes methods on them.
  • RMI provides the mechanism by which the server
    and the client communicate and pass information
    back and forth. Such an application is sometimes
    referred to as a distributed object application.


13
Remote Method Invocation
  • Distributed object applications need
  • Locate remote objects Applications can use one
    of two mechanisms to obtain references to remote
    objects. An application can register its remote
    objects with RMI's simple naming facility, the
    rmiregistry, or the application can pass and
    return remote object references as part of its
    normal operation.
  • Communication with remote objects Details of
    communication between remote objects are handled
    by RMI to the programmer, remote communication
    looks like a standard Java method invocation.
  • Loadable class bytecodes for objects that are
    passed around Because RMI allows a caller to
    pass objects to remote objects, RMI provides the
    necessary mechanisms for loading an object's
    code, as well as for transmitting its data.


14
Remote Method Invocation

15
RMI Programming
  • Writing an RMI Server
  • The server is comprised of an interface and a
    class. The interface provides the definition for
    the methods that can be called from the client.
    Essentially the interface defines the client's
    view of the remote object. The class provides the
    implementation.
  • Designing a Remote Interface
  • Design a Remote interface, which provides the
    connection between the client and the server.
  • Implementing a Remote Interface
  • Class that implements the interface, thereby
    implementing a remote object. This class also
    provides the rest of the code that makes up the
    server program a main method that creates an
    instance of the remote object, registers it with
    the naming facility, and sets up a security
    manager.


16
RMI Programming
  • Creating a Client Program

  • Ex) ComputePi.java, ComputeEngine.java,
  • Pi.java

17
RMI Programming
  • Running Example Program


18
Program Structure(Example)
Package client
Package compute
Package engine
ComputePi.java
Compute.java
ComputeEngine.java
Pi.java
Task.java
19
RMI Programming
  • Running Example Program
  • A Note about Security
  • grant
  • permission java.net.SocketPermission
    "1024-65535", "connect,accept" permission
    java.net.SocketPermission "80", "connect"
  • Start the Server
  • unsetenv CLASSPATH
  • rmiregistry


20
RMI Programming
  • Running Example Program
  • Start the Server
  • unsetenv CLASSPATH
  • rmiregistry
  • setenv CLASSPATH /home/ann/src/home/ann/public
    _html/classes/compute.jar
  • java -Djava.rmi.server.codebasehttp//zaphod/ann
    /classes/
  • -Djava.rmi.server.hostnamezaphod.east.sun
    .com
  • -Djava.security.policyjava.policy
    engine.ComputeEngine


21
RMI Programming
  • Start the Client
  • setenv CLASSPATH /home/jones/src/home/jones/pub
    lic_html/classes/compute.jar
  • java Djava.rmi.server.codebasehttp//ford/jon
    es/classes/
  • -Djava.security.policyjava.policy
    client.ComputePi
  • zaphod.east.sun.com 20
  • Compile and Run Example
  • Refer examples/RMI/compileANDrun.txt

Write a Comment
User Comments (0)
About PowerShow.com