Professional Documents
Culture Documents
Answer: A multi-threaded server creates a new thread to handle each client connection that it
accepts. A server program is generally designed to process connection requests from many clients.
It runs in an infinite loop in which it accepts a connection request and processes it. If the
processing takes a significant amount of time, it's not a good idea to make the other clients wait
while the current client is processed. The solution is for the server to make a new thread to handle
each client connection. The server can continue to accept other client connections even while the
first client is being serviced.
Page 1|5
6. What is the used of try and catch in java programming? Handle exception. Try capture the
exception and catch define the processes to perform in case the exception is appear while
running the program 3mrks
7. What is a deadlock? Give an illustration 2mrks
Section B: Practice
Question 3: 10 marks
1. What is the IDE (name + JDK version) used in class for java network programming? Eclipse
JavaSE - 15 1mrk
2. Explain how to setup that environment. Install the JDK unzip eclipse zipped file lunch the
exe file having the eclipse logo (no need to install eclipse) 1mrk
3. Describe the process involved in writing a java class from edition to the execution. 3mrks
Create a new java project create a class in the created java project edit the code of the
class making sure that it contains a main classbuild and run.
4. Write a java class which ask for the name of and print out hello followed by that name. 5mrks
import java.util.Scanner;
public class sayHello {
Page 3|5
static String name;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
System.out.println("We need your name for configuration purpose");
name = keyboard.next();
System.out.println("Hello "+ name);
}
}
Question 4: 20marks
Complete the following programs
1. To handle a client server communication:
a. Why is better to separate into two different processes? The client and the server have
different needs and therefore have to be treated separately
1mrk
b. Which one is to be handle first? The client or the server? Justify
i. the server because it is the one providing the services. The client simply send
queries 1.5mrks
2. What are the steps involved in the setting up of a server process using TCP socket? 2.5mrks
a. Create a ServerSocket object.
b. Put the server into a waiting state.
c. Set up input and output streams.
d. Send and receive data.
e. Close the connection (after completion of the dialogue).
3. Explain the following codes: 4mrks
a. Scanner input = new Scanner(link.getInputStream());
b. PrintWriter output = new PrintWriter(link.getOutputStream(),true);
They are used to setup input and output streams for the communication
4. Identify the step to which they belong. Step 3 Set up input and output streams
1mrk
5. Describe the steps involved in the settings of a client using TCP socket 4mrks
a. Establish a connection to the server: create a Socket object, supplying
b. Set up input and output streams
c. Send and receive data.
d. Close the connection
6. Modify the code below for it to match with the implementation of a sever using UDP socket
5mrks
7. What does the codes?
1mrk
//Server that echoes back client's messages.
//At end of dialogue, sends message indicating number of
//messages received. Uses datagrams.
import java.io.*;
import java.net.*;
public ____class___ UDPEchoServer{
private static final int PORT = 1234;
private static DatagramSocket datagramSocket;
private static DatagramPacket inPacket, outPacket;
Page 4|5
private static byte[] buffer;
public static void ____main_____(String[] args){
System.out.println("Opening port…\n");
try{
datagramSocket = new ______ DatagramSocket____________(PORT); //Step 1.
}
catch(SocketException sockEx){
System.out.println("Unable to open port!");
System.exit(1);
}
handleClient();
}
private static void handleClient(){
try{
String messageIn,messageOut;
int numMessages = 0;
InetAddress clientAddress = null;
int clientPort;
do{
buffer = new byte[____256____]; //Step 2.
inPacket =new ___ DatagramPacket____(buffer, buffer.length);//Step 3.
datagramSocket.receive(_____inPacket__________);//Step 4.
clientAddress = inPacket.getAddress();//Step 5.
clientPort = inPacket.______ getPort______________();//Step 5.
messageIn =new String(inPacket.getData(), 0,inPacket.getLength());
//Step 6.
System.out.println("Message received.");
numMessages++;
messageOut = "Message " + numMessages + ": " + messageIn;
outPacket = new DatagramPacket(messageOut.___ getBytes___(),
messageOut.length(),clientAddress,
clientPort); //Step 7.
_ datagramSocket__.send(outPacket); //Step 8.
}while (true);
}
catch(IOException ioEx){
ioEx.printStackTrace();
}
finally //If exception thrown, close connection.
{
System.out.println( "\n* Closing connection… *");
datagramSocket.__________close_______(); //Step 9.
}
}
}
GOOD LUCK
Page 5|5