diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework1/GoerickeTcpExample1Telnet.java b/assignments/src/MV3500Cohort2020JulySeptember/homework1/GoerickeTcpExample1Telnet.java new file mode 100644 index 0000000000000000000000000000000000000000..606f5dc8e8922dad6f5f319bf7c320c24104494b --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework1/GoerickeTcpExample1Telnet.java @@ -0,0 +1,72 @@ + +package MV3500Cohort2020JulySeptember.homework1; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * Homework 1 for class MV3500 - Summer 2020 + * @author Goericke, Stefan + */ +public class GoerickeTcpExample1Telnet { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) throws IOException { + System.out.println("Homework 1 MV3500 - Summer 2020 (Author: Stefan Goericke)"); + System.out.println("---------------------------------------------------------"); + String masterString = " ######### ###### # #\r\n"; + masterString = masterString + " # # # #\r\n"; + masterString = masterString + " # ###### # #\r\n"; + masterString = masterString + " # # # #\r\n"; + masterString = masterString + " # ###### #\r\n"; + + //build up connection + try + { + // build serverSOcket with port 2317 + ServerSocket serverSocket = new ServerSocket(2317); + System.out.println("ServerSocket created with port 2317 ..."); + + // open client connection. Wait for answer (method accept) + System.out.println("Open telnet client in cmd-console: telnet localhost 2317"); + Socket clientConnection = serverSocket.accept(); + System.out.println("Client Connection established ..."); + + // create stream-objects + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); // already declared outside try-block + System.out.println("Stream objects created ..."); + + try{// connection was established + System.out.println("Connection established, sending message --> /n"); + + ps.println("This client response was written by server TcpExample1."); + ps.println(masterString); + + System.out.println("This server response was written by server TcpExample1."); + System.out.println(masterString); + + ps.flush(); + } + catch(Exception e){ // in case message could not be send + System.out.println("Error while sending message .... press ENTER"); + System.in.read(); + System.exit(0); + + } + clientConnection.close(); + } + catch (IOException e){ // connection could not be established + System.out.println("Connection could not be established. Quit programm"); + System.in.read(); + System.exit(0); + } + + } + +}