commit 65b08e186377d533d70ceacaa7ab01a82838f358 Author: 3013050 <3013050@stud.hs-mannheim.de> Date: Sun Oct 12 15:03:12 2025 +0200 Initial commit Solved echo praktikum diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6633fd8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/VAR-UDP/.idea +/VAR-UDP/bin +/VAR-UDP/.settings \ No newline at end of file diff --git a/VAR-UDP-solution.zip b/VAR-UDP-solution.zip new file mode 100644 index 0000000..5530831 Binary files /dev/null and b/VAR-UDP-solution.zip differ diff --git a/VAR-UDP/.checkstyle b/VAR-UDP/.checkstyle new file mode 100644 index 0000000..5783bc0 --- /dev/null +++ b/VAR-UDP/.checkstyle @@ -0,0 +1,7 @@ + + + + + + + diff --git a/VAR-UDP/.classpath b/VAR-UDP/.classpath new file mode 100644 index 0000000..fb565a5 --- /dev/null +++ b/VAR-UDP/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/VAR-UDP/.project b/VAR-UDP/.project new file mode 100644 index 0000000..a2c236f --- /dev/null +++ b/VAR-UDP/.project @@ -0,0 +1,17 @@ + + + VAR-UDP + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/VAR-UDP/src/var/sockets/udp/echo/EchoClient.java b/VAR-UDP/src/var/sockets/udp/echo/EchoClient.java new file mode 100644 index 0000000..8156a32 --- /dev/null +++ b/VAR-UDP/src/var/sockets/udp/echo/EchoClient.java @@ -0,0 +1,35 @@ +package var.sockets.udp.echo; + +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.SocketTimeoutException; + +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) { + System.out.printf("Sending: %s - len: %d\n", args[0], args[0].length()); + 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); + } + } +} \ No newline at end of file diff --git a/VAR-UDP/src/var/sockets/udp/echo/EchoServer.java b/VAR-UDP/src/var/sockets/udp/echo/EchoServer.java new file mode 100644 index 0000000..05e7fc1 --- /dev/null +++ b/VAR-UDP/src/var/sockets/udp/echo/EchoServer.java @@ -0,0 +1,36 @@ +package var.sockets.udp.echo; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.util.Arrays; + +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) { + System.out.println("\nListening: "); + socket.receive(packetIn); + System.out.println( + "Received: " + packetIn.getLength() + " bytes: " + new String(packetIn.getData())); + + packetOut.setSocketAddress(packetIn.getSocketAddress()); + System.out.printf("About to send: %d bytes: %s\n", packetIn.getData().length, 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); + } + } +} diff --git a/VAR-UDP/src/var/sockets/udp/messwerte/MesswertClient.java b/VAR-UDP/src/var/sockets/udp/messwerte/MesswertClient.java new file mode 100644 index 0000000..7c06fc7 --- /dev/null +++ b/VAR-UDP/src/var/sockets/udp/messwerte/MesswertClient.java @@ -0,0 +1,22 @@ +package var.sockets.udp.messwerte; + +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); + } + } +} diff --git a/VAR-UDP/src/var/sockets/udp/messwerte/MesswertServer.java b/VAR-UDP/src/var/sockets/udp/messwerte/MesswertServer.java new file mode 100644 index 0000000..a51fa80 --- /dev/null +++ b/VAR-UDP/src/var/sockets/udp/messwerte/MesswertServer.java @@ -0,0 +1,8 @@ +package var.sockets.udp.messwerte; + +public class MesswertServer { + + public static void main(String[] args) { + // TODO Auto-generated method stub + } +} diff --git a/VAR-UDP/src/var/sockets/udp/time/TimeClient.java b/VAR-UDP/src/var/sockets/udp/time/TimeClient.java new file mode 100644 index 0000000..ab80f45 --- /dev/null +++ b/VAR-UDP/src/var/sockets/udp/time/TimeClient.java @@ -0,0 +1,8 @@ +package var.sockets.udp.time; + +public class TimeClient { + + public static void main(String[] args) { + // TODO Auto-generated method stub + } +} diff --git a/VAR-UDP/src/var/sockets/udp/time/TimeServer.java b/VAR-UDP/src/var/sockets/udp/time/TimeServer.java new file mode 100644 index 0000000..4616ff2 --- /dev/null +++ b/VAR-UDP/src/var/sockets/udp/time/TimeServer.java @@ -0,0 +1,8 @@ +package var.sockets.udp.time; + +public class TimeServer { + + public static void main(String[] args) { + // TODO Auto-generated method stub + } +}