Skip to content
Snippets Groups Projects
Commit 3f6a88f2 authored by Brutzman, Don's avatar Brutzman, Don
Browse files

Homework 1 submission with refactor/rename to have student name first

parent d27e72f6
No related branches found
No related tags found
No related merge requests found
/*
* 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;
}
}
projects/Assignments/homework1/YamashitaDeMouraOutputs.png

72.6 KiB

File added
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");
}
}
}
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
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