diff --git a/projects/Assignments/homework1/MaroonHomework1UML.pptx b/projects/Assignments/homework1/MaroonHomework1UML.pptx new file mode 100644 index 0000000000000000000000000000000000000000..2d92f878296ebf46e4e039d9b84157254780b1d6 Binary files /dev/null and b/projects/Assignments/homework1/MaroonHomework1UML.pptx differ diff --git a/projects/Assignments/homework1/MaroonTcpClient.java b/projects/Assignments/homework1/MaroonTcpClient.java new file mode 100644 index 0000000000000000000000000000000000000000..1c965bd153a3a0d278897b7d6a461ae8ee82a2a3 --- /dev/null +++ b/projects/Assignments/homework1/MaroonTcpClient.java @@ -0,0 +1,62 @@ + +package tcpclient; + +import java.io.*; +import java.net.*; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.util.Scanner; + +/** + * Before, we always used telnet to connect to the server. We + * are now writing our own program to do the connection. + * + * As you will see, when we run this after we start the server + * we will see the same string telnet printed, sent by the server. + * The output at the server will show different socket pairs for + * each time we ran it. + * + * @author mcgredo + */ +public class MaroonTcpClient { + + public static DataInputStream input; + public static DataOutputStream output; + + + + public static void main(String[] args) + { + Scanner userInput = new Scanner(System.in); + try + { + System.out.println("creating socket"); + Socket socket = new Socket("localhost", 2317); + + input = new DataInputStream(socket.getInputStream()); + output = new DataOutputStream(socket.getOutputStream()); + + + + System.out.println("Type position or ID: \n "); + String request; + request = userInput.nextLine(); + output.writeUTF(request); + + String reply; + reply = input.readUTF(); + System.out.println(reply); + + + } + catch(Exception e) + { + System.out.println(e); + System.out.println("Problem with client"); + } + + } + +} diff --git a/projects/Assignments/homework1/MaroonTcpServer.java b/projects/Assignments/homework1/MaroonTcpServer.java new file mode 100644 index 0000000000000000000000000000000000000000..5835f592b4621f8a13e9172c7cff2f7f04258902 --- /dev/null +++ b/projects/Assignments/homework1/MaroonTcpServer.java @@ -0,0 +1,86 @@ +package tcpserver; + +import java.io.*; +import java.net.*; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Arrays; + +/** + * Very slightly more complex than example1. A complete copy of + * example 2. The only thing this does + * differently is introduce a loop into the response, so you don't + * have to restart the program after one response. Also, it prints + * out the socket pair the server sees. Run the program via telnet + * several times and compare the socket pairs. + * + * telnet localhost 2317 + * + * If you're sophisticated you can contact the instructor's computer + * while running this program. + * + * telnet <ipOfServersLaptop> 2317 + * + * And have him display the socket pairs he got. + * @author mcgredo + */ +public class MaroonTcpServer +{ + + public static DataInputStream input; + public static DataOutputStream output; + + + public static void main(String[] args) + { + + MaroonUnit testUnit1 = new MaroonUnit (); + + try + { + // ServerSocket waits for a connection from a client. + // Notice that it is outside the loop; ServerSocket + // needs to be made only once. + + ServerSocket serverSocket = new ServerSocket(2317); + + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while(true) + { + Socket clientConnection = serverSocket.accept(); + //OutputStream os = clientConnection.getOutputStream(); + input = new DataInputStream(clientConnection.getInputStream()); + output = new DataOutputStream(clientConnection.getOutputStream()); + + String request = input.readUTF(); + + if ("position".equals(request)){ + output.writeUTF(Arrays.toString(testUnit1.getPosition())); + } + else if ("ID".equals(request)){ + output.writeUTF(""+ testUnit1.getID()); + + }else + output.writeUTF("I don't understand, try www.google.com"); + + + // Notice the use of flush() and close(). Without + // the close() to Socket object may stay open for + // a while after the client has stopped needing this + // connection. Close() explicitly ends the connection. + + clientConnection.close(); + } + } + catch(Exception e) + { + System.out.println("problem with networking"); + } + + } + +} \ No newline at end of file diff --git a/projects/Assignments/homework1/MaroonUnit.java b/projects/Assignments/homework1/MaroonUnit.java new file mode 100644 index 0000000000000000000000000000000000000000..a9737e197f71e6eec677628b59a962d1490cd04b --- /dev/null +++ b/projects/Assignments/homework1/MaroonUnit.java @@ -0,0 +1,43 @@ +/* + * 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 tcpserver; + +/** + * + * @author Kens + */ +class MaroonUnit { + + private int[] position = new int[3]; + private int ID; + + MaroonUnit() { + this.position[0] = 0; + this.position[1] = 0; + this.position[2] = 0; + + this.ID = 50; + + } + + MaroonUnit(int x, int y, int z, int id){ + this.position[0] = x; + this.position[1] = y; + this.position[2] = z; + + this.ID = id; + } + + int getID(){ + return this.ID; + } + + int [] getPosition(){ + return this.position; + } + + +}