Browse Source

initial version

master
Sebastian Rieger 2 years ago
commit
fdba979a0f
  1. 6
      .classpath
  2. 1
      .gitignore
  3. 17
      .project
  4. BIN
      build/Server1.jar
  5. BIN
      build/Server2.jar
  6. BIN
      build/TCPPerfServer.jar
  7. BIN
      build/UDPPerfServer.jar
  8. 53
      build/build-server-jars.xml
  9. 59
      src/perf/TCPPerfClient.java
  10. 55
      src/perf/TCPPerfServer.java
  11. 70
      src/perf/UDPPerfClient.java
  12. 58
      src/perf/UDPPerfServer.java
  13. 43
      src/socket/Client1.java
  14. 37
      src/socket/Client2.java
  15. 42
      src/socket/Server1.java
  16. 45
      src/socket/Server2.java

6
.classpath

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>

1
.gitignore

@ -0,0 +1 @@
/bin/

17
.project

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>kommunikationsnetze-und-protokolle-bsc-ai-examples</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

BIN
build/Server1.jar

BIN
build/Server2.jar

BIN
build/TCPPerfServer.jar

BIN
build/UDPPerfServer.jar

53
build/build-server-jars.xml

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="all" name="Create Runnable Jar for Servers in Project KommProt-Examples">
<!--this file was created by Eclipse Runnable JAR Export Wizard-->
<!--ANT 1.7 is required -->
<!--define folder properties-->
<property name="dir.buildfile" value=".." />
<property name="dir.workspace" value=".." />
<property name="dir.jarfile" value="." />
<target name="all" depends="create_run_jar_server1, create_run_jar_server2, create_run_jar_tcpperfserver, create_run_jar_udpperfserver">
</target>
<target name="create_run_jar_server1">
<jar destfile="${dir.jarfile}/Server1.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="socket.Server1" />
<attribute name="Class-Path" value="." />
</manifest>
<fileset dir="${dir.buildfile}/bin" />
</jar>
</target>
<target name="create_run_jar_server2">
<jar destfile="${dir.jarfile}/Server2.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="socket.Server2" />
<attribute name="Class-Path" value="." />
</manifest>
<fileset dir="${dir.buildfile}/bin" />
</jar>
</target>
<target name="create_run_jar_tcpperfserver">
<jar destfile="${dir.jarfile}/TCPPerfServer.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="perf.TCPPerfServer" />
<attribute name="Class-Path" value="." />
</manifest>
<fileset dir="${dir.buildfile}/bin" />
</jar>
</target>
<target name="create_run_jar_udpperfserver">
<jar destfile="${dir.jarfile}/UDPPerfServer.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="perf.UDPPerfServer" />
<attribute name="Class-Path" value="." />
</manifest>
<fileset dir="${dir.buildfile}/bin" />
</jar>
</target>
</project>

59
src/perf/TCPPerfClient.java

@ -0,0 +1,59 @@
package perf;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class TCPPerfClient {
public static void main(String argv[]) throws Exception {
String hostname = "";
int tcpPort = 36039;
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
//System.out.println("Bitte geben Sie die Server-Adresse ein: ");
//hostname = inFromUser.readLine();
hostname = "127.0.0.1";
while (true ) {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Bitte geben Sie die Anzahl zu übertragender Megabytes ein: ");
Long number = new Long(inFromUser.readLine());
//Long number = (long)1;
Socket clientSocket = new Socket(hostname, tcpPort);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
long timestampStart = System.currentTimeMillis();
System.out.println("Zeit vor Versand = " + timestampStart + " ms, " + df.format(new Date(timestampStart)));
outToServer.writeBytes(number.toString() + "\n");
if (number == 0) {
break;
}
char[] buffer = new char[1024 * 1024];
long totalBytesReceived = 0;
int charsRead = inFromServer.read(buffer);
while (charsRead != -1)
{
totalBytesReceived += charsRead;
charsRead = inFromServer.read(buffer);
}
long timestampEnd = System.currentTimeMillis();
System.out.println("Zeit nach Empfang = " + timestampEnd + " ms, " + df.format(new Date(timestampEnd)));
long duration = timestampEnd - timestampStart;
System.out.println("Delay = " + duration + " ms");
double seconds = (duration / 1000.0);
double throughput = (totalBytesReceived / seconds);
System.out.println("Rate = " + (long)throughput + " B/s");
clientSocket.close();
}
}
}

55
src/perf/TCPPerfServer.java

