Initial commit

Solved echo praktikum
main
Victor Hans-Georg Waitz 2025-10-12 15:03:12 +02:00
commit 65b08e1863
11 changed files with 150 additions and 0 deletions

3
.gitignore vendored 100644
View File

@ -0,0 +1,3 @@
/VAR-UDP/.idea
/VAR-UDP/bin
/VAR-UDP/.settings

Binary file not shown.

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
<fileset name="all" enabled="true" check-config-name="Google Checks" local="false">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
</fileset-config>

View File

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

17
VAR-UDP/.project 100644
View File

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

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

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

View File

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

View File

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