client side networking

Upload: sharkboy1983

Post on 02-Apr-2018

226 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/27/2019 Client Side Networking

    1/27

    Client-side networking

    14.1 Class InetAddress14.2 An InetAddress example

    14.3 Class Socket14.4 Getting Web pages with a socket14.5 Building a PostOutputStream class14.6 Wrapping up

  • 7/27/2019 Client Side Networking

    2/27

    Package java.netAuthenticatorContentHandlerDatagramPacketDatagramSocket

    InetAddressServerSocket

    Socket SocketImplURLURLConnectionURLEncoderURLStreamHandlerMalformedURLExceptionProtocolExceptionSocketExceptionUnknownHostExceptionUnknownServiceExceptionContentHandlerFactory

    SocketImplFactoryURLStreamHandlerFactory

  • 7/27/2019 Client Side Networking

    3/27

    Class InetAddress (no constructors)

    Listing of Public members of java.net. InetAddress .

    public final class InetAddress {public String getHostName()public byte[] getAddress()

    public String getHostAddress()public int hashCode()public boolean equals(Object obj)public String toString()

    public static synchronized InetAddress getByName(String host) throwsUnknownHostException

    public static synchronized InetAddress getAllByName(String host)[] throwsUnknownHostException

    public static InetAddress getLocalHost() throws UnknownHostException }

  • 7/27/2019 Client Side Networking

    4/27

    Static M ethods

    public static I netAddress getL ocalH ost() throws UnknownHostException

    The getLocalHost() returns an InetAddress objectrepresenting the address of the local host.

    public static synchroni zed I netAddress[] getAllByName(Str ing host) throws

    UnknownHostException The getAllByName() method returns an array of

    InetAddress objects representing all of theaddresses for the specified host .

  • 7/27/2019 Client Side Networking

    5/27

    Static M ethods

    public static synchronized I netAddress getByName(Str ing host) throws

    UnknownHostException

    The getByName() method returns an InetAddressobject containing the internet address informationfor the specified hostname.

    The host is a string object for the specified host.

  • 7/27/2019 Client Side Networking

    6/27

    I nstance M ethods

    public Str ing getH ostName()

    The getH ostName() method returns a Stringobject containing the name of the host for this InetAddress.

    If the host is null, the returned string willcontain any of the local machine's availablenetwork addresses.

  • 7/27/2019 Client Side Networking

    7/27

    Instance M ethods

    public byte[] getAddress()

    The getAddress() method returns an array of bytes containing the raw IP address of thisInetAddress in network byte order.

  • 7/27/2019 Client Side Networking

    8/27

    Instance M ethods --- JDK1.1

    public Str ing getH ostAddress() The getH ostAddress() method returns the IP address

    string containing the raw IP address using thestandard format (%d.%d.%d.%d).

    public boolean i sM ulticastAddress() The isM ulticastAddress() method checks if the

    InetAddress is an IP multicast address and returnsa boolean indicating if the InetAddress is an IPmulticast address.

    An IP multicast address is a Class D address i.e thefirst four bits of the address are 1110 (224~239).

  • 7/27/2019 Client Side Networking

    9/27

    UnknownHostException

    java.lang.Object|+--java.lang.Throwable

    |

    +--java.lang.Exception|+--java.io.IOException

    |

    +--java.net.UnknownHostExceptionThrown to indicate that the IP address of a hostcould not be determined.

  • 7/27/2019 Client Side Networking

    10/27

    SecurityException

    java.lang.Object|+--java.lang.Throwable

    |

    +--java.lang.Exception|+--java.lang.RuntimeException

    |

    +--java.lang.SecurityException

    Thrown by the security manager to indicate asecurity violation.

  • 7/27/2019 Client Side Networking

    11/27

    An InetAddress Example

    A nslookup like example .Class FileReader

    java.lang.Object

    | || +--java.io.FileDescriptor |+--java.io.Reader

    | |

    | +--java.io.BufferedReader |

    +--java.io.InputStreamReader |+--java.io.FileReader

    http://localhost/var/www/apps/conversion/tmp/Hugh's%20Examples/InetExample.javahttp://localhost/var/www/apps/conversion/tmp/Hugh's%20Examples/InetExample.java
  • 7/27/2019 Client Side Networking

    12/27

    Class Socket

    An implementation of a TCP/IP network connection.A client create a Socket to a remote host toestablish a streamed-based communicationchannel.

  • 7/27/2019 Client Side Networking

    13/27

    Class Socket

    Protected members of java.net. Socket .protected Socket()

    Creates an unconnected socket, with the system-

    default type of SocketImp protected Socket(SocketI mpl impl)

    Creates an unconnected Socket with a user-

    specified SocketImpl .

  • 7/27/2019 Client Side Networking

    14/27

    Class SocketListing of Public members of java.net. Socket .

    public final class Socket {public Socket (String host, int port) throws UnknownHostException,

    IOExceptionpublic Socket (String host, int port, boolean stream) throws IOExceptionpublic Socket (InetAddress address, int port) throws IOException

    public Socket (InetAddress address, int port, boolean stream) throwsIOException

    public InetAddress getInetAddress()public int getPort()public int getLocalPort()

    public InputStream getInputStream() throws IOExceptionpublic OutputStream getOutputStream() throws IOExceptionpublic synchronized void close() throws IOExceptionpublic String toString()public static synchronized void setSocketImplFactory(SocketImplFactory fac)

    throws IOException}

  • 7/27/2019 Client Side Networking

    15/27

    Constructors

    public Socket(Str ing host, int por t) throws UnknownH ostException, I OException

    This Socket() constructor creates a stream socket tothe specified port on the specified host.

    The host is a String object containing the host nameto create the socket on.

    The port is an integer (1-65535) value representingthe port to create the socket on.

  • 7/27/2019 Client Side Networking

    16/27

    Constructors

    public Socket(Str ing host, int port, boolean stream) throws I OException --- Deprecated

    This Socket() constructor creates a stream socket tothe specified port on the specified host.

    The boolean stream value can be used to specify astream socket or a datagram socket.

    The stream is a boolean value that is true if a stream socket is to be created, false if a datagram socketis to be created.

  • 7/27/2019 Client Side Networking

    17/27

    Constructors

    public Socket(I netAddress address, int port) thr ows I OException

    This Socket() constructor creates a streamsocket to the specified port at the specifiedInetAddress.

    The address is an I netAddress specifying theaddress at which to create the socket.

  • 7/27/2019 Client Side Networking

    18/27

    Constructors

    publ ic Socket(I netAddress address, int port,boolean stream) throws I OException

    --- Deprecated

    This Socket() constructor creates a stream socket tothe specified port at the specified address.

    The boolean stream value can be used to specify astream (true ) socket or a datagram (false ) socket.

    The address is an InetAddress specifying the addressat which to create the socket.

  • 7/27/2019 Client Side Networking

    19/27

    Constructors JDK1.1

    publ ic Socket(I netAddress address, int port,I netAddress localAddr , int localPort) thr ows

    I OException Good for multihomed machine- a machine withmultiple network interface.

    This Socket() constructor creates a socket andconnects it to the specified remote address on thespecified remote port.

    The Socket will also bind() to the local address and port supplied.

  • 7/27/2019 Client Side Networking

    20/27

    Constructors JDK1.1

    public Socket(Str ing host, int por t, I netAddress localAddr , int localPor t) thr ows I OException

    This Socket() constructor creates a socket andconnects it to the specified remote host on the

    specified remote port.The Socket will also bind() to the local address and port supplied.

    Null - use default local address.

    0 - use a random unsed port.

  • 7/27/2019 Client Side Networking

    21/27

    M ethods

    See the JDK1.3 Documentation

    http://localhost/Utilities%20Programs/JAVA/jdk1.3/docs/index.htmlhttp://localhost/Utilities%20Programs/JAVA/jdk1.3/docs/index.html
  • 7/27/2019 Client Side Networking

    22/27

    Related Exceptions

    Class SocketException

    java.lang.Object|+--java.lang.Throwable

    |+--java.lang.Exception

    |+--java.io.IOException

    |+--java.net. SocketException

    Direct Known Subclasses:BindException , ConnectException ,NoRouteToH ostException

  • 7/27/2019 Client Side Networking

    23/27

    Related Exceptions

    Class SecurityException

    java.lang.Object|+--java.lang.Throwable

    |+--java.lang.Exception

    |+--java.lang.RuntimeException

    |+--java.lang. SecurityException

    Direct Known Subclasses:AccessContr olException, RM I Secur i tyException

  • 7/27/2019 Client Side Networking

    24/27

    Socket I mplementation

    Must inherit SocketI mpl abstract class.Create a subclass that implements the necessaryconnection setup and control methods.Create a SocketI mplF actory that returnsinstances of this new SocketI mpl class.Register this SocketI mplF actory with thesocket class through its setSocketI mplF actory()

    method.Provide, if necessary, a correspondingimplementation at the server.

  • 7/27/2019 Client Side Networking

    25/27

    Getting Web pages with a socket

    An Example to demonstrate how to download aweb page.This program uses URL class

    http://localhost/var/www/apps/conversion/tmp/Hugh's%20Examples/GrabPage.javahttp://localhost/Utilities%20Programs/JAVA/jdk1.3/docs/api/java/net/URL.htmlhttp://localhost/Utilities%20Programs/JAVA/jdk1.3/docs/api/java/net/URL.htmlhttp://localhost/var/www/apps/conversion/tmp/Hugh's%20Examples/GrabPage.java
  • 7/27/2019 Client Side Networking

    26/27

    Building a PostOutputStream class

    Demostrating an OutputStream class that perform an HTTP post operation.Connect to the web server and send:

    POST filename HTTP/1.0Content-type: MIME type application/x-www-form-urlencoded Content-length: length

  • 7/27/2019 Client Side Networking

    27/27

    Building a PostOutputStream class

    The body contains:key1=value1&key2=value2.. The Example Program

    http://localhost/var/www/apps/conversion/tmp/Hugh's%20Examples/PostOutputStream.javahttp://localhost/var/www/apps/conversion/tmp/Hugh's%20Examples/PostOutputStream.java