Skip to content
Snippets Groups Projects
Commit 7097ba25 authored by ethanjwilliams's avatar ethanjwilliams
Browse files

Williams - Homework #1

parent 6062cb7d
No related branches found
No related tags found
No related merge requests found
source diff could not be displayed: it is too large. Options to address this: view the blob.
......@@ -114,7 +114,7 @@ manifest.custom.codebase=
manifest.custom.permissions=
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
platform.active=JDK_22
project.licensePath=../license.txt
run.classpath=\
${javac.classpath}:\
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>Networked Graphics MV3500 assignments</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
<spellchecker-wordlist xmlns="http://www.netbeans.org/ns/spellchecker-wordlist/1">
<word>accessor</word>
<word>autogeneration</word>
<word>classpath</word>
<word>deliverables</word>
<word>docx</word>
<word>https</word>
<word>interarrival</word>
<word>javadoc</word>
<word>localhost</word>
<word>multicast</word>
<word>Netbeans</word>
<word>netcat</word>
<word>NPS</word>
<word>pdu</word>
<word>README</word>
<word>simkit</word>
<word>simulationists</word>
<word>SISO</word>
<word>subpackage</word>
<word>teardown</word>
<word>txt</word>
<word>UML</word>
<word>unicast</word>
<word>wikipedia</word>
<word>Wireshark</word>
</spellchecker-wordlist>
</configuration>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>Networked Graphics MV3500 assignments</name>
<explicit-platform explicit-source-supported="true"/>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
<spellchecker-wordlist xmlns="http://www.netbeans.org/ns/spellchecker-wordlist/1">
<word>accessor</word>
<word>autogeneration</word>
<word>classpath</word>
<word>deliverables</word>
<word>docx</word>
<word>https</word>
<word>interarrival</word>
<word>javadoc</word>
<word>localhost</word>
<word>multicast</word>
<word>Netbeans</word>
<word>netcat</word>
<word>NPS</word>
<word>pdu</word>
<word>README</word>
<word>simkit</word>
<word>simulationists</word>
<word>SISO</word>
<word>subpackage</word>
<word>teardown</word>
<word>txt</word>
<word>UML</word>
<word>unicast</word>
<word>wikipedia</word>
<word>Wireshark</word>
</spellchecker-wordlist>
</configuration>
</project>
package MV3500Cohort2024JulySeptember.homework1.Williams;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public Client() {
// default constructor
}
public final static String LOCALHOST = "127.0.0.1"; // localhost IP address
public static void main(String[] args) {
Socket socket = null;
int clientLoopCount = 0;
Scanner scanner = new Scanner(System.in);
try {
while (true) {
clientLoopCount++;
System.out.println(Client.class.getName() + " creating socket...");
socket = new Socket(LOCALHOST, 2317);
System.out.println("Enter a number to send to the server (or type 'exit' to quit):");
String input = scanner.next();
if ("exit".equalsIgnoreCase(input)) {
break;
}
int numberToSend;
try {
numberToSend = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid number.");
continue;
}
// Send the number to the server
try (PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
pw.println(numberToSend);
String 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 + "'");
} catch (IOException e) {
System.err.println("Error sending/receiving data: " + e.getMessage());
}
Thread.sleep(500L);
}
} catch (IOException | InterruptedException e) {
System.err.println("Problem with " + Client.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 {
if (socket != null && !socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {
System.err.println("Error closing socket: " + e.getMessage());
}
}
scanner.close();
System.out.println();
System.out.println(Client.class.getName() + " exit");
}
}
}
package MV3500Cohort2024JulySeptember.homework1.Williams;
import java.io.*;
import java.net.*;
public class Server {
private static int runningTotal = 0; // Initialize running total
public Server() {
// default constructor
}
public static void main(String[] args) {
try {
System.out.println(Server.class.getName() + " has started...");
ServerSocket serverSocket = new ServerSocket(2317);
int serverLoopCount = 0;
while (true) {
try (Socket clientConnectionSocket = serverSocket.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(clientConnectionSocket.getInputStream())); PrintStream ps = new PrintStream(clientConnectionSocket.getOutputStream())) {
serverLoopCount++;
// Read the number sent by the client
String input = br.readLine();
if (input == null) {
System.err.println("Received null input, skipping this client.");
continue;
}
int clientNumber;
try {
clientNumber = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.err.println("Invalid input received: " + input + ". Error: " + e.getMessage());
ps.println("Invalid number format: " + input);
continue;
}
// Update the running total
int clientNumberSqr = clientNumber * clientNumber;
runningTotal += clientNumber;
// Send back the updated total and squared number
ps.println("Client sent: " + clientNumber + ", client number squared was: " + clientNumberSqr + ", Running total: " + runningTotal);
System.out.println("Client sent: " + clientNumber + ", Running total: " + runningTotal);
InetAddress localAddress = clientConnectionSocket.getLocalAddress();
InetAddress remoteAddress = clientConnectionSocket.getInetAddress();
int localPort = clientConnectionSocket.getLocalPort();
int remotePort = clientConnectionSocket.getPort();
System.out.print("Server loop " + serverLoopCount + ": ");
System.out.println(Server.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");
}
} catch (IOException e) {
System.err.println("Problem handling client connection: " + e.getMessage());
}
}
} catch (IOException e) {
System.err.println("Problem with " + Server.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