diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/CainTcpClient.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/CainTcpClient.java new file mode 100644 index 0000000000000000000000000000000000000000..84fa0846ce1713ae38b9c94e28fad3ea09438a96 --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/CainTcpClient.java @@ -0,0 +1,55 @@ +package MV3500Cohort2018JulySeptember.homework2; + +import java.net.Socket; +import java.io.*; +import java.net.*; + +/** + * credit to author mcgredo + */ +public class CainTcpClient { + + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1 + + public static void main(String[] args) { + try { + while (true) { + System.out.println("creating socket"); + + // * Changed IP from 2317 to 2468. + Socket socket = new Socket(LOCALHOST, 2468); // locohost? + + OutputStream os = socket.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os); + BufferedWriter bw = new BufferedWriter(osw); + + InputStream is = socket.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + String inputMessage = "Client: We are under attack! What do we do? "; + System.out.println("Client: we told the server: We are under attack! What do we do? "); + String sendMessage = inputMessage + "\n"; + bw.write(sendMessage); + bw.flush(); + + String input = br.readLine(); + System.out.println("Client: copy, server: " + input); + +// String serverMessage = br.readLine(); + System.out.println("Client: We are on our way!\n" ); // + serverMessage + + String update = br.readLine(); +// String sendUpdate = update + "\n"; +// System.out.println("Answer:" + sendUpdate); + + } // end while(true) + } catch (IOException e) { + System.out.println("Problem with client: "); // describe what is happening + System.out.println(e); + } + // program exit: tell somebody about that + System.out.println("client exit"); + } + +} diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/CainTcpServer.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/CainTcpServer.java new file mode 100644 index 0000000000000000000000000000000000000000..4ef063425e64d28f99acf21a00be85ad0aec1d92 --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/CainTcpServer.java @@ -0,0 +1,73 @@ +package MV3500Cohort2018JulySeptember.homework2; + +import java.io.*; +import java.net.*; + +/** + * + * telnet localhost 2468 + * telnet <ipOfServersLaptop> 2468 + * credit to author mcgredo + */ +public class CainTcpServer +{ + + public static void main(String[] args) + { + try + { + // * Changed IP from 2317 to 2468. + ServerSocket serverSocket = new ServerSocket(2468); + + int x = 1; + while(x<=10) + { + System.out.println("server here, starting loop #: " + x); + Socket clientConnection = serverSocket.accept(); // block until connected + InputStream is = clientConnection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + String receivedInput = br.readLine(); + System.out.println("Server: the client said: \n" + receivedInput); + + String returnInput; + returnInput = "Get to the choppa!"; // shows up + System.out.println("Server: told the client to Get to the choppa!\n"); + OutputStream os = clientConnection.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os); + BufferedWriter bw = new BufferedWriter(osw); + +// bw.write(returnInput); +// System.out.println("Message volley"); + + PrintStream ps = new PrintStream(os); + ps.println("Get to the choppa!"); + ps.println(returnInput); + + 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 + " ))"); + + // 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. + ps.flush(); + clientConnection.close(); + x++; + } + } + catch(Exception e) + { + // cosmetic change to print line + System.out.println("You got problems! Deal with it"); + } + + } + +} \ No newline at end of file