Something went wrong on our end
RomeroClientHW2.java 3.05 KiB
package MV3500Cohort2024JulySeptember.homework2.Romero;
import java.io.*;
import java.net.*;
import java.time.LocalDate;
import java.time.DayOfWeek;
//import java.time.LocalTime; // conversion?
/**
* This client program establishes a socket connection to the {@link RomeroServerHW2},
* then checks how long it takes to read the single line it expects as a server response.
*
* @author Don McGregor
* @author Don Brutzman
* @author MV3500 class
*/
public class RomeroClientHW2
{
/**
* Default constructor
*/
public RomeroClientHW2() { }
/**
* Program invocation, execution starts here
* @param args command-line arguments
*/
public static void main(String[] args) {
/**
* Using DataInputStream and DataOutputStream to convert bytes from an
* input stream and converting then into Java's primitive data and vice versa
* respectively *
*/
DataInputStream in;
DataOutputStream out;
/**
* Getting the current date from the client, and getting the
* specific day of the week.
*/
LocalDate currentDate = LocalDate.now();
DayOfWeek day = currentDate.getDayOfWeek();
//Cheching the day of the week obtined
System.out.println("Current date: " + currentDate);
System.out.println("Today is: " + day + "\n");
System.out.println(RomeroClientHW2.class.getName() + " creating new socket ...");
try {
Socket clientConnectionSocket = new Socket("localhost", 2317);
/**
* Creating a DataInputStream and DataOutputStream objects that reads data
* from the input stream of the socket connection and writes data through
* a socket connection, accordingly
*/
in = new DataInputStream(clientConnectionSocket.getInputStream());
out = new DataOutputStream(clientConnectionSocket.getOutputStream());
//Specifying the daY of the week, in string format
out.writeUTF(day.name());
/**
* Reading and printing strings from the server:
* message1 reads the number of client
* message2 reads the MOTD
*/
String message1 = in.readUTF();
String message2 = in.readUTF();
System.out.println(message1);
System.out.println(message2);
clientConnectionSocket.close();
} catch (IOException e) {
System.out.println("Problem with " + RomeroClientHW2.class.getName() + " networking:networking:"); // describe what is happening
System.out.println("Error: " + e);
// Provide more helpful information to user if exception occurs due to running twice at one time
if (e instanceof java.net.BindException) {
System.out.println("*** Be sure to stop any other running instances of programs using this port!");
}
}
}
}