Skip to content
Snippets Groups Projects
Commit 81abf346 authored by Royer, Nicholas (Capt)'s avatar Royer, Nicholas (Capt)
Browse files

Added HW2

parent 8d113b1e
No related branches found
No related tags found
No related merge requests found
Showing
with 8 additions and 179 deletions
...@@ -44,4 +44,6 @@ ...@@ -44,4 +44,6 @@
/examples/DisShooting/nbproject/private/ /examples/DisShooting/nbproject/private/
/examples/DisDemo/nbproject/private/ /examples/DisDemo/nbproject/private/
/examples/DisDemo/build/ /examples/DisDemo/build/
/examples/DisDemo/dist/ /examples/DisDemo/dist/
\ No newline at end of file /assignments/src/MV3500Cohort2022MayJune/homework2/Royer/Homework2/nbproject/private/
/assignments/src/MV3500Cohort2022MayJune/homework2/Royer/Homework2/build/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>RoyerHomework2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
package royer.homework2;
import java.net.*;
import java.io.*;
import java.util.concurrent.atomic.AtomicBoolean;
/**
*
* @author nick
*/
public class TCPPeer {
private ServerSocket server_socket = null;
private Socket client_socket = null;
private PrintWriter out_stream = null;
private BufferedReader in_stream = null;
private boolean is_server = true;
public boolean isServer() {
return is_server;
}
public TCPPeer(int port, String ip) throws IOException {
is_server = (ip.equals(""));
if (is_server) {
server_socket = new ServerSocket(port);
client_socket = server_socket.accept();
} else {
// Client
client_socket = new Socket(ip, port);
}
in_stream = new BufferedReader(new InputStreamReader(client_socket.getInputStream()));
out_stream = new PrintWriter(client_socket.getOutputStream(), true);
should_stop.set(false);
start();
}
void start() {
if (is_server) {
while(!should_stop.get()) {
try {
String msg = in_stream.readLine();
// if (msg == null || msg.equals(""))
// continue;
System.out.println("Server received \"" + msg + "\"");
out_stream.println("OK");
if (msg.equals("--quit"))
stop_connection();
} catch (IOException ex) {
}
}
}
}
private final AtomicBoolean should_stop = new AtomicBoolean(false);
public void stop_connection() {
should_stop.set(true);
}
public TCPPeer(int port) throws IOException {
this(port, "");
}
public void close() throws IOException {
System.out.println("Closing " + (is_server ? "Server" : "Client"));
in_stream.close();
out_stream.close();
client_socket.close();
if (is_server) {
server_socket.close();
} else {
}
}
public String send(String msg) throws IOException {
out_stream.println(msg);
String response = in_stream.readLine();
System.out.println((is_server ? "Server" : "Client") + " sending \"" + msg + "\": " + response);
return response;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package royer.homework2;
import java.io.*;
/**
*
* @author nick
*/
public class Tester {
private static class ServerThread extends Thread {
private TCPPeer server = null;
int port = 0;
public ServerThread(int port) {
super();
this.port = port;
}
@Override
public void run() {
try {
server = new TCPPeer(port);
server.close();
Thread.currentThread().interrupt();
} catch (Exception ioex) {
System.err.println(ioex.toString());
}
}
}
public static void main(String[] args) {
int test_port = 6668;
try {
System.out.println("Setting up");
ServerThread st = new ServerThread(test_port);
st.start();
TCPPeer client = new TCPPeer(test_port, "127.0.0.1");
System.out.println("Setup complete");
client.send("Test");
client.send("--quit");
client.close();
st.join();
System.out.println("Done");
} catch (Exception ex) {
System.err.println(ex.toString());
}
}
}
royer/homework2/Tester.class
royer/homework2/TCPPeer.class
/home/nick/Documents/NPS/Quarter 4/MV3500/Homework 2/RoyerHomework2/src/main/java/royer/homework2/Tester.java
/home/nick/Documents/NPS/Quarter 4/MV3500/Homework 2/RoyerHomework2/src/main/java/royer/homework2/TCPPeer.java
/**
* ExampleSimpleSimulation program-modification homework assignment supporting the NPS MOVES MV3500 Networked Graphics course.
*
* @see <a href="https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/-/tree/master/assignments">networkedGraphicsMV3500 assignments</a>
* @see java.lang.Package
* @see <a href="https://stackoverflow.com/questions/22095487/why-is-package-info-java-useful">StackOverflow: why-is-package-info-java-useful</a>
* @see <a href="https://stackoverflow.com/questions/624422/how-do-i-document-packages-in-java">StackOverflow: how-do-i-document-packages-in-java</a>
*/
package MV3500Cohort2022MayJune.homework2.Royer;
...@@ -46,8 +46,8 @@ is divided into following sections: ...@@ -46,8 +46,8 @@ is divided into following sections:
<property file="${user.properties.file}"/> <property file="${user.properties.file}"/>
<!-- The two properties below are usually overridden --> <!-- The two properties below are usually overridden -->
<!-- by the active platform. Just a fallback. --> <!-- by the active platform. Just a fallback. -->
<property name="default.javac.source" value="1.8"/> <property name="default.javac.source" value="1.6"/>
<property name="default.javac.target" value="1.8"/> <property name="default.javac.target" value="1.6"/>
</target> </target>
<target depends="-pre-init,-init-private,-init-user" name="-init-project"> <target depends="-pre-init,-init-private,-init-user" name="-init-project">
<property file="nbproject/configs/${config}.properties"/> <property file="nbproject/configs/${config}.properties"/>
......
...@@ -3,6 +3,6 @@ build.xml.script.CRC32=b64626c8 ...@@ -3,6 +3,6 @@ build.xml.script.CRC32=b64626c8
build.xml.stylesheet.CRC32=f85dc8f2@1.91.1.48 build.xml.stylesheet.CRC32=f85dc8f2@1.91.1.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=74d8ac35 nbproject/build-impl.xml.data.CRC32=dec2cf1d
nbproject/build-impl.xml.script.CRC32=e75c0d85 nbproject/build-impl.xml.script.CRC32=7c2cd7eb
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.102.0.48 nbproject/build-impl.xml.stylesheet.CRC32=d549e5cc@1.98.0.48
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