Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Get this document free

java network programming

VIEWS: 8 PAGES: 29

jjava programming

More Info
									Java Network Programming




       Your IT Partner
Objectives
After completing this chapter, the student will be able to:

 •   Explain Java’s networking protocols
 •   Explain Uniform Resource Locator(URL)
 •   Explain Java’s support for networking
 •   Write Java programs to communicate with
     remote systems
 •   Write Java programs to get the IP addresses of
     remote systems
 •   Write Java programs to establish two way
     communication with remote systems


                                            CMC Limited
                                    Objectives contd..




•   Write Java programs to communicate with
    remote systems, using datagram packets
•   Write Java programs to establish TCP
    communication with remote systems
•   Write Java programs to download web pages
•   Develop client-server applications




                                    CMC Limited
Introduction
•   Java is the first mainstream programming language.

•   It provides built-in support       for    high   level
    Network/Internet programming.

•   The Java execution environment is designed in such
    a way that application program, that access
    computers on any part of the globe, can be written
    very easily.

•   Java derives this power from a set of classes defined
    in the java.net package.

                                             CMC Limited
Java’s Networking Protocols
•   Java uses TCP/IP protocol for all its networking
    functionalities.

•   TCP/IP stands for Transmission Control Protocol/
    Internet Protocol.

•   Within the TCP/IP scheme, three protocols are
    commonly used, namely TCP, IP and UDP

•   TCP/IP is a whole collection of protocols.




                                            CMC Limited
Internet Protocol (IP)
•   All data on the internet flows through IP packets.
•   IP is a connectionless unreliable protocol.
•   IP does not exchange control information to establish
    a path before transmitting data to a remote system.
•   Each IP packet takes its own path to reach the
    destination.
•   IP does not retransmit the lost data.
•   It does not detect corrupted data.
•   IP packets reaching the destination does not form the
    part of a stream of data.




                                          CMC Limited
Transmission Control Protocol (TCP)
•   TCP provides a reliable, connection oriented,
    continues stream protocol.
•   Before sending data through TCP, a computer
    establishes a connection with a remote system by
    sending control information.
•   After completing the data transfer, the connection is
    closed.
•   A TCP segment can contain an arbitrary number of
    bytes.
•   TCP allows to send a TCP segment to a destination
    port address.
•   A port identifies a TCP communication stream
    running on a system.

                                          CMC Limited
User Datagram Protocol
•   UDP has no mechanism for detecting lost or
    corrupted data.
•   UDP is a connectionless protocol.
•   UDP data packets are transmitted with the
    assumption that the destination machine will receive
    them.
•   Applications can send self-contained messages in
    UDP datagrams.
•   As with TCP, UDP provides the addressing scheme
    of ports, allowing many applications to send and
    receive UDP datagrams simultaneously.
•   UDP ports are different from TCP ports.


                                         CMC Limited
Uniform Resource Locator (URL)
•   URL provides a uniform format for identifying
    resources on the systems on the internet.
•   The general format of a URL is given below:
    • scheme_name              :      //       host      :
       port/file_info#internal_reference
    • scheme_name is the protocol used such as
       HTTP,FTP,Gopher etc.
    • host is the IP address of the remote system.
    • port is the port number of the service required.
    • file_info is the resource requested from the
       remote system.
    • Internal_reference is the identifier of a named link
       within the HTML page.

                                           CMC Limited
java.net Package
•   The classes of java.net package are listed below:
    • class URL
    • class URLConnection
    • class Socket
    • class ServerSocket
    • class DatagramSocket
    • class DatagramPacket
    • class MulticastPacket
    • class InetAddress
    • class URLEncoder
    • class URLDecoder
    • class URLStreamHandler
    • class ContentHandler

                                          CMC Limited
The URL Class
•   The URL class enables you to get resources from a
    remote system.
•   After creating a URL object, there are two ways to
    get data from a remote system.
•   One way is to read data directly from the remote
    system.
•   In this way you can only read data from a remote
    system and it becomes a read only connection.
•   The URLConnection class enables you to maintain a
    two way communication.




                                        CMC Limited
Reading Data Directly From A URL
Connection
•   The URL class member function.
•   public final InputStream     openStream( ) throws
    java.io.IOException.
•   It is used to create an InputStream object in which
    data from the remote system will be stored byte by
    byte.
•   Since handling data byte by byte is a bit
    cumbersome, a DataInputStream object is created in
    which the InputStream object is embedded.
•   The DataInputStream object enables you to read
    data line by line.

                                         CMC Limited
The URLConnection Class
•   The definition of URLConnection Class is given
    below:
    public abstract class URLConnection extends Object
        {
               :
               :
        public InputStream getInputStream( )
                       throws java.io.IOException;
        public OutputStream getOutputStrem()
                        throws java.io.Exception;
               :
               :
        }

                                        CMC Limited
Two Way Communication Using URL
Connection
•   To establish a two way communication over a URL
    connection, we create a URLConnection object using
    the URL class member function
       public URLConnection openConnection( );

•   Next, we use the URLConnection class member
    functions
       public InputStream getInputStream( ) throws
       java.io.IOException
       public OutputStream getOutputStream( ) throws
       java.io.IOException


                                        CMC Limited
Getting the IP Address
•   If you know the textual IP address of a remote
    system on the Internet, there is a method to get the
    numeric IP address of that host system.

•   We can make use of the InetAddress class in the
    java.net package for this purpose.




                                         CMC Limited
                           Getting the IP Address contd..

The InetAddress Class
•   The definition of InetAddress class is given below.

    public final class InetAddress extends Object
       implements java.io.Serializable
    {
    public static InetAddress[] getAllByName(String host)
                       throws UnknownHostException ;
    public static InetAddress getByName(String host)
                            throws UnknownHostException
       ;
    public static InetAddress getLocalHost( )
                    throws UnknownHostException ;


                                           CMC Limited
Communicating With a Remote
System
•   URL class and URLConnection class from the
    java.net package were used.

•   java.net package provides several other classes to
    enable you to establish two way communication with
    remote systems.

•   The most basic method for communicating with a
    remote system is to use UDP datagrams.




                                        CMC Limited
            Communicating With a Remote System contd..

The DatagramPacket Class
•   The definition of the DatagramPacket class is given
    below.

    public final class DatagramPacket extends Object
    {
    public DatagramPacket(byte[] inBuff, int inLength );
    public DatagramPacket(byte[] outBuff, int outLength,

               InetAddress iaddr, int port );
    public InetAddress getAddress( );
    public byte[] getData( );
    public int getPort( );
    }

                                                CMC Limited
           Communicating With a Remote System contd..

The DatagramSocket Class
•   The definition of the DatagramSocket class is given
    below:

    public final class DatagramSocket extends Object
                     {
                     public DatagramSocket ( ) throws
       SocketException
                     public DatagramSocket ( int port)
       throws SocketException
                     public DatagramSocket ( int port,
       InetAddress laadr)
                                         throws


                                         CMC Limited
           Communicating With a Remote System contd..

Communication with a Remote System:
An Example
•   This program access the sybex web server and asks
    it what its local time is.

    import java.net.*;
    class GetDate
         {
        final static int PORT_DAYTIME = 13;
        public static void main(String[] args) throws
       Exception
          {



                                       CMC Limited
Communication Using TCP
•   For reliable communication involving large volumes
    of data we have to use TCP protocol.

•   The Socket class in the java.net package provides
    the necessary functionalities to enable a TCP
    communication.

•   Socket class    is   often   used   in   client   server
    applications.

•   Server port numbers are valid as long as the server
    application is up and running.


                                             CMC Limited
                         Communication Using TCP contd..

The Socket Class
•   The definition of the Socket class is given below:
    public class Socket extends object
                          {
                          public Socket( String host, int
       port)
                                  throws
       UnknownHostException, java.io.IOException;
                          public    Socket(     InetAddress
       address, int port)
                                              throws
       java.io.IOException;
                            public Socket(String host , int
       port, InetAddress localAddr,

                                            CMC Limited
                        Communication Using TCP contd..

Communication with an HTTP Web Server
•   Web service relies on the HTTP protocol.

•   HTTP operates in an ASCII line based format.

•   The client initiates a communication by sending the
    web server a request header.

•   Common type of request header is “GET” request.

•   There are three components to this request header:
    • The request method ( GET )
    • The resource ( URL )
    • The version of HTTP protocol used for data
       exchange ( HTTP/1.0 )
                                         CMC Limited
                        Communication Using TCP contd..

Downloading a Web Page
•   A program to download a web page from the internet:
    import java.io.*;
    import java.net.*;

    public class GetWebPage
        {
      public static void main(String[]     args)throws
       IOException,

       UnknownHostException;
           {……..



                                         CMC Limited
Client Server Programming
•   To write a server program to provide some special
    kind of service to the world, you can use the
    ServerSocket class in the java.net package.




                                       CMC Limited
                       Client Server Programming contd..

The ServerSocket Class
•   The ServerSocket class has three constructors.
•   All the three constructors take the port number as
    one of the parameters.
•   If you want to write standard server application, you
    have to use their respective port numbers.
•   If you are writing a server applications to provide a
    totally new service you have to use a port number
    that nobody else is using.
•   The port numbers range from 0 to 65,535.
•   Port numbers from 0 to 1023 are reserved for
    standard services.From 1024 onwards it is client
    ephemeral ports.


                                          CMC Limited
                       Client Server Programming contd..

An Example Server Program
•   An example server program is given below:

    import java.util.*; // for new Date ( )
    import java.io.*;
    import java.net.*;
    public class ServerTest
       {
       final static int SERVER_PORT = 8001;
       public static void main(String[] args )
                  {




                                          CMC Limited
                        Client Server Programming contd..

A Client Program for Our Example Server
Program
•   We have seen in the above section that even though
    our server program is running,
•   It cannot proceed until a client program connects to it
    at port number 8001.
•   We will now write a client program that will do this
    job.
        import java.io.*;
        import java.net.*;
        public class ClientTest
            {
            public static void main(String[] args)
              {

                                            CMC Limited
                        Client Server Programming contd..

An example demonstrating multiple clients
interacting with a MultiThreaded Server
•   /*A Simple Web Server

•   This server handles very simple GET commands
    only and is good enough to return web pages, even
    those which contain applets.
       httpd.java
       */

       import java.io.*;
       import java.net.*;
       import java.util.*;
       import java.lang.*;

                                         CMC Limited

								
To top