you’re going to create two Java classes: A Server class and a Client class. The
ID: 3674422 • Letter: Y
Question
you’re going to create two Java classes: A Server class and a Client class. The server will run first, set up a local connection, and wait for the client. The client will then connect to the server and send some data to be processed. The server will work as a conversion app. The client will ask for the user to enter a temperature in the form “80F” or “16C” to be converted. Note that the client should be able to enter either Fahrenheit or Celsius temperatures. Invalid entries (being anything not in the format “xF” or “xC” where x is any real float value) should be turned down by the client and the program should ask for new entry until the user enters a valid input. The server should then receive the user’s entry by any means and convert the given temperature into its counterpart, sending the converted temperature back to the client. The formula for converting temperatures can be found below.
When writing this program, here’s some things to keep in mind:
The nature of the app is to collect an entry from the user, send the data to the server, have the server convert the data, send the data back to the client, and display the converted data to the user. The program should run through only once, and both the client and server should terminate once finished. Keep in mind that it’s possible for either the server or the client to terminate while the other is sending data – if this happens it’ll result in a Connection Reset error. If you encounter this error, make sure that both your classes stay connected beyond sending data.
For this assignment, the program must be entirely console-based. No GUI applications or stand-alone executables will be accepted for this submission.
Explanation / Answer
Comments added
ServerAdmin.java
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerAdmin {
//declaring server socket variable
private static ServerSocket server;
//port definining
private static int port = 9876;
public static void main(String args[]) throws IOException, ClassNotFoundException{
//creating server socket object .. with port
server = new ServerSocket(port);
while(true){
System.out.println("Request client waiting...");
//connection creation .. and wait for client
Socket socket = server.accept();
//reading client message
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
double temp = (double) inputStream.readObject();
double result = 0.0;
//calculating temperature
if(temp > 100){
double celsius = (temp-32)/1.8;
result = celsius;
System.out.println("Converted Temperature: " + celsius+" C");
}
else{
double fahrenheit = (temp*1.8) + 32;
result = fahrenheit;
System.out.println("Converted Temperature: " + fahrenheit+" F");
}
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
//sending message to client
outputStream.writeObject("Calculated Temperature "+result);
//close objects
inputStream.close();
outputStream.close();
socket.close();
if(message.equalsIgnoreCase("exit")) break;
}
//closing server
server.close();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
clientAdmin.java
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class clientAdmin {
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{
//object variables declaring
InetAddress host = InetAddress.getLocalHost();
Socket datasocket = null;
ObjectOutputStream outputStream = null;
ObjectInputStream inputStream = null;
for(int i=0; i<5;i++){
//making connection .. with same port number
datasocket = new Socket(host.getHostName(), 9876);
//creating and writing data using ObjectOutputStream
outputStream = new ObjectOutputStream(datasocket.getOutputStream());
System.out.println("Temperature sending to server for conversion");
if(i==4)outputStream.writeObject("exit");
else outputStream.writeObject(""+(100+i));
//read the server response message
inputStream = new ObjectInputStream(datasocket.getInputStream());
double convertedTemp = (double) inputStream.readObject();
System.out.println("converted Temperature: " + convertedTemp);
//close connections
inputStream.close();
outputStream.close();
Thread.sleep(100);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.