1
0
Fork 0
main
Sandro Leuchter 2025-03-18 07:08:10 +01:00
commit 3230b722fb
377 changed files with 13199 additions and 0 deletions

4
02-udp/justfile 100644
View File

@ -0,0 +1,4 @@
import '../justfile'
zed:
$VISUAL udp.echo udp.echo_solution udp.time udp.time_solution udp.messwerte udp.messwerte_solution udp.game_solution

14
02-udp/pom.xml 100644
View File

@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>udp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,43 @@
{
"folders": [
{
"name": "1. Echo Service",
"path": "udp.echo",
},
{
"name": "1. Echo Service (ML)",
"path": "udp.echo_solution",
},
{
"name": "2. Time Service",
"path": "udp.time",
},
{
"name": "2. Time Service (ML)",
"path": "udp.time_solution",
},
{
"name": "3. Messwert Service",
"path": "udp.messwerte",
},
{
"name": "3. Messwert Service (ML)",
"path": "udp.messwerte_solution",
},
{
"name": "4. ReactionGame (ML)",
"path": "udp.game_solution",
},
],
"extensions": {
"recommendations": [
"vscjava.vscode-java-pack",
"ms-azuretools.vscode-docker",
"skellock.just",
],
},
"settings": {
"java.dependency.syncWithFolderExplorer": true,
"java.project.explorer.showNonJavaResources": false,
},
}

View File

