A Simple Client / Server                                             P Komisar
 


//********************************************************************************

// A client streams a password from the command line to a Password_Server   P.Komisar

//********************************************************************************

import java.io.*;
import java.net.*;

class ID_check{

    public static void main(String a[]) throws IOException {
        if(a.length!=1){
        System.out.println("\n\n  Enter your password after java ID_check \n");
        System.exit(0);
        }
        Socket socket;
        BufferedReader in;
        PrintWriter out;

        socket = new Socket("localhost",7777);

        // streams on socket
        in = new BufferedReader( new InputStreamReader(socket.getInputStream()) );
        out = new PrintWriter( socket.getOutputStream() );
         System.out.println("\n");
        String fromServer1 = in.readLine();
        System.out.println(fromServer1 +"\n");

        out.println(a[0]);
        out.flush();

        String fromServer2 = in.readLine();
        System.out.println(fromServer2+"\n");

        socket.close();
        }
      }


//************************************************************************

//  a network server that checks passwords against a preselected group       P.Komisar

//************************************************************************

import java.io.*;
import java.net.*;

class Password_Server {
    public static void main(String a[]) throws IOException {
        int queue=5;
        int port = 7777;
        int hits=1;
        int i,k;
        PrintStream out;
        BufferedReader in;
        Socket socket;
        String request="   Authenicating...";

        ServerSocket servsock = new ServerSocket(port, queue);
         System.out.println("Server is running...");
    while (true) {

        // wait for the next client connection
 socket=servsock.accept();

        // Get I/O streams from the socket
        out = new PrintStream( socket.getOutputStream() );
 InputStreamReader isr  = new InputStreamReader( socket.getInputStream() );
 in  = new BufferedReader( isr );
        System.out.println("Server hit " + hits++);
        // Send our query
        out.println(request);
        out.flush();

        // receive password to test
        String ID = in.readLine();

   String[] string_array=new String[] {"B52", "DC7", "Aero","Sopwith","Spitfire" };
   boolean b=false;
   for(k=0,i=0;i<string_array.length;i++,k++){

   if(string_array[i].equals(ID)){
      out.println("   " + ID + " is a valid password ");
      b=true;
      break;
      }
   }

     if((k==string_array.length) && (b==false))
      out.println("   Wrong password. Try again ");
      //close spawned socket
    socket.close();
     }
  }
}