diff --git a/projects/Assignments/homework1/YamashitaDeMouraEntity.java b/projects/Assignments/homework1/YamashitaDeMouraEntity.java new file mode 100644 index 0000000000000000000000000000000000000000..160d794ba08c4bfc2dac0c174ab516ec196ecbad --- /dev/null +++ b/projects/Assignments/homework1/YamashitaDeMouraEntity.java @@ -0,0 +1,82 @@ +/* + * 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. + */ + +/** + * + * @author dougl + */ +public class YamashitaDeMouraEntity { + + private String name; + private float x; + private float y; + private float z; + + public YamashitaDeMouraEntity (String name, float x, float y, float z) { + this.name = name; + this.x = x; + this.y = y; + this.z = z; + } + + /** + * @return the x + */ + public float getX() { + return x; + } + + /** + * @param x the x to set + */ + public void setX(float x) { + this.x = x; + } + + /** + * @return the y + */ + public float getY() { + return y; + } + + /** + * @param y the y to set + */ + public void setY(float y) { + this.y = y; + } + + /** + * @return the z + */ + public float getZ() { + return z; + } + + /** + * @param z the z to set + */ + public void setZ(float z) { + this.z = z; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + +} diff --git a/projects/Assignments/homework1/YamashitaDeMouraOutputs.png b/projects/Assignments/homework1/YamashitaDeMouraOutputs.png new file mode 100644 index 0000000000000000000000000000000000000000..7f979a0d27544b621fd207fd152c2d919b16779e Binary files /dev/null and b/projects/Assignments/homework1/YamashitaDeMouraOutputs.png differ diff --git a/projects/Assignments/homework1/YamashitaDeMouraSequenceDiagram.docx b/projects/Assignments/homework1/YamashitaDeMouraSequenceDiagram.docx new file mode 100644 index 0000000000000000000000000000000000000000..e209e4ff03f1af688dfdb9fb6942f1006581d54f Binary files /dev/null and b/projects/Assignments/homework1/YamashitaDeMouraSequenceDiagram.docx differ diff --git a/projects/Assignments/homework1/YamashitaDeMouraTcpClient.java b/projects/Assignments/homework1/YamashitaDeMouraTcpClient.java new file mode 100644 index 0000000000000000000000000000000000000000..b927e2f0c830f8779718284bdec8597efe658544 --- /dev/null +++ b/projects/Assignments/homework1/YamashitaDeMouraTcpClient.java @@ -0,0 +1,80 @@ + +import java.io.*; +import java.net.*; + +/** + * MV3500 + * + * TCP Client + * + * @author Douglas Yamashita de Moura + * @version 20180212 + * + */ +public class YamashitaDeMouraTcpClient { + + public static void main(String[] args) + { + try + { + boolean openConnection = true; + + Socket socket = new Socket("localhost", 2317); + + System.out.println("Socket created. Communication started...\n"); + + // Create entity + YamashitaDeMouraEntity entity = new YamashitaDeMouraEntity("Test Entity", 10, 20, 15); + String response = ""; + + int messageCount = 1; + + while(openConnection) { + + System.out.println("Request #" + messageCount); + + // Output + OutputStream os = socket.getOutputStream(); + PrintStream ps = new PrintStream(os); + + switch(messageCount) { + case 1: + response = "'Hi, I am the client and I request a connection.'\n"; + break; + case 2: + response = "'Sending entity with name " + entity.getName() + + " and position (" + entity.getX() + ", " + entity.getY() + + ", " + entity.getZ() + ").'\n"; + break; + case 3: + response = "'Thanks!'\n"; + break; + } + + ps.print(response); + ps.flush(); + System.out.print("Message sent to server: " + response); + messageCount++; + + InputStream is = socket.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + String line = br.readLine(); + System.out.println("Message received from server: " + line + "\n"); + + if (line.contains("Closing connection.")) { + openConnection = false; + socket.close(); + } + } + } + catch(Exception e) + { + System.out.println(e); + System.out.println("Problem with client"); + } + + } + +} diff --git a/projects/Assignments/homework1/YamashitaDeMouraTcpServer.java b/projects/Assignments/homework1/YamashitaDeMouraTcpServer.java new file mode 100644 index 0000000000000000000000000000000000000000..c580b0dd368b9653564c1f8d4f2427a4c66cd3b4 --- /dev/null +++ b/projects/Assignments/homework1/YamashitaDeMouraTcpServer.java @@ -0,0 +1,85 @@ + +import java.io.*; +import java.net.*; + +/** + * MV3500 + * + * TCP Server. + * This server works with only one client and can exchange + * multiples messages with it. + * + * @author Douglas Yamashita de Moura + * @version 20180212 + * + */ +public class YamashitaDeMouraTcpServer +{ + + public static void main(String[] args) + { + try + { + ServerSocket serverSocket = new ServerSocket(2317); + System.out.print("Server socket created. Waiting for client...\n"); + + Socket clientConnection = serverSocket.accept(); + + InetAddress localAddress = clientConnection.getLocalAddress(); + InetAddress remoteAddress = clientConnection.getInetAddress(); + + int localPort = clientConnection.getLocalPort(); + int remotePort = clientConnection.getPort(); + + System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))\n"); + + int count = 1; + boolean openConnection = true; + + while(openConnection) + { + String response = ""; + + System.out.println("Request #" + count); + + // Receives message from client + InputStream is = clientConnection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + String clientMessage = br.readLine(); + System.out.println("The message the client sent was: " + clientMessage + "."); + + // Sends message to client + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + + switch (count){ + + case 1: + response = "'I am the server and I accepted the request. Send entity name and position.'\n"; + break; + case 2: + response = "'I confirm the receipt of the message.'\n"; + break; + case 3: + response = "'You are welcome. Closing connection.'\n"; + openConnection = false; + break; + } + + System.out.println("Message sent to client: " + response); + ps.print(response); + ps.flush(); + response = ""; + count++; + + } + clientConnection.close(); + } catch(Exception e){ + System.out.println("Problem with networking"); + System.out.println("Exception: " + e); + } + } + +} \ No newline at end of file