Skip to content
Snippets Groups Projects
Commit 3e0a6765 authored by stefa's avatar stefa
Browse files

Upload Homework 1 - Goericke

parent ce140bc4
No related branches found
No related tags found
No related merge requests found
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);
}
}
}
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