Download wrl0001.tmp

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS 476/576
Systems Programming
Fall 2002
Final Exam
Time 2 & 1/2 hours
Open Book & Notes
Name:
Login:
NOTE: In order to make the programs short, you may not write any include,
comments or error statements.
Question 1: (25 points)
Consider the next program ButtonsQ.java that creates 12 buttons.
public class ButtonsQ extends JFrame {
ArrayList buttons = new ArrayList();
public ButtonsQ() {
for(int i = 0; i < 64; i++)
buttons.add(new JButton("Button " + i));
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
for(int i = 0; i < buttons.size(); i++)
cp.add((JButton)buttons.get(i));
}
public static void main(String[] args) {
JFrame frame = new ButtonsQ();
frame.setSize(400,150);
frame.setVisible(true);
}
}
1
Modify this program:
1. To arrange the buttons in 8 rows and 8 columns and the label of each button is “(r,c)”
where r and c the row and column where the button is located.
2. If the user clicks at any button, the program exits.
Solution (Q1)
2
3
Question 2: (25 points)
Write a Java program called “Command.java “ that has an interface with two components: An
input TextLine and a quit Button. The program executes any command entered by the user in the
TextLine (by typing the command followed by the Enter key) . During the execution of the
command, both the TextLine and the quit button becomes insensitive. The program exits by
clicking on the quit button ( no popup dialogue conformation is required, so just click on quit to
exit).
Solution (Q2)
4
5
Question 3: (25 points)
Write a C program that creates two server sockets, one is a tcp and the other is a udp and
the program will have only two clients, one is atcp client and the other is a udp client (see
Question 4 for the details of these two clients).
When the tcp client is connected, the server socket is closed since there is no more tcp
clients are expected to connect to this server. When the first udp message arrives from the
udp client, it is forwared to the tcp client. After that the program forks a child process that
continuously reads any message from the tcp socket and forwards it to the udp socket.
The parent process will do the reverse, i.e., it continuously reads any message from the udp
socket and forwards it to the tcp socket. We may call this program: “TcpUdpAdapter”
because it helps a tcp client to communicate with a udp client. For the purpose of this
question, assume that both the tcp and the udp sockets are binded to the same port number
1234.
Solution (Q3)
6
7
Question 4: (25 points)
The following program is called “tcpChatClient.java” creates two threads, one thread reads the any
message typed on the keyboard and sends it the tcp socket while the other thread reads any
message that arrives from the tcp socket and displays it to the tty. The program has no command
line arguments since it assumes the server runs at “localhost” and uses port number 1234.
You are required to write another similar program called “udpChatClient.java” that uses a udp
socket instead of a tcp socket. The two programs may interact with each other via the
TcpUdpAdapter program developed in of Question 3 . You may use any method of Dgram.class.
class tcpSocketRead extends Thread {
private Socket socket;
private BufferedReader in;
public tcpSocketRead(Socket s)
throws IOException {
socket = s;
in = new BufferedReader( new InputStreamReader(
socket.getInputStream()));
start();
}
public void run() {
String str;
try {
while((str = in.readLine()).length() != 0) {
System.out.println(str);
}
} catch (IOException e) { }
}
}
class tcpSocketWrite extends Thread {
private Socket socket;
private PrintWriter out;
private BufferedReader stdin;
public tcpSocketWrite(Socket s)
throws IOException {
socket = s;
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter( socket.getOutputStream())), true);
stdin =
new BufferedReader(new InputStreamReader(System.in));
start();
}
public void run() {
String str;
try {
while((str = stdin.readLine()).length() != 0)
out.println(str);
} catch (IOException e) { }
}
}
8
public class tcpChatClient {
static final int PORT = 1234;
public static void main(String[] args) throws IOException {
InetAddress addr = InetAddress.getByName(null);
Socket socket = new Socket(addr, PORT);
try {
new tcpSocketRead(socket);
new tcpSocketWrite(socket);
} catch(IOException e) {
socket.close();
}
}
}
Solution (Q4)
9
10