Your grade on lab 11 is: 100

Deductions:

None, great job.


If you have any comments or questions, please speak with me personally or contact me via e-mail at lfallow@clemson.edu.  If I am unable to satisfy your concerns, please contact Miss Sarah Matzko.

Your code:

import java.net.*;
import java.io.*;
import java.util.Scanner;

class SynChat 
{

   // "sender" is responsible for sending locally-typed text over the network.
   // "receiver" is responsible for receiving network-send text and displaying 
   //    it locally.
   private CommThread sender, receiver;
   private ServerSocket serverSocket;
   private Socket clientSocket;

   // Server Socket. After obtaining the client Socket, call setup.
   public SynChat (int port) throws IOException
   {
      
      setup(new ServerSocket(port).accept());
         
   }
   
   // Client Socket. After creating the client Socket, call setup.
   public SynChat (String server, int port) throws UnknownHostException, 
                                                          IOException 
   {
      setup(new Socket(server, port));
   }

   public void setup (Socket clientSocket) throws IOException 
   {

      // Initialize appropriately. localIn and localOut use standard IO.
      Scanner localIn = new Scanner(System.in);
      Scanner networkIn = new Scanner(clientSocket.getInputStream());

      PrintStream localOut = new PrintStream(System.out);
      PrintStream networkOut = new PrintStream(clientSocket.getOutputStream(), true);
      TurnMonitor turn = new TurnMonitor();
      //setting up threads
      sender   = new CommThread (localIn, networkOut, turn);
      receiver = new CommThread (networkIn, localOut, turn);
      
      // make both threads begin execution
      sender.start();
      receiver.start();

   }



   public static void main (String args[]) 
   {
      //if there is a command line argument then this is the client
      if (args.length > 0) 
      { 
         try 
         { 
            new SynChat (args[0], 4444);
         } 
         catch (UnknownHostException e) 
         {
            System.err.println ("Can't connect to server " + args[0] + e);
         } 
         catch (IOException e) 
         {
            System.err.println ("Could not connect to port 4444 on server: "
                                + args[0] + ": " + e);
         }
      } 
      //no command line argument means this is the server
      else
      {
         try 
         {
            new SynChat (4444); 
         } 
         catch (IOException e) 
         {
            System.err.println ("Could not connect to port 4444: " + e);
         }
      }
   }
}  

//  monitor class
class TurnMonitor
{
     //class to force taking turns
     public synchronized void takeTurn()
     {
        notify();
        try
        {
           wait();
        }
        catch(InterruptedException e)
        {
           System.err.println("Encountering exception" + e);
        }
     }
}


//Thread class
class CommThread extends Thread 
{
   private Scanner in;
   private PrintStream out;
   private boolean keepGoing;
   private TurnMonitor turn;

   //constructor
   public CommThread (Scanner in, PrintStream out, TurnMonitor turn) 
   {
      this.in = in;
      this.out = out;
      this.keepGoing = true;
      this.turn = turn;
   }
   
   //when start() is called it starts the run method
   public void run () 
   {
      while (keepGoing) 
      {
         // "QUIT" will need to be send both directions to end both threads in 
         //    both running sessions.
        String line = in.nextLine();
        out.println(line);
        keepGoing = !line.equals("QUIT");


        //This makes the quit work again
        if (!keepGoing)
        {
          System.exit(0);
        }
         turn.takeTurn();       
      }   
   }
}	

