<!doctype html public "-//w3c//dtd html 4.0 transitional//en">

CS 476/576
Systems Programming
Fall 2003
Final Exam
Time 2 & 1/2 hours
Open Book & Notes

 

 

Name:                              
Login:

 

 


Question 1: (25 points)

 

Write two C programs, called Q1Server and Q1Client such that:

Ø      Q1Client has two sockets: a tcp client socket and a udp server socket.

Ø      Q2Server has two sockets: a tcp server socket and a udp client socket.

Ø      Both the tcp and the udp server sockets uses port number 12345.

Ø      Q1Client requires one argument, which is the machine name where the server is running. For example if Q1Server is running at “cash”, then we use:

% Q1Client cash

Ø      As soon as Q1Client connects to Q1Server, it reads from its udp socket the message sends by Q1Server. It then exits after displaying the received message.

As soon as Q1Server accepts the connection from Q1Client, it sends a datagram containing the message “HI” to the Q1Client. It then exits after sending the message.

NOTE: Please Do NOT write any include or error checking statements in your code.

Ansewer: Q1Server  &  Q1Client



Question 2: (25 points)

Consider the following “partial” Java program that creates a matrix of 25 push buttons arranged as 5 rows by 5 columns. The buttons are labeled as 0, 1, 2 … 24. The background of each button is randomly assigned by calling the newColor() method. One button is randomly chosen so that if the user clicks on that button, the button’s label changes to “winner”. If the user clicks on any other button, the button’s label changes to “looser”.  Each button is disabled after clicking on it.

Complete the code of the program.

public class LuckyMatrix extends JApplet {

 

 

  private static final Color[] colors = {

    Color.black, Color.blue, Color.cyan,

    Color.darkGray, Color.gray, Color.green,

    Color.lightGray, Color.magenta,

    Color.orange, Color.pink, Color.red,

    Color.white, Color.yellow

  };

  private static final Color newColor() {

    return colors[

      (int)(Math.random() * colors.length)

    ];

  }

 

 

 

 

  public static void main(String[] args) {

    Console.run(new LuckyMatrix(), 600, 600);

  }

} ///:~

 

Ansewer: LuckyMatrix


Question 3: (25 points)

 

Write two Java programs, called Q3Server and Q3Client such that:

Ø      Q3Client has two sockets: a tcp client socket and a udp server socket.

Ø      Q3Server has two sockets: a tcp server socket and a udp client socket.

Ø      The tcp server socket uses port number 12345. The udp server socket uses the port number of its connected tcp socket.

Ø      Q3Client requires no arguments, since we assume that the server runs on  “localhost”.

Ø      As soon as Q3Client connects to Q3Server, it reads from its udp socket the message sends by Q3Server. It then exits after displaying the received message.

Ø      As soon as Q3Server accepts the connection from Q3Client, it sends a datagram containing the message “HI” to the Q3Client. It then exits after sending the message.

NOTE: Please Do NOT write any import in your code.

Ansewer:  Q3Server  and  Q3Client


 


Question 4: (25 points)

What is the output of the following program:

 

public class Q4  extends Thread {

  int Item = 0;

  Consumer Cons;

  Producer Prod;

  class Producer extends Thread {

      public Producer() {

        System.out.println("construct Producer");

      }

      public synchronized void run() {

 

        while(true) {

            int sleepTime = (int)(Math.random() * 10000);

            try {

              sleep(sleepTime);

            } catch(InterruptedException e) {

            }

            System.out.println("Producer " + Item++ );

            Cons.interrupt();

        }

      }

  }

  class Consumer extends Thread {

      public Consumer() {

        System.out.println("construct Consumer");

      }

      public synchronized void run() {

        while(true){

           try {

             wait();

           } catch(InterruptedException e) {

           }

           System.out.println("Consumer " + Item-- );

        }

     }

  }


 

  public Q4() {

      System.out.println("construct Q4");

      start();

      System.out.println("Q4 Started");

  }

 

  public void run() {

      Prod = new Producer();

      Cons = new Consumer();

      Prod.start();

      System.out.println("Producer Started");

      Cons.start();

      System.out.println("Consumer  Started");

  }

 

  public static void main(String[] args) {

      new Q4();

  }

 

} ///:

 

 

Ansewer:

 

Output