Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/ciphercore/samples/ClientDemo.java


1   /* $Id: ClientDemo.java,v 1.4 2001/04/02 08:42:50 cvsbob Exp $ */
2   
3   /*
4    * ClientDemo.java, simple example of a ciphercore client.
5    * Copyright (C) 2001 Robert Bushman.
6    *
7    * I reserve the right to release this program under seperate license.
8    * If you require a special license grant contact Robert Bushman.
9    *
10   * This program is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU General Public License
12   * as published by the Free Software Foundation; either version 2
13   * of the License, or (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Public License
21   * along with this program; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
23   * 02111-1307, USA.
24   */
25  
26  package com.ciphercore.samples;
27  
28  import java.io.DataInputStream;
29  import java.io.DataOutputStream;
30  import java.io.IOException;
31  
32  import com.ciphercore.Client;
33  import com.ciphercore.ServerKeyException;
34  import com.ciphercore.UserPasswordException;
35  
36  public class ClientDemo {
37      
38      public static void main( String[] args ) throws IOException {
39          Client client = new Client( "com.ciphercore.samples.ChildDemo",
40                                      "localhost",
41                                      5943 );
42          try {
43              client.connect();
44          } catch( ServerKeyException e ) {
45              e.printStackTrace();
46              System.out.println( "Secure Connection Failed." );
47              System.out.println();
48              System.out.println( "Demonstrating insecure connection." );
49              System.out.println( "Don't try this at home." );
50              client.setServerHash
51                  ( e.getHostName(), e.getNewDigest().toString( 16 ) );
52              try {
53                  client.connect();
54              } catch( ServerKeyException f ) {
55                  f.printStackTrace();
56                  System.out.println( "Insecure Connection Failed." );
57              } catch( UserPasswordException f ) {
58                  f.printStackTrace();
59              }
60          } catch( UserPasswordException e ) {
61              e.printStackTrace();
62          }
63          commDemo( client );
64      }
65      
66      public static void commDemo( Client client ) {
67          System.out.println( "Entering Comm Demo" );
68          try {
69              DataInputStream in =
70                  new DataInputStream( client.getBaseInputStream() );
71              DataOutputStream out =
72                  new DataOutputStream( client.getBaseOutputStream() );
73              
74              String testString = "This is Major Tom to ground control.";
75              
76              out.writeInt( 6400 );
77              out.writeInt( testString.getBytes().length );
78              out.write( testString.getBytes() );
79              out.writeInt( 3200 );
80              out.flush();
81              
82              System.out.println( "int 1: " + in.readInt() );
83              int testStringLength = in.readInt();
84              System.out.println( "Test String Length: " + testStringLength );
85              byte[] testStringBytes = new byte[ testStringLength ];
86              in.readFully( testStringBytes );
87              System.out.println
88                  ( "Test String: " + new String( testStringBytes ) );
89              System.out.println( "int 2: " + in.readInt() );
90          } catch( IOException e ) {
91              e.printStackTrace();
92              System.exit( 1 );
93          }
94      }
95  }
96