Skip to content
Snippets Groups Projects
Commit c310477b authored by tjsus's avatar tjsus
Browse files

Assignment 1

parent 9b4662e5
No related branches found
No related tags found
No related merge requests found
package MV3500Cohort2024JulySeptember.homework1.Smith;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class HomeworkClient {
public HomeworkClient() {
// default constructor
}
public final static String LOCALHOST = "0:0:0:0:0:0:0:1";
public static void main(String[] args) {
Socket socket = null;
InputStream is;
Reader isr;
BufferedReader br;
PrintWriter pw;
String serverMessage;
int clientLoopCount = 0;
Scanner scanner = new Scanner(System.in);
try {
while (true) {
clientLoopCount++;
System.out.println(HomeworkClient.class.getName() + " creating socket...");
socket = new Socket(LOCALHOST, 2317);
System.out.println("Enter a number to send to the server:");
int numberToSend = scanner.nextInt();
// Send the number to the server
pw = new PrintWriter(socket.getOutputStream(), true);
pw.println(numberToSend);
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
serverMessage = br.readLine();
System.out.println("==================================================");
System.out.print("Client loop " + clientLoopCount + ": ");
System.out.println("now we're talking!");
System.out.println("The message the server sent was: '" + serverMessage + "'");
Thread.sleep(500l);
}
} catch (IOException | InterruptedException e) {
System.err.println("Problem with " + HomeworkClient.class.getName() + " networking:");
System.err.println("Error: " + e);
if (e instanceof java.net.BindException) {
System.err.println("*** Be sure to stop any other running instances of programs using this port!");
}
} finally {
try {
if (socket != null)
socket.close();
} catch (IOException e) {}
System.out.println();
System.out.println(HomeworkClient.class.getName() + " exit");
}
}
}
package MV3500Cohort2024JulySeptember.homework1.Smith;
import java.io.*;
import java.net.*;
public class HomeworkServer {
private static int runningTotal = 0; // Initialize running total
public HomeworkServer() {
// default constructor
}
public static void main(String[] args) {
try {
System.out.println(HomeworkServer.class.getName() + " has started...");
ServerSocket serverSocket = new ServerSocket(2317);
OutputStream os;
PrintStream ps;
InetAddress localAddress, remoteAddress;
int localPort, remotePort;
int serverLoopCount = 0;
while (true) {
try (Socket clientConnectionSocket = serverSocket.accept()) {
serverLoopCount++;
os = clientConnectionSocket.getOutputStream();
ps = new PrintStream(os);
// Read the number sent by the client
BufferedReader br = new BufferedReader(new InputStreamReader(clientConnectionSocket.getInputStream()));
int clientNumber = Integer.parseInt(br.readLine());
// Update the running total
int clientNumnerSqr = clientNumber * clientNumber;
runningTotal += clientNumber;
// Send back the updated total
ps.println("Client sent: " + clientNumber + ", client number squared was: "+ clientNumnerSqr+", Running total: " + runningTotal);
System.out.println("Client sent: " + clientNumber + ", Running total: " + runningTotal);
localAddress = clientConnectionSocket.getLocalAddress();
remoteAddress = clientConnectionSocket.getInetAddress();
localPort = clientConnectionSocket.getLocalPort();
remotePort = clientConnectionSocket.getPort();
System.out.print("Server loop " + serverLoopCount + ": ");
System.out.println(HomeworkServer.class.getName() + " socket pair showing host name, address, port:");
System.out.println(" (( " +
localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " +
remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))");
if (localAddress.getHostName().equals(localAddress.getHostAddress()) ||
remoteAddress.getHostName().equals(remoteAddress.getHostAddress()))
System.out.println(" note HostName matches address if host has no DNS name");
ps.flush();
}
}
} catch (IOException e) {
System.err.println("Problem with " + HomeworkServer.class.getName() + " networking: " + e);
if (e instanceof java.net.BindException) {
System.err.println("*** Be sure to stop any other running instances of programs using this port!");
}
}
}
}
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