Implementing the Network Functionality


The NetCompConnect.java file implements the functions of the Network Information application. This file reads the hosts IP address and port number on which the end user wants to create the socket channel. The NetCompConnect.java file then retrieves information, such as echo time and current system date and time of the computer the end user selects. The NetCompConnect.java file passes this information to the CompInfo.java file, which sends it to the CompInfoDialog.java file. The CompInfoDialog.java file displays the computer information in a dialog box.

Listing 7-2 shows the contents of the NetCompConnect.java file:

Listing 7-2: The NetCompConnect.java File
start example
 /* Imports java.net package classes. */ import java.net.*; /* Imports java.io package classes. */ import java.io.*; /* Imports java.nio package classes.*/ import java.nio.*; import java.nio.channels.*;  import java.nio.charset.*; import java.util.*; /*  Class NetCompConnect - This class connects the application to the network using socket channel. This class also performs the ping and date/time operations.  Fields:    strCompAddr - Contains the IP address.    mainApp - Creates the object of the NetCompFrame class.    port - Contains the port number.    charset - Contains the charset.    decoder - Creates the decoder.    buff - Creates the buffer.     isa - Contains the object of the InetSocketAddress class.    sc - Create the socket channel. Methods:    connectComp() - This method creates the socket channel at the specified port number.    run() - This method is invoked when the thread is started.    */ public class NetCompConnect extends Thread {    /* Declares the object of the String class. */    String strCompAddr = null;    /* Declares the object of the NetCompFrame class. */    NetCompFrame mainApp = null;    /* Declares the object of the Charset class. */    Charset charset;    /* Declares the object of the CharsetDecoder class. */    CharsetDecoder decoder;    /* Declares the object of the ByteBuffer class. */    ByteBuffer buff;    /* Declares the object of the InetSocketAddress class. */    InetSocketAddress isa;    /* Declares the object of the SocketChannel class. */    SocketChannel sc = null;    int port = 0;    /* Defines the default constructor. */    public NetCompConnect(String strCompAddr, NetCompFrame mainApp, int port)    {       this.strCompAddr = strCompAddr;       this.mainApp = mainApp;       this.port = port;    }    /*        connectComp() - This method creates and initializes the socket at port 7 or 13 to        retrieve the echo time or date/time of the specific computer.       Parameter:       strCompAddr - Stores the computer IP address.       port - Stores the port number.       Return Value: NA    */    public int connectComp(String strCompAddr, int port)    {       String compInfo = "";       String temp = "";       String time = "";       String am_pm = "";       String date = "";       String date_time = "";       /* Gets the current time of the system in milliseconds. */       long startTime = System.currentTimeMillis();       try       {             /*           Checks the port number, if port number is 13, this code is executed.           */          if (port == 13)          {             /* Initializes the object of the Charset class. */             charset = Charset.forName("US-ASCII");             /* Initializes the object of the CharsetDecoder class. */             decoder = charset.newDecoder();             /* Initializes and allocates the size of the buffer. */             buff = ByteBuffer.allocateDirect(1024);                /* Initializes the object of the InetSocketAddress class at port 13. */             isa = new InetSocketAddress(strCompAddr, port);             try              {                /* Opens the socket channel. */                sc = SocketChannel.open();                /* Connects the socket channel to the specified IP address. */                sc.connect(isa);                /* Clears the buffer. */                buff.clear();                /* Reads the data from the buffer. */                sc.read(buff);                /* Flips the buffer. */                buff.flip();                /*                 Creates and initializes the object of CharBuffer and stores the decoded data from the buffer.                 */                CharBuffer cb = decoder.decode(buff);                temp = new String(cb.array());                StringTokenizer st = new StringTokenizer(temp, " ");                time = (String)st.nextToken();                am_pm = (String)st.nextToken();                date = (String)st.nextToken();                date_time = date + " " + time + " " + am_pm;                compInfo = compInfo + date_time;             }             finally              {                if (sc != null)                /* Close the socket channel. */                sc.close();             }             /*              Creates the object of CompInfo class and Adds this object to the              compInfoVec vector of the NetCompFrame class.              */             mainApp.compInfoVec.addElement(new CompInfo(strCompAddr, compInfo));             return 1;          }          /* Checks the port number, if port number is 7, this code is executed. */          else          {             /* Initializes the object of the InetSocketAddress class at port 7. */              isa = new InetSocketAddress(strCompAddr, port);             try              {                /* Opens the socket channel. */                sc = SocketChannel.open();                /* Connects the socket channel to the specified IP address. */                sc.connect(isa);                /* Gets the socket from the socket channel. */                 Socket t = sc.socket();                /*                 Creates and initializes the object of DataInputStream class to retrieves                 the data from the socket.                 */                 DataInputStream is = new DataInputStream(t.getInputStream());                /*                 Creates and initializes the object of PrintStream class to put the data to the socket.                 */                PrintStream ps = new PrintStream(t.getOutputStream());                ps.println("ping");                String str = is.readLine();                if (str.equals("ping"))                {                   /* Gets the current system time. */                    long endTime = System.currentTimeMillis();                   /* Evaluates the echo time. */                   compInfo = (endTime-startTime) + " ms";                   /*                   Creates the object of CompInfo class and Adds this object to the                    compInfoVec vector of the NetCompFrame class.                    */                   mainApp.compInfoVec.addElement(new CompInfo(strCompAddr, compInfo));                   return 1;                }                else                {                   return 0;                }             }             finally              {                if (sc != null)                /* Close the socket channel. */                sc.close();             }          }       }       catch (Exception e)         {          return 0;       }    }    /*     run() - This method is executed when thread is started.    Parameter: NA    Return Value: NA    */    public void run()    {       /* Calls the connectComp() method */        connectComp(this.strCompAddr, this.port);    } } 
end example
 

Download this Listing .

In the above code, the constructor of the NetCompConnect class reads the host IP address, port number, and object of the NetCompFrame class. The methods defined in the above code are:

  • run() : Calls the connectComp() method to connect the application to the network.

  • connectComp() : Checks the port number to establish a connection. If the port number is 13, this method initializes the objects of the Charset and CharsetDecoder classes. This method then initializes and allocates the size of the buffer, initializes the object of the InetSocketAddress class at port 13, and opens a socket channel on port 13. Next, the connectComp() method connects the socket channel to the specified host address, reads the bytes into buffer, and decodes the bytes from buffer to character. This method then creates the object of the CompInfo class and adds this object to the compInfoVec vector of the NetCompFrame class. If the port number is 7, the connectComp() method initializes the object of the InetSocketAddress class at port 7 and opens the socket channel on that port. The connectComp() method then connects the socket channel to the specified IP address, gets the socket from the socket channel, and evaluates the echo time in milliseconds. Next , the connectComp() method creates the object of the CompInfo class and adds this object to the compInfoVec vector of the NetCompFrame class.




Java InstantCode. Developing Applications Using Java NIO
Java InstantCode. Developing Applications Using Java NIO
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 55

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net