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

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

parent 62a57306
No related branches found
No related tags found
No related merge requests found
File added
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");
}
}
}
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
/*
* 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;
}
}
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