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

Duplicated HW2 to HW1 since I have no idea what the numbering scheme is actually supposed to be.

parent f6653965
No related branches found
No related tags found
No related merge requests found
Showing
with 174 additions and 0 deletions
<?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;
nothing yet
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