@ -0,0 +1,6 @@
import '../justfile'
server:
just exec vs.EchoServer ""
client message:
just exec vs.EchoClient "{{message}}"

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>udp.echo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>udp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,33 @@
package vs;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
public class EchoClient {
private static final String HOST = "localhost";
private static final int PORT = 4711;
private static final int BUFSIZE = 512;
private static final int TIMEOUT = 2000;
public static void main(String[] args) {
byte[] data = args[0].getBytes();
try (DatagramSocket socket = new DatagramSocket()) {
socket.setSoTimeout(TIMEOUT); // Zeit in ms, für wie lange ein read() auf socket blockiert.
// Bei timeout is java.net.SocketTimeoutException (TIMEOUT == 0
// => blockiert für immer)
InetAddress iaddr = InetAddress.getByName(HOST);
DatagramPacket packetOut = new DatagramPacket(data, data.length, iaddr, PORT);
socket.send(packetOut);
DatagramPacket packetIn = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
socket.receive(packetOut);
String received = new String(packetIn.getData(), 0, packetIn.getLength());
System.out.println("Received: " + received);
} catch (SocketTimeoutException e) {
System.err.println("Timeout: " + e.getMessage());
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,31 @@
package vs;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class EchoServer {
private static final int PORT = 4711;
private static final int BUFSIZE = 512;
public static void main(final String[] args) {
try (DatagramSocket socket = new DatagramSocket(PORT)) {
DatagramPacket packetIn = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
DatagramPacket packetOut = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
System.out.println("Server gestartet ...");
while (true) {
socket.receive(packetIn);
System.out.println(
"Received: " + packetIn.getLength() + " bytes: " + new String(packetIn.getData()));
packetOut.setData(packetIn.getData());
packetOut.setLength(packetIn.getLength());
// mehr Eigenschaften von packetOut setzen...
socket.send(packetOut);
}
} catch (IOException e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,6 @@
import '../justfile'
server:
just exec vs.EchoServer ""
client message:
just exec vs.EchoClient "{{message}}"

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>udp.echo_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>udp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,58 @@
package vs;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
/**
* Client for echo var.sockets.udp.echo.EchoServer service. Sendet Kommandozeilenargument an HOST,
* wartet bis TIMEOUT auf Antwort vom Server, liest sie und gibt den Inhalt aus.
*
* @author Sandro Leuchter
*
*/
public class EchoClient {
/**
* host on which server is running (IP-address or hostname)
*/
private static final String HOST = "localhost";
/**
* port on which service is running on host
*/
private static final int PORT = 4711;
/**
* maximum size of payload in datagram
*/
private static final int BUFSIZE = 512;
/**
* timeout in ms for waiting for a response from server
*/
private static final int TIMEOUT = 2000;
/**
* main method: entrypoint to run
*
* @param args
* must be String[1]: message to be send to server
*/
public static void main(String[] args) {
byte[] data = args[0].getBytes();
try (DatagramSocket socket = new DatagramSocket()) {
socket.setSoTimeout(TIMEOUT); // Zeit in ms, für wie lange ein read() auf socket blockiert.
// Bei timeout is java.net.SocketTimeoutException (TIMEOUT == 0
// => blockiert für immer)
InetAddress iaddr = InetAddress.getByName(HOST);
DatagramPacket packetOut = new DatagramPacket(data, data.length, iaddr, PORT);
socket.send(packetOut);
DatagramPacket packetIn = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
socket.receive(packetIn);
String received = new String(packetIn.getData(), 0, packetIn.getLength());
System.out.println("Received: " + received);
} catch (SocketTimeoutException e) {
System.err.println("Timeout: " + e.getMessage());
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,50 @@
package vs;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* Server for var.sockets.udp.echo.EchoServer service. Empfängt ANfrage von Clients und sendet den
* Inhalt jeweils an den Client zurück
*
* @author Sandro Leuchter
*
*/
public class EchoServer {
/**
* port on which service is running on host
*/
private static final int PORT = 4711;
/**
* maximum size of payload in datagram
*/
private static final int BUFSIZE = 512;
/**
* main method: entrypoint to run
*
* @param args
* ignored
*/
public static void main(final String[] args) {
try (DatagramSocket socket = new DatagramSocket(PORT)) {
DatagramPacket packetIn = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
DatagramPacket packetOut = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
System.out.println("Server gestartet ...");
while (true) {
socket.receive(packetIn);
System.out.println("Received: " + packetIn.getLength() + " bytes: "
+ new String(packetIn.getData(), 0, packetIn.getLength()));
packetOut.setData(packetIn.getData());
packetOut.setLength(packetIn.getLength());
packetOut.setSocketAddress(packetIn.getSocketAddress());
socket.send(packetOut);
}
} catch (IOException e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,6 @@
import '../justfile'
server:
just exec vs.ReactionGameServer ""
client:
just exec vs.ReactionGameClient ""

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>udp.game_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>udp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,42 @@
package vs;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* Client for echo var.sockets.udp.game.ReactionGameServer service. Sendet leerees Paket an HOST.
*
* @author Sandro Leuchter
*
*/
public class ReactionGameClient {
/**
* host on which server is running (IP-address or hostname)
*/
private static final String HOST = "localhost";
/**
* port on which service is running on host
*/
private static final int PORT = 4714;
/**
* maximum size of payload in datagram
*/
private static final int BUFSIZE = 0;
/**
* main method: entrypoint to run
*
* @param args
* ignored
*/
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) {
InetAddress iaddr = InetAddress.getByName(HOST);
DatagramPacket packetOut = new DatagramPacket(new byte[BUFSIZE], BUFSIZE, iaddr, PORT);
socket.send(packetOut);
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,63 @@
package vs;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketTimeoutException;
import java.util.Random;
/**
* Server for var.sockets.udp.game.ReactionGameServer service. Waits a random time less than
* MAX_WAITING_TIME_MS ms, then opens Gate for GATE_OPEN_DURATION_MS ms on port PORT, waiting for a
* UDP packet to receive.
*
* @author Sandro Leuchter
*
*/
public class ReactionGameServer {
/**
* port on which service is running on host
*/
private static final int PORT = 4714;
private static final int GATE_OPEN_DURATION_MS = 2000;
private static final int MAX_WAITING_TIME_MS = 4000;
private static final Random random = new Random(System.currentTimeMillis());
/**
* maximum size of payload in datagram
*/
private static final int BUFSIZE = 512;
/**
* main method: entrypoint to run
*
* @param args
* ignored
* @throws InterruptedException
* if interrupt during random sleeping period
*/
public static void main(final String[] args) throws InterruptedException {
try (DatagramSocket socket = new DatagramSocket(PORT)) {
socket.setSoTimeout(GATE_OPEN_DURATION_MS);
DatagramPacket packetIn = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
System.out.println("Server gestartet ...");
Thread.sleep(random.nextInt(MAX_WAITING_TIME_MS));
boolean timeout = false;
System.out.println("offen für Empfang");
try {
socket.receive(packetIn);
} catch (SocketTimeoutException e) {
timeout = true;
}
System.out.println("Empfang geschlossen");
if (timeout) {
System.out.println("nicht schnell genug!");
} else {
System.out.println("Paket hat es geschafft");
}
} catch (IOException e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,6 @@
import '../justfile'
server:
just exec vs.MesswertServer ""
client:
just exec vs.MesswertClient ""

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>udp.messwerte</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>udp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,22 @@
package vs;
import java.util.Date;
import java.util.Random;
public class MesswertClient {
public static void main(String[] args) {
Random rg = new Random();
try {
// ...
while (true) {
String jetzt = (new Date()).toString();
String messung = Double.toString(rg.nextDouble() * 100.0);
// ...
Thread.sleep(5000);
}
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,8 @@
package vs;
public class MesswertServer {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,6 @@
import '../justfile'
server:
just exec vs.MesswertServer ""
client:
just exec vs.MesswertClient ""

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>udp.messwerte_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>udp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,56 @@
package vs;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.util.Random;
/**
* Client for var.sockets.udp.messwerte.MesswertServer service. Sendet fortwährend Messwerte
*
* @author Sandro Leuchter
*
*/
public class MesswertClient {
/**
* host on which server is running (IP-address or hostname)
*/
private static final String HOST = "localhost";
/**
* port on which service is running on host
*/
private static final int PORT = 4713;
/**
* timeout in ms for waiting for a response from server
*/
private static final int TIMEOUT = 2000;
/**
* main method: entrypoint to run
*
* @param args
* ignored
*/
public static void main(String[] args) {
Random randomGenerator = new Random();
try (DatagramSocket socket = new DatagramSocket()) {
socket.setSoTimeout(TIMEOUT); // Zeit in ms, für wie lange ein read() auf socket blockiert.
// Bei timeout is java.net.SocketTimeoutException (TIMEOUT == 0
// => blockiert für immer)
InetAddress iaddr = InetAddress.getByName(HOST);
while (true) {
String messung = Double.toString(randomGenerator.nextDouble() * 100.0);
DatagramPacket packetOut = new DatagramPacket(messung.getBytes(), messung.length(), iaddr,
PORT);
socket.send(packetOut);
Thread.sleep(5000);
}
} catch (SocketTimeoutException e) {
System.err.println("Timeout: " + e.getMessage());
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,47 @@
package vs;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Date;
/**
* Server for var.sockets.udp.messwerte.MesswertServer service. Empfängt Messwerte von Clients und
* gibt sie aus.
*
* @author Sandro Leuchter
*
*/
public class MesswertServer {
/**
* port on which service is running on host
*/
private static final int PORT = 4713;
/**
* maximum size of payload in datagram
*/
private static final int BUFSIZE = 512;
/**
* main method: entrypoint to run
*
* @param args
* ignored
*/
public static void main(final String[] args) {
try (DatagramSocket socket = new DatagramSocket(PORT)) {
DatagramPacket packetIn = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
System.out.println("Server gestartet ...");
while (true) {
socket.receive(packetIn);
String jetzt = (new Date()).toString();
System.out.println(packetIn.getAddress().getHostAddress() + ":" + packetIn.getPort() + " "
+ jetzt + " " + new String(packetIn.getData()));
}
} catch (IOException e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,6 @@
import '../justfile'
server:
just exec vs.TimeServer ""
client:
just exec vs.TimeClient ""

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>udp.time</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>udp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,8 @@
package vs;
public class TimeClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,8 @@
package vs;
public class TimeServer {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,6 @@
import '../justfile'
server:
just exec vs.TimeServer ""
client:
just exec vs.TimeClient ""

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>udp.time_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>udp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,57 @@
package vs;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
/**
* Client for echo var.sockets.udp.time.TimeServer service. Sendet leeres Paket an Server, wartet
* bis TIMEOUT aus Antwort vom Server, liest die Antwort und gibt sie aus
*
* @author Sandro Leuchter
*
*/
public class TimeClient {
/**
* host on which server is running (IP-address or hostname)
*/
private static final String HOST = "localhost";
/**
* port on which service is running on host
*/
private static final int PORT = 4712;
/**
* maximum size of payload in datagram
*/
private static final int BUFSIZE = 512;
/**
* timeout in ms for waiting for a response from server
*/
private static final int TIMEOUT = 2000;
/**
* main method: entrypoint to run
*
* @param args
* ignored
*/
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) {
socket.setSoTimeout(TIMEOUT); // Zeit in ms, für wie lange ein read() auf socket blockiert.
// Bei timeout is java.net.SocketTimeoutException (TIMEOUT == 0
// => blockiert für immer)
InetAddress iaddr = InetAddress.getByName(HOST);
DatagramPacket packetOut = new DatagramPacket(new byte[0], 0, iaddr, PORT);
socket.send(packetOut);
DatagramPacket packetIn = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
socket.receive(packetIn);
String received = new String(packetIn.getData(), 0, packetIn.getLength());
System.out.println("Received: " + received);
} catch (SocketTimeoutException e) {
System.err.println("Timeout: " + e.getMessage());
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,53 @@
package vs;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Date;
/**
* Server for echo var.sockets.udp.time.TimeServer service. Empfängt Datagramm von Client, liest
* Absenderinformationen daraus und sendet Zeitinformation zurück.
*
* @author Sandro Leuchter
*
*/
public class TimeServer {
/**
* port on which service is running on host
*/
private static final int PORT = 4712;
/**
* maximum size of payload in datagram
*/
private static final int BUFSIZE = 512;
/**
* main method: entrypoint to run
*
* @param args
* ignored
*/
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(PORT)) {
DatagramPacket packetIn = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
DatagramPacket packetOut = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
System.out.println("Server gestartet ...");
while (true) {
socket.receive(packetIn);
System.out.println(
"Received from: " + packetIn.getAddress().getHostAddress() + ":" + packetIn.getPort());
String jetzt = (new Date()).toString();
packetOut.setData(jetzt.getBytes());
packetOut.setLength(jetzt.length());
packetOut.setSocketAddress(packetIn.getSocketAddress());
socket.send(packetOut);
}
} catch (final IOException e) {
System.err.println(e);
}
}
}

4
03-tcp/justfile 100644
View File

@ -0,0 +1,4 @@
import '../justfile'
zed:
$VISUAL tcp.echo tcp.echo_solution tcp.filer tcp.filer_solution tcp.time tcp.time_solution

14
03-tcp/pom.xml 100644
View File

@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>tcp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,39 @@
{
"folders": [
{
"name": "1. Echo Service",
"path": "tcp.echo",
},
{
"name": "1. Echo Service (ML)",
"path": "tcp.echo_solution",
},
{
"name": "2. File Service",
"path": "tcp.filer",
},
{
"name": "2. File Service (ML)",
"path": "tcp.filer_solution",
},
{
"name": "3. Time Service",
"path": "tcp.time",
},
{
"name": "3. Time Service (ML)",
"path": "tcp.time_solution",
},
],
"extensions": {
"recommendations": [
"vscjava.vscode-java-pack",
"ms-azuretools.vscode-docker",
"skellock.just",
],
},
"settings": {
"java.dependency.syncWithFolderExplorer": true,
"java.project.explorer.showNonJavaResources": false,
},
}

View File

@ -0,0 +1,11 @@
import '../justfile'
port := "4747"
server-iterativ backlog_size:
just exec vs.EchoServerIterativ "{{port}} {{backlog_size}}"
server-threaded:
just exec vs.EchoServerThreaded {{port}}
client host:
just exec vs.EchoClient "{{host}} {{port}}"

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>tcp.echo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>tcp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,37 @@
package vs;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class EchoClient {
public static void main(String[] args) {
String host = args[0];
int port = Integer.parseInt(args[1]);
try (Socket socket = new Socket(host, port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in))) {
// Begrüßung vom Server empfangen und auf Konsole ausgeben
String msg = in.readLine();
System.out.println(msg);
// Zeile von Konsole einlesen, an Server senden und Antwort von
// Server auf Konsole ausgeben, bis eingegebene Zeile == "q"
while (true) {
System.out.print(">> ");
String line = stdin.readLine();
if ("q".equals(line)) {
break;
}
out.println(line);
System.out.println(in.readLine());
}
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,60 @@
package vs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
public class EchoServerIterativ {
private int port;
private int backlog;
public EchoServerIterativ(int port, int backlog) {
this.port = port;
this.backlog = backlog;
}
public void start() {
try (ServerSocket serverSocket = new ServerSocket(port, backlog)) {
System.out.println("EchoServer (iterativ) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
while (true) {
handleClient(serverSocket);
}
} catch (IOException e) {
System.err.println(e);
}
}
private void handleClient(ServerSocket server) {
SocketAddress socketAddress = null;
try (Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
socketAddress = socket.getRemoteSocketAddress();
System.out.println("Verbindung zu " + socketAddress + " aufgebaut");
out.println("Server ist bereit ...");
String input;
while ((input = in.readLine()) != null) {
System.out.println(socketAddress + ">> [" + input + "]");
out.println("echo: " + input);
}
} catch (IOException e) {
System.err.println(e);
} finally {
System.out.println("Verbindung zu " + socketAddress + " abgebaut");
}
}
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
int backlog = 50;
if (args.length == 2) {
backlog = Integer.parseInt(args[1]);
}
new EchoServerIterativ(port, backlog).start();
}
}

View File

@ -0,0 +1,41 @@
package vs;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServerThreaded {
private int port;
public EchoServerThreaded(int port) {
this.port = port;
}
public void start() {
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("EchoServer (threaded) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
// hier müssen Verbindungswünsche von Clients in einem neuen Thread
// angenommen werden
} catch (IOException e) {
System.err.println(e);
}
}
private class EchoThread extends Thread {
private Socket socket;
public EchoThread(Socket socket) {
this.socket = socket;
}
public void run() {
// hier muss die Verbindung mit dem Client über this.socket
// abgearbeitet werden
}
}
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
new EchoServerThreaded(port).start();
}
}

View File

@ -0,0 +1,13 @@
import '../justfile'
port := "4747"
server-iterativ backlog_size:
just exec vs.EchoServerIterativ "{{port}} {{backlog_size}}"
server-threaded:
just exec vs.EchoServerThreaded {{port}}
server-pool:
just exec vs.EchoServerThreadPool {{port}}
client host:
just exec vs.EchoClient "{{host}} {{port}}"

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>tcp.echo_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>tcp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,53 @@
package vs;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
* Client for echo var.sockets.tcp.echo.EchoServer* service. Liest zeilenweise
* von der Console und sendet zum Server. Der Server sendet eine Zeichenkette
* zurück, die auf der Konsole ausgegeben wird.
*
* @author Sandro Leuchter
*
*/
public class EchoClient {
/**
* main method: entrypoint to run
*
* @param args address of service to connect to (must be String[0]: host
* (IP-address or DNS hostname), String[1]: port)
*
*/
public static void main(String[] args) {
String host = args[0];
int port = Integer.parseInt(args[1]);
try (Socket socket = new Socket(host, port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in))) {
// Begrüßung vom Server empfangen und auf Konsole ausgeben
String msg = in.readLine();
System.out.println(msg);
// Zeile von Konsole einlesen, an Server senden und Antwort von
// Server auf Konsole ausgeben, bis eingegebene Zeile == "q"
while (true) {
System.out.print(">> ");
String line = stdin.readLine();
if ("q".equals(line)) {
break;
}
out.println(line);
System.out.println(in.readLine());
}
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,98 @@
package vs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
/**
* iterative server for var.sockets.tcp.echo Echo service. waits for the next
* client to connect, sends greeting message to client, reads line by line from
* client and sends it back adding "echo: " in front of each line until
* connection is closed by client.
*
* @author Sandro Leuchter
*
*/
public class EchoServerIterativ {
/**
* port on which this service is currently listening on localhost
*/
private final int port;
/**
* current maximum length of the queue of incoming connections.
*/
private final int backlog;
/**
* the only constructor for this class
*
* @param port port on which this service will be listening on localhost
* @param backlog requested maximum length of the queue of incoming connections.
*/
public EchoServerIterativ(int port, int backlog) {
this.port = port;
this.backlog = backlog;
}
/**
* creates server socket on localhost:port, infinitely handles connections to
* clients one after another
*/
public void start() {
try (ServerSocket serverSocket = new ServerSocket(this.port, this.backlog)) {
System.out.println("EchoServer (iterativ) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
while (true) {
handleClient(serverSocket);
}
} catch (IOException e) {
System.err.println(e);
}
}
/**
* waits for the next client to connect to server, sends greeting message to
* client, reads line by line from client and sends it back adding "echo: " in
* front of each line until connection is closed by client.
*
* @param server "welcome socket" on which server is listening for clients
*/
private void handleClient(ServerSocket server) {
SocketAddress socketAddress = null;
try (Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
socketAddress = socket.getRemoteSocketAddress();
System.out.println("Verbindung zu " + socketAddress + " aufgebaut");
out.println("Server ist bereit ...");
String input;
while ((input = in.readLine()) != null) {
System.out.println(socketAddress + ">> [" + input + "]");
out.println("echo: " + input);
}
} catch (IOException e) {
System.err.println(e);
} finally {
System.out.println("Verbindung zu " + socketAddress + " abgebaut");
}
}
/**
* main method: entrypoint to run service
*
* @param args args[0] must be the port number of the server (int); rest of args
* is ignored
*/
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
int backlog = 50;
if (args.length == 2) {
backlog = Integer.parseInt(args[1]);
}
new EchoServerIterativ(port, backlog).start();
}
}

View File

@ -0,0 +1,115 @@
package vs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* threaded server for var.sockets.tcp.echo Echo service. waits for the next
* client to connect, creates thread and handles connection in concurrently:
* sends greeting message to client, reads line by line from client and sends it
* back adding "echo: " in front of each line until connection is closed by
* client.
*
* @author Sandro Leuchter
*
*/
public class EchoServerThreadPool {
/**
* port on which this service is currently listening on localhost
*/
private final int port;
/**
* thread pool of this server
*/
private final Executor threadPool;
/**
* the only constructor for this class
*
* @param port port on which this service will be listening on localhost
*/
public EchoServerThreadPool(int port) {
this.port = port;
// threadPool = Executors.newSingleThreadExecutor();
// threadPool = Executors.newCachedThreadPool();
this.threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
}
/**
* creates server socket on localhost:port, infinitely handles connections to
* clients concurrently
*/
public void start() {
try (ServerSocket serverSocket = new ServerSocket(this.port)) {
System.out.println("EchoServer (threaded) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
while (true) {
Socket socket = serverSocket.accept();
this.threadPool.execute(new EchoThread(socket));
}
} catch (IOException e) {
System.err.println(e);
}
}
/**
* Each connection is handled with an instance of this class.
*/
private class EchoThread implements Runnable {
/**
* TCP connection to client
*/
private final Socket socket;
/**
* the only constructor for this class
*
* @param socket the individual socket that the server created on accepting a
* client that this EchoThread instance will be communicating with
*/
public EchoThread(Socket socket) {
this.socket = socket;
}
/**
* defines the behavior of this Thread instance, will be executed concurrently
* if start() is called on instance
*
*/
@Override
public void run() {
SocketAddress socketAddress = this.socket.getRemoteSocketAddress();
System.out.println("Verbindung zu " + socketAddress + " aufgebaut");
try (BufferedReader in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true)) {
out.println("Server ist bereit ...");
String input;
while ((input = in.readLine()) != null) {
System.out.println(socketAddress + ">> [" + input + "]");
out.println("echo: " + input);
}
} catch (Exception e) {
System.err.println(e);
} finally {
System.out.println("Verbindung zu " + socketAddress + " abgebaut");
}
}
}
/**
* main method: entrypoint to run service
*
* @param args args[0] must be the port number of the server (int); rest of args
* is ignored
*/
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
new EchoServerThreadPool(port).start();
}
}

View File

@ -0,0 +1,106 @@
package vs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
/**
* threaded server for var.sockets.tcp.echo Echo service. waits for the next
* client to connect, creates thread and handles connection in concurrently:
* sends greeting message to client, reads line by line from client and sends it
* back adding "echo: " in front of each line until connection is closed by
* client.
*
* @author Sandro Leuchter
*
*/
public class EchoServerThreaded {
/**
* port on which this service is currently listening on localhost
*/
private final int port;
/**
* the only constructor for this class
*
* @param port port on which this service will be listening on localhost
*/
public EchoServerThreaded(int port) {
this.port = port;
}
/**
* creates server socket on localhost:port, infinitely handles connections to
* clients concurrently
*/
public void start() {
try (ServerSocket serverSocket = new ServerSocket(this.port)) {
System.out.println("EchoServer (threaded) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
while (true) {
Socket socket = serverSocket.accept();
new EchoThread(socket).start();
}
} catch (IOException e) {
System.err.println(e);
}
}
/**
* Each connection is handled with an instance of this class.
*/
private class EchoThread extends Thread {
/**
* TCP connection to client
*/
private final Socket socket;
/**
* the only constructor for this class
*
* @param socket the individual socket that the server created on accepting a
* client that this EchoThread instance will be communicating with
*/
public EchoThread(Socket socket) {
this.socket = socket;
}
/**
* defines the behavior of this Thread instance, will be executed concurrently
* if start() is called on instance
*
*/
@Override
public void run() {
SocketAddress socketAddress = this.socket.getRemoteSocketAddress();
System.out.println("Verbindung zu " + socketAddress + " aufgebaut");
try (BufferedReader in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true)) {
out.println("Server ist bereit ...");
String input;
while ((input = in.readLine()) != null) {
System.out.println(socketAddress + ">> [" + input + "]");
out.println("echo: " + input);
}
} catch (Exception e) {
System.err.println(e);
} finally {
System.out.println("Verbindung zu " + socketAddress + " abgebaut");
}
}
}
/**
* main method: entrypoint to run service
*
* @param args args[0] must be the port number of the server (int); rest of args
* is ignored
*/
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
new EchoServerThreaded(port).start();
}
}

View File

@ -0,0 +1,10 @@
import '../justfile'
port := "4747"
server-iterativ backlog_size:
just exec vs.FileServerIterativ "{{port}} {{backlog_size}}"
server-threaded:
just exec vs.FileServerThreaded {{port}}
client host:
just exec vs.FileClient "{{host}} {{port}}"

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>tcp.filer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>tcp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,22 @@
package vs;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class FileClient {
public static void main(String[] args) {
String host = args[0];
int port = Integer.parseInt(args[1]);
try (Socket socket = new Socket(host, port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,61 @@
package vs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
public class FileServerIterativ {
private static final String FILE = "target/classes/message.txt";
private int port;
private int backlog;
public FileServerIterativ(int port, int backlog) {
this.port = port;
this.backlog = backlog;
}
public void start() {
try (ServerSocket serverSocket = new ServerSocket(port, backlog)) {
System.out.println("FileServer (iterativ) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
File file = new File(FILE);
if (file.exists()) {
System.out.println("\"" + file.getAbsolutePath() + "\" soll gesendet werden.");
while (true) {
handleClient(serverSocket);
}
}
} catch (IOException e) {
System.err.println(e);
}
}
private void handleClient(ServerSocket server) {
SocketAddress socketAddress = null;
try (Socket socket = server.accept();
BufferedReader in = new BufferedReader(new FileReader(FILE));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
socketAddress = socket.getRemoteSocketAddress();
System.out.println("Verbindung zu " + socketAddress + " aufgebaut");
// Inhalt von in zeilenweise an out senden
} catch (IOException e) {
System.err.println(e);
} finally {
System.out.println("Verbindung zu " + socketAddress + " abgebaut");
}
}
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
int backlog = 50;
if (args.length == 2) {
backlog = Integer.parseInt(args[1]);
}
new FileServerIterativ(port, backlog).start();
}
}

View File

@ -0,0 +1,49 @@
package vs;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class FileServerThreaded {
private static final String FILE = "target/classes/message.txt";
private int port;
public FileServerThreaded(int port) {
this.port = port;
}
public void start() {
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("FileServer (threaded) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
File file = new File(FILE);
if (file.exists()) {
System.out.println("\"" + file.getAbsolutePath() + "\" soll gesendet werden.");
while (true) {
// hier müssen Verbindungswünsche von Clients in einem neuen
// Thread angenommen werden
}
}
} catch (IOException e) {
System.err.println(e);
}
}
private class FileThread extends Thread {
private Socket socket;
public FileThread(Socket socket) {
this.socket = socket;
}
public void run() {
// hier muss die Verbindung mit dem Client über this.socket
// abgearbeitet werden
}
}
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
new FileServerThreaded(port).start();
}
}

View File

@ -0,0 +1,25 @@
Mannheimer Mittelstandsmesse 2017
=================================
Zum zehnten Mal lädt die Hochschule Mannheim zur Mittelstandsmesse (MaMi) ein. 14 Unternehmen,
die sich beim Mannheimer Modell Mittelstands-Stipendien engagieren, präsentierten sich in
der Aula der Hochschule
Studierende der Hochschule haben am 04.04.2017 von 12.00 16.00 Uhr wieder die Gelegenheit,
Kontakte zu mittelständischen Unternehmen der Metropolregion zu knüpfen, Möglichkeiten für
Praktika auszuloten oder mit Personalverantwortlichen über Stellenangebote zu sprechen. Auch
für Lehrende und Forschende ist die Messe eine gute Gelegenheit, mit interessierten Unternehmen
Kooperationen anzubahnen oder zu vertiefen.
Ziel der Messe ist, die Sichtbarkeit von mittelständischen Unternehmen in der Metropolregion
Rhein-Neckar zu erhöhen und Hochschulabsolventen die Attraktivität einer beruflichen Karriere
insbesondere im Mittelstand deutlich zu machen. Mittelständische Unternehmen prägen die
Wirtschaftsstruktur in Deutschland. 99 Prozent aller Unternehmen werden den mittelständischen
Unternehmen zugerechnet, sie beschäftigen rund 2/3 aller Erwerbstätigen.
Parallel zur Eröffnung der MaMi wird auch die Förderrunde 2017/18 für die Mannheimer
Mittelstands-Stipendien ausgeschrieben. Durch das seit 2007 etablierte Stipendienmodell, das
in tatkräftiger Kooperation der Hochschule mit mittelständischen Unternehmen und unterstützt
von der Metropolregion Rhein-Neckar ins Leben gerufen wurde, können im Studienjahr 30-40
Stipendien in Höhe von je 1.000 € vergeben werden. So ist die MaMi auch eine gute Gelegenheit
für Studierende, sich an den Ständen der Unternehmen als potenzielle Stipendiaten zu präsentieren.

View File

@ -0,0 +1,12 @@
import '../justfile'
port := "4747"
server-iterativ backlog_size:
just exec vs.FileServerIterativ "{{port}} {{backlog_size}}"
server-threaded:
just exec vs.FileServerThreaded {{port}}
server-pool:
just exec vs.FileServerThreadPool {{port}}
client host:
just exec vs.FileClient "{{host}} {{port}}"

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>tcp.filer_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>tcp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,39 @@
package vs;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* Client for echo var.sockets.tcp.filer.FileServer* service. Verbindet sich mit
* Server, empfängt dann zeilenweise vom Server und gibt auf der Konsole aus,
* was empfangen wurde. Empfängt und gibt so lange aus, bis der Server die
* Kommunikation beendet und den Socket schließt.
*
* @author Sandro Leuchter
*
*/
public class FileClient {
/**
* main method: entrypoint to run
*
* @param args address of service to connect to (must be String[0]: host
* (IP-address or DNS hostname), String[1]: port)
*
*/
public static void main(String[] args) {
String host = args[0];
int port = Integer.parseInt(args[1]);
try (Socket socket = new Socket(host, port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,109 @@
package vs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
/**
* iterative server for var.sockets.tcp.filer File service. waits for the next
* client to connect. Upon connection sends a manually defined file back to
* client. closes the connection directly afterwards and handles then next
* client
*
* @author Sandro Leuchter
*
*/
public class FileServerIterativ {
/**
* path to file which will be sent to clients; relative to current working
* directory (e.g. project root)
*/
private static final String FILE = "target/classes/message.txt";
/**
* port on which this service is currently listening on localhost
*/
private final int port;
/**
* requested maximum length of the queue of incoming connections.
*/
private final int backlog;
/**
* the only constructor for this class
*
* @param port port on which this service will be listening on localhost
* @param backlog requested maximum length of the queue of incoming connections.
*/
public FileServerIterativ(int port, int backlog) {
this.port = port;
this.backlog = backlog;
}
/**
* creates server socket on localhost:port, infinitely handles connections to
* clients one after another
*/
public void start() {
try (ServerSocket serverSocket = new ServerSocket(this.port, this.backlog)) {
System.out.println("FileServer (iterativ) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
File file = new File(FILE);
if (file.exists()) {
System.out.println("\"" + file.getAbsolutePath() + "\" soll gesendet werden.");
while (true) {
handleClient(serverSocket);
}
}
} catch (IOException e) {
System.err.println(e);
}
}
/**
* waits for the next client to connect. Upon connection sends a manually
* defined file back to client. closes the connection directly afterwards and
* handles then next client
*
* @param server "welcome socket" on which server is listening for clients
*/
private void handleClient(ServerSocket server) {
SocketAddress socketAddress = null;
try (Socket socket = server.accept();
BufferedReader in = new BufferedReader(new FileReader(FILE));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
socketAddress = socket.getRemoteSocketAddress();
System.out.println("Verbindung zu " + socketAddress + " aufgebaut");
// Inhalt von in zeilenweise an out senden:
System.out.println("Übertragung zu " + socketAddress + " begonnen");
String input;
while ((input = in.readLine()) != null) {
out.println(input);
}
System.out.println("Übertragung zu " + socketAddress + " beendet");
} catch (IOException e) {
System.err.println(e);
} finally {
System.out.println("Verbindung zu " + socketAddress + " abgebaut");
}
}
/**
* main method: entrypoint to run service
*
* @param args args[0] must be the port number of the server (int); rest of args
* is ignored
*/
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
int backlog = 50;
if (args.length == 2) {
backlog = Integer.parseInt(args[1]);
}
new FileServerIterativ(port, backlog).start();
}
}

View File

@ -0,0 +1,128 @@
package vs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* threaded server for var.sockets.tcp.filer File service. waits for clients to
* connect. Upon connection sends concurrently to other client connections a
* manually defined file back to client. closes the connection directly
* afterwards.
*
* @author Sandro Leuchter
*
*/
public class FileServerThreadPool {
/**
* path to file which will be sent to clients; relative to current working
* directory (e.g. project root)
*/
private static final String FILE = "target/classes/message.txt";
/**
* port on which this service is currently listening on localhost
*/
private final int port;
/**
* thread pool of this server
*/
private final Executor threadPool;
/**
* the only constructor for this class
*
* @param port port on which this service will be listening on localhost
*/
public FileServerThreadPool(int port) {
this.port = port;
this.threadPool = Executors.newSingleThreadExecutor();
// threadPool = Executors.newCachedThreadPool();
// threadPool =
// Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*2);
}
/**
* creates server socket on localhost:port, infinitely handles connections to
* clients concurrently
*/
public void start() {
try (ServerSocket serverSocket = new ServerSocket(this.port)) {
System.out.println("FileServer (threaded) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
File file = new File(FILE);
if (file.exists()) {
System.out.println("\"" + file.getAbsolutePath() + "\" soll gesendet werden.");
while (true) {
Socket socket = serverSocket.accept();
this.threadPool.execute(new FileThread(socket));
}
}
} catch (IOException e) {
System.err.println(e);
}
}
/**
* Each connection is handled with an instance of this class.
*/
private class FileThread implements Runnable {
/**
* TCP connection to client
*/
private final Socket socket;
/**
* the only constructor for this class
*
* @param socket the individual socket that the server created on accepting a
* client that this EchoThread instance will be communicating with
*/
public FileThread(Socket socket) {
this.socket = socket;
}
/**
* defines the behavior of this Thread instance, will be executed concurrently
* if start() is called on instance
*
*/
@Override
public void run() {
SocketAddress socketAddress = null;
try (BufferedReader in = new BufferedReader(new FileReader(FILE));
PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true)) {
socketAddress = this.socket.getRemoteSocketAddress();
System.out.println("Verbindung zu " + socketAddress + " aufgebaut");
// Inhalt von in zeilenweise an out senden:
System.out.println("Übertragung zu " + socketAddress + " begonnen");
String input;
while ((input = in.readLine()) != null) {
out.println(input);
// Thread.sleep(1000);
}
System.out.println("Übertragung zu " + socketAddress + " beendet");
} catch (Exception e) {
System.err.println(e);
} finally {
System.out.println("Verbindung zu " + socketAddress + " abgebaut");
}
}
}
/**
* main method: entrypoint to run service
*
* @param args args[0] must be the port number of the server (int); rest of args
* is ignored
*/
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
new FileServerThreadPool(port).start();
}
}

View File

@ -0,0 +1,118 @@
package vs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
/**
* threaded server for var.sockets.tcp.filer File service. waits for clients to
* connect. Upon connection sends concurrently to other client connections a
* manually defined file back to client. closes the connection directly
* afterwards.
*
* @author Sandro Leuchter
*
*/
public class FileServerThreaded {
/**
* path to file which will be sent to clients; relative to current working
* directory (e.g. project root)
*/
private static final String FILE = "target/classes/message.txt";
/**
* port on which this service is currently listening on localhost
*/
private final int port;
/**
* the only constructor for this class
*
* @param port port on which this service will be listening on localhost
*/
public FileServerThreaded(int port) {
this.port = port;
}
/**
* creates server socket on localhost:port, infinitely handles connections to
* clients concurrently
*/
public void start() {
try (ServerSocket serverSocket = new ServerSocket(this.port)) {
System.out.println("FileServer (threaded) auf " + serverSocket.getLocalSocketAddress() + " gestartet ...");
File file = new File(FILE);
if (file.exists()) {
System.out.println("\"" + file.getAbsolutePath() + "\" soll gesendet werden.");
while (true) {
Socket socket = serverSocket.accept();
new FileThread(socket).start();
}
}
} catch (IOException e) {
System.err.println(e);
}
}
/**
* Each connection is handled with an instance of this class.
*/
private class FileThread extends Thread {
/**
* TCP connection to client
*/
private final Socket socket;
/**
* the only constructor for this class
*
* @param socket the individual socket that the server created on accepting a
* client that this EchoThread instance will be communicating with
*/
public FileThread(Socket socket) {
this.socket = socket;
}
/**
* defines the behavior of this Thread instance, will be executed concurrently
* if start() is called on instance
*
*/
@Override
public void run() {
SocketAddress socketAddress = null;
try (BufferedReader in = new BufferedReader(new FileReader(FILE));
PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true)) {
socketAddress = this.socket.getRemoteSocketAddress();
System.out.println("Verbindung zu " + socketAddress + " aufgebaut");
// Inhalt von in zeilenweise an out senden:
System.out.println("Übertragung zu " + socketAddress + " begonnen");
String input;
while ((input = in.readLine()) != null) {
out.println(input);
// Thread.sleep(1000);
}
System.out.println("Übertragung zu " + socketAddress + " beendet");
} catch (Exception e) {
System.err.println(e);
} finally {
System.out.println("Verbindung zu " + socketAddress + " abgebaut");
}
}
}
/**
* main method: entrypoint to run service
*
* @param args args[0] must be the port number of the server (int); rest of args
* is ignored
*/
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
new FileServerThreaded(port).start();
}
}

View File

@ -0,0 +1,25 @@
Mannheimer Mittelstandsmesse 2017
=================================
Zum zehnten Mal lädt die Hochschule Mannheim zur Mittelstandsmesse (MaMi) ein. 14 Unternehmen,
die sich beim Mannheimer Modell Mittelstands-Stipendien engagieren, präsentierten sich in
der Aula der Hochschule
Studierende der Hochschule haben am 04.04.2017 von 12.00 16.00 Uhr wieder die Gelegenheit,
Kontakte zu mittelständischen Unternehmen der Metropolregion zu knüpfen, Möglichkeiten für
Praktika auszuloten oder mit Personalverantwortlichen über Stellenangebote zu sprechen. Auch
für Lehrende und Forschende ist die Messe eine gute Gelegenheit, mit interessierten Unternehmen
Kooperationen anzubahnen oder zu vertiefen.
Ziel der Messe ist, die Sichtbarkeit von mittelständischen Unternehmen in der Metropolregion
Rhein-Neckar zu erhöhen und Hochschulabsolventen die Attraktivität einer beruflichen Karriere
insbesondere im Mittelstand deutlich zu machen. Mittelständische Unternehmen prägen die
Wirtschaftsstruktur in Deutschland. 99 Prozent aller Unternehmen werden den mittelständischen
Unternehmen zugerechnet, sie beschäftigen rund 2/3 aller Erwerbstätigen.
Parallel zur Eröffnung der MaMi wird auch die Förderrunde 2017/18 für die Mannheimer
Mittelstands-Stipendien ausgeschrieben. Durch das seit 2007 etablierte Stipendienmodell, das
in tatkräftiger Kooperation der Hochschule mit mittelständischen Unternehmen und unterstützt
von der Metropolregion Rhein-Neckar ins Leben gerufen wurde, können im Studienjahr 30-40
Stipendien in Höhe von je 1.000 € vergeben werden. So ist die MaMi auch eine gute Gelegenheit
für Studierende, sich an den Ständen der Unternehmen als potenzielle Stipendiaten zu präsentieren.

View File

@ -0,0 +1,10 @@
import '../justfile'
port := "4747"
server-long:
just exec vs.TimeLongServer {{port}}
server-text:
just exec vs.TimeTextServer {{port}}
client host:
just exec vs.TimeClient "{{host}} {{port}}"

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>tcp.time</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>tcp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,23 @@
package vs;
import java.io.InputStream;
import java.net.Socket;
public class TimeClient {
public static void main(String[] args) {
try (Socket socket = new Socket(args[0], Integer.parseInt(args[1]));
InputStream in = socket.getInputStream()) {
StringBuilder stringBuilder = new StringBuilder();
int c;
while ((c = in.read()) != -1) {
stringBuilder.append((char) c);
}
// stringBuilder-Inhalt in ein Date-Objekt konvertieren und ausgeben
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,38 @@
package vs;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.util.Date;
public class TimeLongServer {
private int port;
public TimeLongServer(int port) {
this.port = port;
}
public void startServer() {
try (ServerSocket serverSocket = new ServerSocket(port)) {
while (true) {
try (Socket socket = serverSocket.accept();
PrintWriter out = new PrintWriter(socket.getOutputStream())) {
Date now = new Date();
long currentTime = // Zeit von now in ms seit 01.01.1970 00:00:00 GMT abrufen
out.print(currentTime);
out.flush();
} catch (IOException e) {
System.err.println(e);
}
}
} catch (IOException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
new TimeLongServer(Integer.parseInt(args[0])).startServer();
}
}

View File

@ -0,0 +1,38 @@
package vs;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.util.Date;
public class TimeTextServer {
private int port;
public TimeTextServer(int port) {
this.port = port;
}
public void startServer() {
try (ServerSocket serverSocket = new ServerSocket(port)) {
while (true) {
try (Socket socket = serverSocket.accept();
PrintWriter out = new PrintWriter(socket.getOutputStream())) {
Date now = new Date();
String currentTime = // DateFormat Instanz holen und mit dessen format Methode now zum String machen
out.print(currentTime);
out.flush();
} catch (IOException e) {
System.err.println(e);
}
}
} catch (IOException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
new TimeTextServer(Integer.parseInt(args[0])).startServer();
}
}

View File

@ -0,0 +1,10 @@
import '../justfile'
port := "4747"
server-long:
just exec vs.TimeLongServer {{port}}
server-text:
just exec vs.TimeTextServer {{port}}
client host:
just exec vs.TimeClient "{{host}} {{port}}"

View File

@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>tcp.time_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>tcp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,63 @@
package vs;
import java.io.InputStream;
import java.net.Socket;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
/**
* Client for echo var.sockets.tcp.time.Time*Server service. Verbindet sich mit
* dem Server und empfängt eine Repräsentation eines Zeitstempels. Das Format
* des Zeitstempels kann entweder die Zeit in ms seit dem 01.01.1970 00:00:00
* GMT als ASCII-Zeichen sein oder ein String, der die Zeit kompatibel zu ISO
* 8601 enthält.
*
* @author Sandro Leuchter
*
*/
public class TimeClient {
/**
* main method: entrypoint to run
*
* @param args address of service to connect to (must be String[0]: host
* (IP-address or DNS hostname), String[1]: port)
*
*/
public static void main(String[] args) {
try (Socket socket = new Socket(args[0], Integer.parseInt(args[1])); InputStream in = socket.getInputStream()) {
StringBuilder stringBuilder = new StringBuilder();
int c;
while ((c = in.read()) != -1) {
stringBuilder.append((char) c);
}
// stringBuilder-Inhalt in ein Date-Objekt konvertieren und ausgeben
DateFormat dateFormatter = DateFormat.getInstance();
Date date = null;
try {
date = dateFormatter.parse(stringBuilder.toString());
} catch (ParseException parseException) {
try {
date = new Date(Long.parseLong(stringBuilder.toString()));
} catch (NumberFormatException numberException) {
// weder verständliches Textformat, noch long-Zahl (ms. seit
// 01.01.1970 00:00)
// System.err.println(numberException);
}
}
System.out.println("empfangen: \"" + stringBuilder.toString() + "\"");
if (date != null) {
System.out.println("gewandelt: " + date);
} else {
// weder verständliches Textformat, noch long-Zahl (ms. seit
// 01.01.1970 00:00)
System.err.println("es war nicht mögich ein Date-Objekt daraus zu erzeugen.");
}
} catch (Exception e) {
System.err.println(e);
}
}
}

View File

@ -0,0 +1,65 @@
package vs;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
/**
* iterative server for var.sockets.tcp.time Time service. waits for the next
* client to connect, then sends time back. format for time is ASCII
* representation of ms since begin of epoch: Jan 1, 1970 00:00:00 GMT.
*
* @author Sandro Leuchter
*
*/
public class TimeLongServer {
/**
* port on which this service is currently listening on localhost
*/
private final int port;
/**
* the only constructor for this class
*
* @param port port on which this service will be listening on localhost
*/
public TimeLongServer(int port) {
this.port = port;
}
/**
* creates server socket on localhost:port, infinitely handles connections to
* clients one after another: waits for the next client to connect, send time
* back. format for time is ASCII representation of ms since begin of epoch: Jan
* 1, 1970 00:00:00 GMT.
*/
public void startServer() {
try (ServerSocket serverSocket = new ServerSocket(this.port)) {
while (true) {
try (Socket socket = serverSocket.accept();
PrintWriter out = new PrintWriter(socket.getOutputStream())) {
Date now = new Date();
long currentTime = now.getTime();
out.print(currentTime);
out.flush();
} catch (IOException e) {
System.err.println(e);
}
}
} catch (IOException e) {
System.err.println(e);
}
}
/**
* main method: entrypoint to run service
*
* @param args args[0] must be the port number of the server (int); rest of args
* is ignored
*/
public static void main(String[] args) {
new TimeLongServer(Integer.parseInt(args[0])).startServer();
}
}

View File

@ -0,0 +1,65 @@
package vs;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.util.Date;
/**
* iterative server for var.sockets.tcp.time Time service. waits for the next
* client to connect, then sends time back. format for time is short date and
* time according to current locale.
*
* @author Sandro Leuchter
*
*/
public class TimeTextServer {
/**
* port on which this service is currently listening on localhost
*/
private final int port;
/**
* the only constructor for this class
*
* @param port port on which this service will be listening on localhost
*/
public TimeTextServer(int port) {
this.port = port;
}
/**
* creates server socket on localhost:port, infinitely handles connections to
* clients one after another: waits for the next client to connect, send time
* back. format for time is short date and time according to current locale.
*/
public void startServer() {
try (ServerSocket serverSocket = new ServerSocket(this.port)) {
while (true) {
try (Socket socket = serverSocket.accept();
PrintWriter out = new PrintWriter(socket.getOutputStream())) {
Date now = new Date();
String currentTime = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(now);
out.print(currentTime);
out.flush();
} catch (IOException e) {
System.err.println(e);
}
}
} catch (IOException e) {
System.err.println(e);
}
}
/**
* main method: entrypoint to run
*
* @param args args[0] must be the port number of the server (int); rest of args
* is ignored
*/
public static void main(String[] args) {
new TimeTextServer(Integer.parseInt(args[0])).startServer();
}
}

View File

@ -0,0 +1,8 @@
import '../justfile'
chatter1:
just chatter Dave
chatter2:
just chatter HAL9000
chatter name:
just exec vs.ChatClient "{{name}}"

View File

@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>jms.chat</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>jms</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,69 @@
package vs;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageListener;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
// Code aus JMS-Client mit Umbenennung des Typs von JMSClient zu ChatClient und Anpassung des packages
public class ChatClient implements MessageListener {
private Connection connection;
private Session session;
private MessageConsumer consumer;
public ChatClient() throws NamingException, JMSException {
Context ctx = new InitialContext();
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
Destination queue = (Destination) ctx.lookup(Conf.QUEUE);
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(queue);
consumer.setMessageListener(this);
connection.start();
}
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String messageText = textMessage.getText();
String priority = textMessage.getStringProperty("Priority");
System.out.println(messageText + " [Priority=" + priority + "]");
}
} catch (JMSException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
long wait = Long.parseLong(args[0]);
ChatClient node = null;
try {
node = new ChatClient();
Thread.sleep(wait);
} catch (InterruptedException | NamingException | JMSException e) {
System.err.println(e);
} finally {
try {
if (node != null && node.consumer != null)
node.consumer.close();
if (node != null && node.session != null)
node.session.close();
if (node != null && node.connection != null)
node.connection.close();
} catch (JMSException e) {
System.err.println(e);
}
}
}
}

View File

@ -0,0 +1,5 @@
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616
topic.vs.channel = topic4735

View File

@ -0,0 +1,15 @@
import '../justfile'
chatter1:
just chatter Dave
chatter2:
just chatter HAL9000
chatter name:
just exec vs.ChatClient "{{name}}"
chatterA:
just chatter-prop Dave
chatterB:
just chatter-prop HAL9000
chatter-prop name:
just exec vs.ChatClientUserProperty "{{name}}"

View File

@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>jms.chat_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>jms</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,111 @@
package vs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageListener;
import jakarta.jms.MessageProducer;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* Chat Client using publish/subscribe on JMS provider Topic; represents user
* name as part of message payload
*
* @author Sandro Leuchter
*
*/
public class ChatClient implements MessageListener {
private Connection connection;
private Session session;
private MessageProducer producer;
private MessageConsumer consumer;
/**
* constructor, establishes and starts connection to JMS provider specified in
* JNDI (via jndi.properties), afterwards producer and consumer are ready
*
* @param sendDest Destination for producer
* @param receiveDest Destination for consumer
*
* @throws NamingException JNDI exceptions
* @throws JMSException JMS exceptions
* @see jakarta.jms.Destination
*/
public ChatClient(String sendDest, String receiveDest) throws NamingException, JMSException {
Context ctx = new InitialContext();
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
this.connection = factory.createConnection();
this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destOut = (Destination) ctx.lookup(sendDest);
Destination destIn = (Destination) ctx.lookup(receiveDest);
this.producer = this.session.createProducer(destOut);
this.consumer = this.session.createConsumer(destIn);
this.consumer.setMessageListener(this);
this.connection.start();
}
/**
* asynchronous message consumption
*
* @see jakarta.jms.MessageListener
*/
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
System.err.println(e);
}
}
}
/**
* main routine and starting point of program
*
* @param args[0] user name
*/
public static void main(String[] args) {
ChatClient node = null;
try {
node = new ChatClient(Conf.TOPIC, Conf.TOPIC);
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line;
while (true) {
line = input.readLine();
node.producer.send(node.session.createTextMessage(args[0] + "> " + line));
}
} catch (NamingException | JMSException | IOException e) {
System.err.println(e);
} finally {
try {
if ((node != null) && (node.producer != null)) {
node.producer.close();
}
if ((node != null) && (node.consumer != null)) {
node.consumer.close();
}
if ((node != null) && (node.session != null)) {
node.session.close();
}
if ((node != null) && (node.connection != null)) {
node.connection.close();
}
} catch (JMSException e) {
System.err.println(e);
}
}
}
}

View File

@ -0,0 +1,113 @@
package vs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageListener;
import jakarta.jms.MessageProducer;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* Chat Client using publish/subscribe on JMS provider Topic; represents user
* name as String property of message
*
* @author Sandro Leuchter
*
*/
public class ChatClientUserProperty implements MessageListener {
private Connection connection;
private Session session;
private MessageProducer producer;
private MessageConsumer consumer;
/**
* constructor, establishes and starts connection to JMS provider specified in
* JNDI (via jndi.properties), afterwards producer and consumer are ready
*
* @param sendDest Destination for producer
* @param receiveDest Destination for consumer
*
* @throws NamingException JNDI exceptions
* @throws JMSException JMS exceptions
* @see jakarta.jms.Destination
*/
public ChatClientUserProperty(String sendDest, String receiveDest) throws NamingException, JMSException {
Context ctx = new InitialContext();
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
this.connection = factory.createConnection();
this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destOut = (Destination) ctx.lookup(sendDest);
Destination destIn = (Destination) ctx.lookup(receiveDest);
this.producer = this.session.createProducer(destOut);
this.consumer = this.session.createConsumer(destIn);
this.consumer.setMessageListener(this);
this.connection.start();
}
/**
* asynchronous message consumption
*
* @see jakarta.jms.MessageListener
*/
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println(textMessage.getStringProperty("user") + "> " + textMessage.getText());
} catch (JMSException e) {
System.err.println(e);
}
}
}
/**
* main routine and starting point of program
*
* @param args[0] user name
*/
public static void main(String[] args) {
ChatClientUserProperty node = null;
try {
node = new ChatClientUserProperty(Conf.TOPIC, Conf.TOPIC);
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line;
while (true) {
line = input.readLine();
TextMessage msg = node.session.createTextMessage(line);
msg.setStringProperty("user", args[0]);
node.producer.send(msg);
}
} catch (NamingException | JMSException | IOException e) {
System.err.println(e);
} finally {
try {
if ((node != null) && (node.producer != null)) {
node.producer.close();
}
if ((node != null) && (node.consumer != null)) {
node.consumer.close();
}
if ((node != null) && (node.session != null)) {
node.session.close();
}
if ((node != null) && (node.connection != null)) {
node.connection.close();
}
} catch (JMSException e) {
System.err.println(e);
}
}
}
}

View File

@ -0,0 +1,5 @@
package vs;
public class Conf {
public static final String TOPIC = "vs.chat";
}

View File

@ -0,0 +1,5 @@
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616
topic.vs.chat = topic4735

View File

@ -0,0 +1,4 @@
import '../justfile'
client:
just exec vs.JMSClient ""

View File

@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>jms.client</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>jms</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,84 @@
package vs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import jakarta.jms.BytesMessage;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageListener;
import jakarta.jms.MessageProducer;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JMSClient implements MessageListener {
private Connection connection;
private Session session;
private MessageProducer producer;
private MessageConsumer consumer;
public JMSClient(String sendDest, String receiveDest) throws NamingException, JMSException {
Context ctx = new InitialContext();
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destOut = (Destination) ctx.lookup(sendDest);
Destination destIn = (Destination) ctx.lookup(receiveDest);
producer = session.createProducer(destOut);
consumer = session.createConsumer(destIn);
consumer.setMessageListener(this);
connection.start();
}
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
System.err.println(e);
}
}
}
public static void main(String[] args) {
JMSClient node = null;
try {
node = new JMSClient("vs.queue1", "vs.queue2");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line;
while (true) {
line = input.readLine();
node.producer.send(node.session.createTextMessage(line));
}
} catch (NamingException | JMSException | IOException e) {
System.err.println(e);
} finally {
try {
if (node != null && node.producer != null) {
node.producer.close();
}
if (node != null && node.consumer != null) {
node.consumer.close();
}
if (node != null && node.session != null) {
node.session.close();
}
if (node != null && node.connection != null) {
node.connection.close();
}
} catch (JMSException e) {
System.err.println(e);
}
}
}
}

View File

@ -0,0 +1,6 @@
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616
queue.vs.queue1 = queue4731
queue.vs.queue2 = queue4732

View File

@ -0,0 +1,4 @@
import '../justfile'
client:
just exec vs.JMSClient ""

View File

@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>jms.client_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>jms</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,110 @@
package vs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageListener;
import jakarta.jms.MessageProducer;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* general JMS client with one producer and one consumer
*
* @author Sandro Leuchter
*
*/
public class JMSClient implements MessageListener {
private Connection connection;
private Session session;
private MessageProducer producer;
private MessageConsumer consumer;
/**
* constructor, establishes and starts connection to JMS provider specified in
* JNDI (via jndi.properties), afterwards producer and consumer are ready
*
* @param sendDest Destination for producer
* @param receiveDest Destination for consumer
*
* @throws NamingException JNDI exceptions
* @throws JMSException JMS exceptions
* @see jakarta.jms.Destination
*/
public JMSClient(String sendDest, String receiveDest) throws NamingException, JMSException {
Context ctx = new InitialContext();
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
this.connection = factory.createConnection();
this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destOut = (Destination) ctx.lookup(sendDest);
Destination destIn = (Destination) ctx.lookup(receiveDest);
this.producer = this.session.createProducer(destOut);
this.consumer = this.session.createConsumer(destIn);
this.consumer.setMessageListener(this);
this.connection.start();
}
/**
* asynchronous message consumption
*
* @see jakarta.jms.MessageListener
*/
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
System.err.println(e);
}
}
}
/**
* main routine and starting point of program
*
* @param args not used
*/
public static void main(String[] args) {
JMSClient node = null;
try {
node = new JMSClient("vs.queue1", "vs.queue2");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line;
while (true) {
line = input.readLine();
node.producer.send(node.session.createTextMessage(line));
}
} catch (NamingException | JMSException | IOException e) {
System.err.println(e);
} finally {
try {
if ((node != null) && (node.producer != null)) {
node.producer.close();
}
if ((node != null) && (node.consumer != null)) {
node.consumer.close();
}
if ((node != null) && (node.session != null)) {
node.session.close();
}
if ((node != null) && (node.connection != null)) {
node.connection.close();
}
} catch (JMSException e) {
System.err.println(e);
}
}
}
}

View File

@ -0,0 +1,6 @@
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616
queue.vs.queue1 = queue4731
queue.vs.queue2 = queue4732

View File

@ -0,0 +1,55 @@
{
"folders": [
{
"name": "0. Client Service",
"path": "jms.client",
},
{
"name": "0. Client Service (ML)",
"path": "jms.client_solution",
},
{
"name": "1. Echo Service",
"path": "jms.echo",
},
{
"name": "1. Echo Service (ML)",
"path": "jms.echo_solution",
},
{
"name": "2. Logger Service",
"path": "jms.logger",
},
{
"name": "2. Logger Service (ML)",
"path": "jms.logger_solution",
},
{
"name": "3. Textumdreher Service (ML)",
"path": "jms.revers_solution",
},
{
"name": "4. Chat Service",
"path": "jms.chat",
},
{
"name": "4. Chat Service (ML)",
"path": "jms.chat_solution",
},
{
"name": "5. Worker/Tasker für SHA-256 Hashes (ML)",
"path": "jms.tasks_solution",
},
],
"extensions": {
"recommendations": [
"vscjava.vscode-java-pack",
"ms-azuretools.vscode-docker",
"skellock.just",
],
},
"settings": {
"java.dependency.syncWithFolderExplorer": true,
"java.project.explorer.showNonJavaResources": false,
},
}

View File

@ -0,0 +1,6 @@
import '../justfile'
requester text:
just exec vs.EchoRequesterNode "{{text}}"
replier:
just exec vs.EchoReplierNode ""

View File

@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>jms.echo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>jms</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,5 @@
package vs;
public class Conf {
public static final String QUEUE = "vs.echo";
}

View File

@ -0,0 +1,74 @@
package vs;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageListener;
import jakarta.jms.MessageProducer;
import jakarta.jms.Queue;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EchoReplierNode implements MessageListener {
private Connection connection;
private Session session;
private MessageConsumer consumer;
public EchoReplierNode() throws NamingException, JMSException {
Context ctx = new InitialContext();
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
Queue queue = (Queue) ctx.lookup(Conf.QUEUE);
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(queue);
consumer.setMessageListener(this);
connection.start();
}
@Override
public void onMessage(Message request) {
try {
if (request instanceof TextMessage) {
TextMessage requestText = (TextMessage) request;
System.out.println("empfangen: " + requestText.getText());
MessageProducer replyProducer = session.createProducer(request.getJMSReplyTo());
TextMessage reply = session.createTextMessage();
reply.setText("echo: " + requestText.getText());
Thread.sleep(5000);
replyProducer.send(reply);
replyProducer.close();
}
} catch (JMSException | InterruptedException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
EchoReplierNode node = null;
try {
node = new EchoReplierNode();
while (true) {
Thread.sleep(1000);
}
} catch (InterruptedException | NamingException | JMSException e) {
System.err.println(e);
} finally {
try {
if (node != null && node.consumer != null)
node.consumer.close();
if (node != null && node.session != null)
node.session.close();
if (node != null && node.connection != null)
node.connection.close();
} catch (JMSException e) {
System.err.println(e);
}
}
}
}

View File

@ -0,0 +1,85 @@
package vs;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageProducer;
import jakarta.jms.Queue;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EchoRequesterNode {
private Connection connection;
private Session session;
private MessageProducer producer;
private MessageConsumer consumer;
private Queue replyQueue;
public EchoRequesterNode() throws NamingException, JMSException {
Context ctx = new InitialContext();
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ctx.lookup(Conf.QUEUE);
producer = session.createProducer(queue);
replyQueue = session.createTemporaryQueue();
consumer = session.createConsumer(replyQueue);
connection.start();
}
public void receiveAndPrintMessages() throws JMSException {
Message request;
while ((request = consumer.receive()) != null) {
try {
if (request instanceof TextMessage) {
TextMessage requestText = (TextMessage) request;
String messageText = requestText.getText();
System.out.println("empfangen: " + messageText);
}
} catch (JMSException e) {
System.err.println(e);
}
}
}
public void sendMessage(String text) throws JMSException {
TextMessage message = session.createTextMessage();
message.setText(text);
message.setJMSReplyTo(replyQueue);
producer.send(message);
}
public static void main(String[] args) {
String text = args[0];
EchoRequesterNode node = null;
try {
node = new EchoRequesterNode();
node.sendMessage(text);
node.receiveAndPrintMessages();
} catch (NamingException | JMSException e) {
System.err.println(e);
} finally {
try {
if (node != null && node.producer != null) {
node.producer.close();
}
if (node != null && node.consumer != null) {
node.consumer.close();
}
if (node != null && node.session != null) {
node.session.close();
}
if (node != null && node.connection != null) {
node.connection.close();
}
} catch (JMSException e) {
System.err.println(e);
}
}
}
}

View File

@ -0,0 +1,6 @@
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616
queue.vs.echo = queue4734

View File

@ -0,0 +1,6 @@
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616
queue.vs.echo = queue4734

Binary file not shown.

View File

@ -0,0 +1,6 @@
import '../justfile'
requester text:
just exec vs.EchoRequesterNode "{{text}}"
replier:
just exec vs.EchoReplierNode ""

View File

@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vs</groupId>
<artifactId>jms.echo_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>vs</groupId>
<artifactId>jms</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,16 @@
package vs;
/**
* global configuration for echo service
*
* @author Sandro Leuchter
*
*/
public class Conf {
/**
* String constant for requests to the destination of echo service
*
* @see javax.jms.Destination
*/
public static final String QUEUE = "vs.echo";
}

Some files were not shown because too many files have changed in this diff Show More