@ -0,0 +1,55 @@
package perf;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
class TCPPerfServer {
public static void main(String argv[]) throws Exception {
int tcpPort = 36039;
Long clientNumberOfMegabytes;
ServerSocket serverSocket = new ServerSocket(tcpPort);
System.out.println("TCPPerfServer gestartet. Warte auf eingehende Requests...");
while (true) {
Socket connectionSocket = serverSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
BufferedWriter outToClient = new BufferedWriter(new OutputStreamWriter(connectionSocket.getOutputStream()));
char[] buffer = new char[1024];
clientNumberOfMegabytes = new Long(inFromClient.readLine());
if (clientNumberOfMegabytes == 0) {
connectionSocket.close();
break;
}
System.out.println("Request von TCPPerfClient " + connectionSocket.getInetAddress() + ":" + connectionSocket.getPort() + " empfangen. Erzeuge und sende " + clientNumberOfMegabytes + "MB ...");
for (long mb = 0; mb < clientNumberOfMegabytes; mb++) {
long megabyteStartTimeMilli = System.currentTimeMillis();
for (long segment = 1; segment < 1024; segment++) {
long segmentStartTimeNano = System.nanoTime();
outToClient.write(buffer);
if (clientNumberOfMegabytes == 1) {
double transferDurationSegment = System.nanoTime() - segmentStartTimeNano;
//System.out.printf("Dauer pro Segment: %.0f ns, Rate: %.0f KByte/s (MB: %d, Segment: %d)\n", transferDurationSegment, (1024 / (transferDurationSegment / 1000000000)), mb, segment);
// CSV Output Excel
System.out.println(mb + "," + segment + "," + transferDurationSegment);
}
}
double transferDurationMB = System.currentTimeMillis() - megabyteStartTimeMilli;
System.out.printf("1 MB (genauer MiB) gesendet in: %.0f ms, Rate: %.0f Bytes/s (MB: %d)\n", transferDurationMB, ((1024 * 1024) / (transferDurationMB / 1000)), mb);
}
System.out.print("\n");
connectionSocket.close();
}
serverSocket.close();
}
}

70
src/perf/UDPPerfClient.java

@ -0,0 +1,70 @@
package perf;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class UDPPerfClient {
public static void main(String argv[]) throws Exception {
String hostname = "";
int udpPort = 36039;
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
//System.out.println("Bitte geben Sie die Server-Adresse ein: ");
//hostname = inFromUser.readLine();
hostname = "127.0.0.1";
InetAddress serverAddress = InetAddress.getByName(hostname);
while (true) {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Bitte geben Sie die Anzahl zu übertragender Megabytes ein: ");
Long numberOfMegabytes = new Long(inFromUser.readLine());
//Long number = (long)1;
DatagramSocket clientSocket = new DatagramSocket();
clientSocket.setSoTimeout(5000);
long timestampStart = System.currentTimeMillis();
System.out.println("Zeit vor Versand = " + timestampStart + " ms, " + df.format(new Date(timestampStart)));
byte[] outbuffer = (new String(numberOfMegabytes + "\n")).getBytes();
DatagramPacket sendPacket = new DatagramPacket(outbuffer, outbuffer.length, serverAddress, udpPort);
clientSocket.send(sendPacket);
if (numberOfMegabytes == 0) {
break;
}
long totalBytesReceived = 0;
while (true) {
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
clientSocket.receive(receivePacket);
} catch (SocketTimeoutException ste) {
System.out.println("Timeout");
break;
}
String receivedString = new String(receiveData);
if (receivedString.contains("done")) {
System.out.println("Ende der Übermittlung erkannt.");
break;
}
else
{
totalBytesReceived += receivePacket.getLength();
System.out.println(totalBytesReceived + " Bytes insgesamt empfangen.");
}
}
System.out.printf("Insgesamt %d Bytes von %d (%d MB) empfangen.\n", totalBytesReceived - 4, (1024 * 1024) * numberOfMegabytes, numberOfMegabytes);
clientSocket.close();
}
}
}

58
src/perf/UDPPerfServer.java

