Skip to content
Snippets Groups Projects
Commit 97f0ccb5 authored by rojas's avatar rojas
Browse files

Update

parent adf33ded
No related branches found
No related tags found
No related merge requests found
......@@ -4,4 +4,16 @@
## Description
Modification to TCPExample4 to show the Message of the day (MOTD), depending on the day of the week.
The server side sets up a server listening for incoming connectios.
When a client attempts a connection to the server it sends the day of the week for the current connection.
Once received the day of the week, the server reply with a message according to the day of the week.
Phrases taken from
https://www.divein.com/everyday/monday-motivation-quotes/
......@@ -3,7 +3,6 @@ package MV3500Cohort2024JulySeptember.homework2.Romero;
import java.io.*;
import java.net.*;
import java.time.LocalDate;
import java.io.PrintWriter;
import java.time.DayOfWeek;
//import java.time.LocalTime; // conversion?
......@@ -30,8 +29,6 @@ public class RomeroClientHW2
{
// default constructor
}
// static String DESTINATION_HOST = "localhost";
// static int MAX_LOOP_COUNT = 4;
/**
* Program invocation, execution starts here
......@@ -44,128 +41,33 @@ public class RomeroClientHW2
LocalDate currentDate = LocalDate.now();
DayOfWeek day = currentDate.getDayOfWeek();
System.out.println("Current date: " + currentDate);
System.out.println("Day: " + day);
System.out.println("Today is: " + day + "\n");
System.out.println(RomeroClientHW2.class.getName() + " creating new socket ...");
try {
Socket clientConnectionSocket = new Socket("localhost", 2317);
try {
Socket clientConnectionSocket = new Socket("localhost", 2317);
in = new DataInputStream(clientConnectionSocket.getInputStream());
out = new DataOutputStream(clientConnectionSocket.getOutputStream());
in = new DataInputStream(clientConnectionSocket.getInputStream());
out = new DataOutputStream(clientConnectionSocket.getOutputStream());
//PrintWriter out = new PrintWriter(clientConnectionSocket.getOutputStream(), true);
out.writeUTF("New client has arrived!, conected on " + day);
out.writeUTF(day.name());
String mensaje = in.readUTF();
String mensaje = in.readUTF();
System.out.println(mensaje);
System.out.println(mensaje);
clientConnectionSocket.close();
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!");
} 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!");
}
}
}
// try {
// System.out.println(TcpExample4Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times");
// System.out.println("=======================================================");
//
// for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit
// {
// System.out.println(TcpExample4Client.class.getName() + " creating new socket #" + loopCount + "...");
//
// // We request an IP to connect to ("localhost") and
// // port number at that IP (2317). This establishes
// // a connection to that IP in the form of the Socket
// // object; the server uses a ServerSocket to wait for
// // connections.This particualar example is interacting
// // with what it expects is a server that writes a single text
// // line after 10 sec.
// long startTime = System.currentTimeMillis();
//
// // open a socket for each loop
// Socket socket = new Socket(DESTINATION_HOST, 2317);
//
// // Setup. Read the single line written by the server.
// // We'd do things a bit differently if many lines to be read
// // from the server, instead of one only.
// InputStream is = socket.getInputStream();
// Reader isr = new InputStreamReader(is);
// BufferedReader br = new BufferedReader(isr);
//
// String serverMessage = br.readLine(); // blocks
// long readTime = System.currentTimeMillis();
// long timeLength = readTime - startTime;
//
// System.out.println(TcpExample4Client.class.getName() + ": message received from server='" + serverMessage + "'");
// System.out.println(TcpExample4Client.class.getName() + ": time msec required for read=" + timeLength);
// System.out.println("=======================================================");
// // To push this further, launch multiple copies of TcpExample4Client simultaneously
// }
// System.out.println(TcpExample4Client.class.getName() + " complete");
// // main method now exits
// } catch (IOException e) {
// System.out.println("Problem with " + TcpExample4Client.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!");
// }
// }
// try {
// System.out.println(TcpExample4Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times");
// System.out.println("=======================================================");
//
// for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit
// {
// System.out.println(TcpExample4Client.class.getName() + " creating new socket #" + loopCount + "...");
//
// // We request an IP to connect to ("localhost") and
// // port number at that IP (2317). This establishes
// // a connection to that IP in the form of the Socket
// // object; the server uses a ServerSocket to wait for
// // connections.This particualar example is interacting
// // with what it expects is a server that writes a single text
// // line after 10 sec.
// long startTime = System.currentTimeMillis();
//
// // open a socket for each loop
// Socket socket = new Socket(DESTINATION_HOST, 2317);
//
// // Setup. Read the single line written by the server.
// // We'd do things a bit differently if many lines to be read
// // from the server, instead of one only.
// InputStream is = socket.getInputStream();
// Reader isr = new InputStreamReader(is);
// BufferedReader br = new BufferedReader(isr);
//
// String serverMessage = br.readLine(); // blocks
// long readTime = System.currentTimeMillis();
// long timeLength = readTime - startTime;
//
// System.out.println(TcpExample4Client.class.getName() + ": message received from server='" + serverMessage + "'");
// System.out.println(TcpExample4Client.class.getName() + ": time msec required for read=" + timeLength);
// System.out.println("=======================================================");
// // To push this further, launch multiple copies of TcpExample4Client simultaneously
// }
// System.out.println(TcpExample4Client.class.getName() + " complete");
// // main method now exits
// } catch (IOException e) {
// System.out.println("Problem with " + TcpExample4Client.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!");
// }
// }
}
}
package MV3500Cohort2024JulySeptember.homework2.Romero;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
......@@ -34,8 +33,7 @@ public class RomeroServerHW2
* @param args command-line arguments
*/
public static void main(String[] args)
{
{
DataInputStream in;
DataOutputStream out;
......@@ -57,23 +55,31 @@ public class RomeroServerHW2
clientConnectionSocket = serverSocket.accept(); // block! until connected
connectionCount++; // unblocked, got another connection
in = new DataInputStream(clientConnectionSocket.getInputStream());
out = new DataOutputStream(clientConnectionSocket.getOutputStream());
String messageFromClient = in.readUTF();
System.out.println(messageFromClient);
out.writeUTF("Hello world from SERVER, you are the client numer: " + connectionCount);
System.out.println("Client connected on: " + messageFromClient);
out.writeUTF("**//*///*/*/*/*/*a/a//a*/*a/*a/*a/*: ");
String messageOTD = "";
// TODO option for the student, provide initial message *to the client*
// that we are handing off to a dispatch thread... because that is polite behavior.
// Plenty of code in Example3, we instead let our proxy thread
// TcpExample4HandlerThread introduce itself to the client.
switch(messageFromClient) {
case "MONDAY" -> messageOTD = "\n\"Mondays are the start of the work week which offer new beginnings 52 times a year!\"\n \t- David Dweck";
case "TUESDAY" -> messageOTD = "\n\"May your Tuesday be like your coffee: strong, smooth, and full of warmth.\"\n \t- Unknown";
case "WEDNESDAY" -> messageOTD = "\n\"Wednesdays will always bring smiles for the second half of the week.\"\n \t- Anthony T. Hincks";
case "THURSDAY" -> messageOTD = "\n\"Thursday is a day to admit your mistakes and try to improve.\"\n \t- Byron Pulsifer";
case "FRIDAY" -> messageOTD = "\n\"Every Friday, I like to high five myself for getting through another week\non little more than caffeine, willpower, and inappropriate humor.\"\n \t- Anonymous";
case "SATURDAY" -> messageOTD = "\n\"Saturday is a time to enjoy the small things in life, and to look back on the week with gratitude.\"\n \t- Unknown";
case "SUNDAY" -> messageOTD = "\n\"Sunday clears away the rust of the whole week.\"\n \t- Joseph Addison";
default -> messageOTD = "\nError while getting the day of the week";
}
out.writeUTF("\n**********************************************************************\n"
+ messageOTD +
"\n\n**********************************************************************\n\n"
+ "You are the client numer: " + connectionCount);
System.out.println("=============================================================");
System.out.println(RomeroServerHW2.class.getName() + ".handlerThread created for connection #" + connectionCount + "...");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment