Please help needed to complete assignment:
For this question I need to develop a server which either returns the time of day in hours
and minutes when the client sends a message to it, or echoes back the message that was sent. The
server should also respond to a quit message from the client. I have been provided with a simple client
class named Client in a project (attached below).
I have two tasks. First to read the code within the client class and understand what is happening;
especially the method actionPerformed. Note that the client has three buttons which are
used to communicate with the server.
Next, develop a class Server which interacts with the client and either sends it the time of day
or echoes the message that was sent, depending on what service the client requests. There are a
number of important points to notice about this question.
•It would be very useful if you have completed Exercise 2.3 (suppliedon request ) before doing this question.
•Try and make your code have the same structure as that of Exercise 2.3 (supplied on request). I may
borrow a large amount of code from this project. We shall not penalise you if you do!
•When the client quits, the server should close down.
The code that creates the current time is
Calendar c = Calendar.getInstance();
int hours = c.get(Calendar.HOUROFDAY);
int minutes = c.get(Calendar.MINUTE);
where the class Calendar can be found in java.util (don’t forget to import this). You will also find the method substring found in the class String useful when developing the server code.
Thanks in advance
CLIENT CODE:
import java.awt.;
import java.awt.event.;
import java.io.;
import java.net.;
//Note that there is no error processing in this client
//Note, also that the important part of the code is in actionPerformed
//The interface is not the most elegant: the buttons are long!
public class Client extends Frame implements ActionListener{
private Button timeButton; //Button pressed to get the current time
private Button echoButton; //Button for echoing the text sent
private Button quitButton; //Button for quitting the client, click this to exit.
//When this button has been clicked the client will exit
//and the server will close down
private TextField toBeSent; //Text to be sent for echoing
private TextField displayBox; //Text sent from server displayed here
//Network and stream objects
private Socket s;
private InputStream is;
private BufferedReader bf;
private OutputStream os;
private PrintWriter pw;
public Client(String title) throws IOException{
//Create a window
super(title);
//Create visual objects
timeButton = new Button("Time"); //When clicked asks for the time
echoButton = new Button("Echo"); //When clicked will echo text
quitButton = new Button("Quit"); //When clicked will quit the client and shit down the server
toBeSent = new TextField(36); //No more than 36 characters to be sent
displayBox = new TextField(36); //No more than 36 characters to be displayed
//Set layout manager
this.setLayout(new GridLayout(7,1));
//Add seven visual objects in rows (includes labels)
add(new Label("Message to be sent"));
add(toBeSent);
add(new Label("Message from server"));
add(displayBox);
add(timeButton);
add(echoButton);
add(quitButton);
//Register the window to listen to the button clicks
timeButton.addActionListener(this);
echoButton.addActionListener(this);
quitButton.addActionListener(this);
//Set up network connections
s = new Socket("127.0.0.1",1024); //Uses port 1024 and student pc, server must use this port
is = s.getInputStream();
bf = new BufferedReader(new InputStreamReader(is));
os = s.getOutputStream();
pw = new PrintWriter(os, true);
}
public void actionPerformed(ActionEvent ae){ //Executed when one of the buttons is clicked
//Gets the label of the button that has been clicked
String buttonLabel = ae.getActionCommand();
//Code uses simple application protocol of single upper case letter
//either E, Q or T
try{
if(buttonLabel.equals("Time")){
//Clear the toBeSent box
toBeSent.setText("");
//Asks for the time from server
pw.println("T");
//Gets the time returned from the server
String time = bf.readLine();
//Displays it
displayBox.setText(time);
}
if (buttonLabel.equals("Echo")){
//Server needs to echo text
//Send echo text preceded by E
pw.println("E"+toBeSent.getText());
//Read echoed text sent back from server and display it
String echoText = bf.readLine();
displayBox.setText(echoText);
}
if (buttonLabel.equals("Quit")){
//Client quits, send quit command to server
pw.println("Q");
System.exit(0); //Collapses the window
}
}
catch(IOException io){System.out.println("Problems with client "+ io);}
}
public static void main(String[] args){
try{
//Set up window
Client cl = new Client("TMA02 client");
//Size of window 400 x 200 pixels
cl.setSize(400,200);
//Make the window visible
cl.setVisible(true);
}catch(IOException io){System.out.println("Problem setting up client");}
}
}
SERVER CODE (not full - the one i need to fill in):
import java.net.;
import java.io.;
import java.util.*; //For Calendar
public class Server {
public static void main(String[] args){
//First set up all the network objects, don't forget to use port 1024
while(true){
// Read a line from the client
// Decode the line and carry out the processing required
// This could be the time of day, an echo or quit, the latter
// quits the loop
}
}
}
Please let me know if you need the code of the mentioned exersise code.
Tanx ;-)
Enter your message below
Sign in or Join us (it's free).