@ -0,0 +1,58 @@
package perf;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
class UDPPerfServer {
public static void main(String argv[]) throws Exception {
int udpPort = 36039;
Long clientNumberOfMegabytes;
DatagramSocket datagramSocket = new DatagramSocket(udpPort);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
System.out.println("UDPPerfServer gestartet. Warte auf eingehende Requests...");
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
datagramSocket.receive(receivePacket);
InetAddress clientAddress = receivePacket.getAddress();
int clientSrcPort = receivePacket.getPort();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(receivePacket.getData())));
clientNumberOfMegabytes = new Long(inFromClient.readLine());
if (clientNumberOfMegabytes == 0) {
break;
}
for (long mb = 0; mb < clientNumberOfMegabytes; mb++) {
long megabyteStartTimeMilli = System.currentTimeMillis();
for (long segment = 1; segment < 1024; segment++) {
//long segmentStartTimeNano = System.nanoTime();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, clientSrcPort);
datagramSocket.send(sendPacket);
//double transferDurationSegment = System.nanoTime() - segmentStartTimeNano;
//System.out.printf("Dauer pro Segment: %.0f ns, Rate: %.0f KByte/s (MB: %d, Segment: %d)\n", transferDurationSegment, (1024 / (transferDurationSegment / 1000000000)), mb, segment);
// CSV Output Excel
//System.out.println(mb + "," + segment + "," + transferDurationSegment);
}
double transferDurationMB = System.currentTimeMillis() - megabyteStartTimeMilli;
System.out.printf("1 MB (genauer MiB) gesendet in: %.0f ms, Rate: %.0f Bytes/s (MB: %d)\n", transferDurationMB, ((1024 * 1024) / (transferDurationMB / 1000)), mb);
}
System.out.print("\n");
byte[] finishLabel = (new String("done")).getBytes();
DatagramPacket sendPacket = new DatagramPacket(finishLabel, finishLabel.length, clientAddress, clientSrcPort);
datagramSocket.send(sendPacket);
}
datagramSocket.close();
}
}

43
src/socket/Client1.java

@ -0,0 +1,43 @@
/* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */
package socket;
import java.io.*;
import java.net.*;
class Client1 {
public static void main(String args[]) throws Exception {
String hostname = "localhost";
int port = 36037;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName(hostname);
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String message;
String modifiedMessage;
while (true)
{
System.out.print("Nachricht an Server1: ");
message = inFromUser.readLine();
sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, port);
clientSocket.send(sendPacket);
if (message.equals("exit")) {
break;
}
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
modifiedMessage = new String(receivePacket.getData());
System.out.println("Antwort von Server1: " + modifiedMessage);
}
clientSocket.close();
}
}

37
src/socket/Client2.java

@ -0,0 +1,37 @@
/* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */
package socket;
import java.io.*;
import java.net.*;
class Client2 {
public static void main(String argv[]) throws Exception {
String hostname = "localhost";
int port = 36037;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(hostname, port);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String message;
String modifiedMessage;
while (true)
{
System.out.print("Nachricht an Server2: ");
message = inFromUser.readLine();
outToServer.writeBytes(message + '\n');
if (message.equals("exit")) {
break;
}
modifiedMessage = inFromServer.readLine();
System.out.println("Antwort von Server2: " + modifiedMessage);
}
clientSocket.close();
}
}

42
src/socket/Server1.java

@ -0,0 +1,42 @@
/* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */
package socket;
import java.net.*;
class Server1 {
public static void main(String args[]) throws Exception {
int port = 36037;
DatagramSocket serverSocket = new DatagramSocket(port);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
String message;
String modifiedMessage;
System.out.println("Server1 gestartet. Warte auf eingehende Requests...");
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
message = new String(receivePacket.getData(), 0, receivePacket.getLength());
InetAddress clientAddress = receivePacket.getAddress();
int clientSrcPort = receivePacket.getPort();
System.out.println("Request von Client1 " + clientAddress + ":" + clientSrcPort + " empfangen. Erzeuge und sende Antwort...");
if (message.equals("exit"))
{
break;
}
modifiedMessage = message.toUpperCase();
sendData = modifiedMessage.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, clientSrcPort);
serverSocket.send(sendPacket);
}
serverSocket.close();
}
}

45
src/socket/Server2.java

@ -0,0 +1,45 @@
/* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */
package socket;
import java.io.*;
import java.net.*;
class Server2 {
public static void main(String argv[]) throws Exception {
int port = 36037;
ServerSocket serverSocket = new ServerSocket(port);
String message;
String modifiedMessage;
System.out.println("Server2 gestartet. Warte auf eingehende Requests...");
Socket connectionSocket = serverSocket.accept();
while (true) {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
// Try/Catch hinzugefügt, nachdem bei Einsatz mit Amazon AWS (Healthcheck des ELB) message null war
try {
message = inFromClient.readLine();
if (message != null) {
System.out.println("Request von Client2 " + connectionSocket.getInetAddress() + ":" + connectionSocket.getPort() + " empfangen. Erzeuge und sende Antwort...");
if (message.equals("exit")) {
break;
}
modifiedMessage = message.toUpperCase() + '\n';
outToClient.writeBytes(modifiedMessage);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
connectionSocket.close();
serverSocket.close();
}
}
Loading…
Cancel
Save