lundi 30 avril 2018

Knock knock conversation between server and client , it terminates the connection after I submit the UID

This is part of my code, the problem is while, and if loops. It is like a knock-knock conversation between a client and a server. the server receives the UID from the user to allow him to send knock-knock joke. if the client submits incorrect UID, there should be a loop to iterates until UID in the same form (G#########)is received.

  add( enterField, BorderLayout.NORTH );

  displayArea = new JTextArea(); // create displayArea
  add( new JScrollPane( displayArea ), BorderLayout.CENTER );

  setSize( 300, 150 ); // set size of window
  setVisible( true ); // show window
  } // end Server constructor

  // set up and run server
  public void runServer()
  {
  try // set up server to receive connections; process connections
  {
      server = new ServerSocket( 12345, 100 ); // create ServerSocket //change the addresses
  while ( true )
  {
  try
  {
  waitForConnection(); // wait for a connection
  getStreams(); // get input & output streams
  processConnection(); // process connection
  } // end try
  catch ( EOFException eofException )
  {
  displayMessage( "\nServer terminated connection" );
  } // end catch
  finally
  {
  closeConnection(); // close connection
  ++counter;
  } // end finally
  } // end while
  } // end try
  catch ( IOException ioException )
  {
  ioException.printStackTrace();
  } // end catch
  } // end method runServer

  // wait for connection to arrive, then display connection info
  private void waitForConnection() throws IOException
  {
 displayMessage( "Waiting for connection\n" );
  connection = server.accept(); // allow server to accept connection
  displayMessage( "Welcome to the Knock-Knock Server. Please submit your ID ");
 // &&&&&
  } // end method waitForConnection

           // get streams to send and receive data
           private void getStreams() throws IOException
           {
           // set up output stream for objects
               output = new ObjectOutputStream( connection.getOutputStream() );
               output.flush(); // flush output buffer to send header information
           // set up input stream for objects
               input = new ObjectInputStream( connection.getInputStream() );
           displayMessage( "\nGot I/O streams\n" );
           }

           // process connection with client
           private void processConnection() throws IOException
           {
               BufferedReader in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));  //receives from the client
                PrintWriter out = new PrintWriter(connection.getOutputStream(), true); //sends to the client 

           String message = "Welcome to the Knock-Knock Server. Please submit your UIDs";
           sendData( message ); // send connection successful message
           String  clientMessage = null;
           String  ServerMessage1 = null;
           // enable enterField so server user can send messages
           setTextFieldEditable( true );

           do // process messages sent from client
           {
           try // read message and display it
           {
             //  clientMessage = ( String ) input.readObject(); // read new message
             //displayMessage( "\n" + clientMessage ); // display message

                String GID= null;


               ServerMessage1 = "Please submit your G-number"; 
            sendData( ServerMessage1 );
            GID = input.readLine();

            String regex= "^[G-g]{1}[0-9]{8}$";

            while (!GID.matches(regex))
            {
            if (GID.equalsIgnoreCase("shutdown"))
            {closeConnection();
            }
        if (GID.isEmpty())
            { ServerMessage1 = "The field should not be Empty.";
            sendData( ServerMessage1 );}
        else 
        {ServerMessage1 = "Incorrect identifier; please submit again";
        sendData( ServerMessage1 );
         GID = (String) input.readLine();

        }

        }//end while 
        String ServerReply1 =  null;
        String Jokeinput ;
        Jokeinput = (String) input.readObject();
        while (!Jokeinput.equalsIgnoreCase("shutdown"))
        {
            if ( Jokeinput.equalsIgnoreCase("Knock Knock") || Jokeinput.equalsIgnoreCase("Knock, Knock") || Jokeinput.equalsIgnoreCase("Knock-Knock" ))
            {
                ServerReply1 = "Who's there?";
                        sendData( ServerReply1 );
                        Jokeinput = (String) input.readObject();
                        if (!Jokeinput.isEmpty())
                        {
                            ServerReply1= Jokeinput.concat("who?");
                                    sendData( ServerReply1 );
                                    Jokeinput = (String) input.readObject();
                                    if (!Jokeinput.isEmpty())
                                    { ServerReply1= "Nice Joke!";}      
                        }}
                        else
                        {
                            ServerReply1="unKnown response";
                            sendData( ServerReply1 );
                        }


            }}



           catch ( ClassNotFoundException classNotFoundException )
           {
           displayMessage( "\nUnknown object type received" );
           } // end catch

           } while ( !message.equals( "CLIENT>>> shutdown" ) );
           } // end method processConnection

           // close streams and socket



           private void closeConnection()
           {
           displayMessage( "\nTerminating connection\n" );
           setTextFieldEditable( false ); // disable enterField

           try
           {
               output.close(); // close output stream
               input.close(); // close input stream
               connection.close(); // close socket

           } // end try

           catch ( IOException ioException )
            {
            ioException.printStackTrace();
            } // end catch
            } // end method closeConnection




           private void sendData( String message )
            {
            try // send object to client
            {
                output.writeObject( "SERVER>>> " + message );
                output.flush(); // flush output to client

            displayMessage( "\nSERVER>>> " + message );
            } // end try
            catch ( IOException ioException )
            {
            displayArea.append( "\nError writing object" );
            } // end catch
            } // end method sendData

            // manipulates displayArea in the event-dispatch thread


           private void displayMessage( final String messageToDisplay )
            {
            SwingUtilities.invokeLater(
            new Runnable()
            {

Aucun commentaire:

Enregistrer un commentaire