home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 February / InternetNews_1999_02.iso / pc / Professionale / codici9810 / SampleClient.java next >
Text File  |  1998-01-25  |  5KB  |  184 lines

  1. /*
  2. **    Programma:        SampleClient
  3. **    Versione:        1.0.0
  4. **    Autore:        Dario de Judicibus
  5. **    Indirizzo:        dejudicibus@geocities.com
  6. **    Creato il:        January 3rd, 1998
  7. **    Modificato il:    January 4th, 1998
  8. **    Linguaggio:        Java (jdk 1.1)
  9. **    -------------------------------------------------------------
  10. **    (c) 1998 - All rights reserved 
  11. **    -------------------------------------------------------------
  12. **    I make no representations or warranties about the suitability of
  13. **    the software, either express or implied, including but not limited
  14. **    to the implied warranties of merchantability, fitness for a
  15. **    particular purpose, or non-infringement. I shall not be liable for
  16. **    any damages suffered by licensee as a result of using, modifying or
  17. **    distributing this software or its derivatives.
  18. */
  19.  
  20. /**
  21.  *    @author Dario de Judicibus
  22.  *    @version 1.0.0
  23.  *    @see jdk 1.1
  24.  */
  25. import java.io.* ;
  26. import java.net.* ;
  27. import java.util.Date ;
  28. import java.text.SimpleDateFormat ;
  29.  
  30. class SampleClient
  31. {
  32.     // CLASS VARIABLES
  33.     private static final int     cvPORT = 2000 ;
  34.     private static final String     cvDATEFORMAT = "EEE dd MMM yyyy HH:mm:ss z" ;
  35.  
  36.     // INSTANCE VARIABLES
  37.     private String             ivHostName ;
  38.     private String             ivServerAddress ;
  39.     private Socket            ivClientSocket ;
  40.     private BufferedReader        ivFromServerStream ;
  41.     private PrintWriter        ivToServerStream ;
  42.     private SimpleDateFormat    ivFormatter ;
  43.  
  44.     public static void main( String[] args )
  45.     {
  46.         String givenAddress = null ;
  47.  
  48.         if ( args.length == 1 )
  49.         {
  50.             givenAddress = args[ 0 ] ;
  51.         }
  52.  
  53.         SampleClient sampleClient = new SampleClient( givenAddress ) ;
  54.         sampleClient.run( ) ;
  55.     }
  56.  
  57.     private SampleClient( String serverAddress )
  58.     {
  59.         ivServerAddress = serverAddress ;
  60.         ivFormatter = new SimpleDateFormat( cvDATEFORMAT ) ;
  61.  
  62.         System.out.println( "[" + ivFormatter.format( new Date( ) ) + "] " + "Starting client." ) ; 
  63.     }
  64.  
  65.     private void run( )
  66.     {
  67.         // Try to create a client socket first.
  68.         // If fails, just abend the client.
  69.         try
  70.         {
  71.               createClientSocket( ) ;
  72.         }
  73.         catch ( UnknownHostException exc )
  74.         {
  75.             System.out.println( "[ERROR] Unknown host: " + exc.getMessage( ) ) ;
  76.             System.exit( 1 ) ; // Terminate server
  77.         }
  78.         catch ( IOException exc )
  79.         {
  80.             System.out.println( "[ERROR] IO: " + exc.getMessage( ) ) ;
  81.             System.exit( 1 ) ; // Terminate server
  82.         }
  83.     
  84.         // Manage interactions with server.
  85.         // If fails, print the stack trace and abend the client.
  86.         try
  87.         {
  88.             // Prepare for data exchange
  89.             prepareForExchange( ) ;
  90.             
  91.             int c ;
  92.             String inputLine ;
  93.                   StringBuffer outputLine = new StringBuffer( 50 ) ;
  94.  
  95.             // Process input from server until closing
  96.             while ( ( inputLine = ivFromServerStream.readLine( ) ) != null )
  97.             {
  98.                 System.out.println( "[SERVER] " + inputLine ) ;
  99.                 if ( inputLine.equals( "OK Closing server." ) )
  100.                             break ;
  101.  
  102.                 while ( ( c = System.in.read( ) ) != '\n' )
  103.                 {
  104.                     outputLine.append( (char)c ) ;
  105.                 }
  106.  
  107.                 System.out.println("[CLIENT] " + outputLine ) ;
  108.                 ivToServerStream.println( outputLine.toString( ) ) ;
  109.  
  110.                 outputLine.setLength( 0 ) ;
  111.             }
  112.  
  113.             // Terminate session and close connection
  114.             terminateConnection( ) ;
  115.  
  116.         } 
  117.         catch ( UnknownHostException exc )
  118.         {
  119.             System.out.println( "[ERROR] Unknown host: " + exc.getMessage( ) ) ;
  120.             System.exit( 1 ) ; // Terminate server
  121.         }
  122.         catch ( IOException exc )
  123.         {
  124.             System.out.println( "[ERROR] IO: " + exc.getMessage( ) ) ;
  125.             System.exit( 1 ) ; // Terminate server
  126.         }
  127.     }
  128.  
  129.     private void createClientSocket( ) throws UnknownHostException,IOException
  130.     {
  131.         InetAddress serverAddress ;
  132.  
  133.         try
  134.         {
  135.             serverAddress = InetAddress.getByName( ivServerAddress ) ;
  136.         }
  137.         catch ( UnknownHostException exc )
  138.         {
  139.             serverAddress = InetAddress.getByName( null ) ;
  140.  
  141.         }
  142.  
  143.         ivHostName = serverAddress.getHostName( ) ;
  144.  
  145.         ivClientSocket = new Socket( ivHostName, cvPORT ) ;
  146.  
  147.         System.out.println( "[" + ivFormatter.format( new Date( ) ) + "] " 
  148.             + "Connecting to server " + ivHostName + " on port " + cvPORT ) ;      
  149.  
  150.         return ;
  151.     }
  152.  
  153.     private void prepareForExchange( ) throws IOException
  154.     {
  155.         // Create the input and output streams
  156.         // Both streams are buffered
  157.                ivFromServerStream = new BufferedReader(
  158.             new InputStreamReader(  ivClientSocket.getInputStream( ) ) ) ;
  159.  
  160.                ivToServerStream = new PrintWriter(    ivClientSocket.getOutputStream( ), true ) ;
  161.  
  162.         System.out.println( "[" + ivFormatter.format( new Date( ) ) + "] " + "Client ready" ) ; 
  163.  
  164.         return ;
  165.     }
  166.  
  167.     private void terminateConnection( ) throws IOException
  168.     {
  169.             ivToServerStream.close( ) ;
  170.             ivFromServerStream.close( ) ;
  171.             ivClientSocket.close( ) ;
  172.  
  173.         System.out.println( "[" + ivFormatter.format( new Date( ) ) + "] "
  174.             + "Closing connection from " + ivHostName ) ;
  175.  
  176.         // Reset all instance variables
  177.             ivToServerStream = null ;       
  178.             ivFromServerStream = null ;       
  179.             ivClientSocket = null ;       
  180.  
  181.         return ;
  182.     }
  183. }
  184.