modify justfile to work without vs -> vs.classname; modiify SyslogClient & Server (improved comments and made the port variable a constant variable)
parent
b1c4fc0e8a
commit
824c41012e
2
justfile
2
justfile
|
|
@ -5,7 +5,7 @@ set shell := ["powershell.exe", "-c"]
|
||||||
default:
|
default:
|
||||||
mvn clean compile test
|
mvn clean compile test
|
||||||
exec class +args="": compile
|
exec class +args="": compile
|
||||||
mvn exec:java "-Dexec.mainClass={{class}}" "-Dexec.args={{args}}"
|
mvn exec:java "-Dexec.mainClass=vs.{{class}}" "-Dexec.args={{args}}"
|
||||||
clean:
|
clean:
|
||||||
mvn clean
|
mvn clean
|
||||||
compile:
|
compile:
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,12 @@ import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class SyslogClient {
|
public class SyslogClient {
|
||||||
|
|
||||||
|
private static final int PORT = 514;
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
DatagramSocket socket = new DatagramSocket();
|
DatagramSocket socket = new DatagramSocket();
|
||||||
socket.setBroadcast(true);
|
socket.setBroadcast(true);
|
||||||
|
|
||||||
// 🔍 Discovery (Unverändert)
|
// Discovery
|
||||||
byte[] discoverMsg = "DISCOVER".getBytes();
|
byte[] discoverMsg = "DISCOVER".getBytes();
|
||||||
DatagramPacket packet = new DatagramPacket(
|
DatagramPacket packet = new DatagramPacket(
|
||||||
discoverMsg, discoverMsg.length,
|
discoverMsg, discoverMsg.length,
|
||||||
|
|
@ -19,13 +20,16 @@ public class SyslogClient {
|
||||||
);
|
);
|
||||||
socket.send(packet);
|
socket.send(packet);
|
||||||
|
|
||||||
// 📥 Antwort empfangen (Unverändert)
|
// Antwort empfangen
|
||||||
byte[] buffer = new byte[256];
|
byte[] buffer = new byte[256];
|
||||||
DatagramPacket response = new DatagramPacket(buffer, buffer.length);
|
DatagramPacket response = new DatagramPacket(buffer, buffer.length);
|
||||||
socket.receive(response);
|
socket.receive(response);
|
||||||
System.out.println("Server gefunden: " + response.getAddress());
|
System.out.println("Server gefunden: " + response.getAddress());
|
||||||
|
|
||||||
// 📨 ECHTE RFC 5424 Syslog-Nachricht aus DEINEN Klassen bauen!
|
// ECHTE RFC 5424 Syslog-Nachricht aus AsciiChars & SyslogMessage
|
||||||
|
// args als massage, ansonsten default message
|
||||||
|
String text = (args.length > 0) ? String.join(" ", args): "Dies ist eine standardkonforme Testnachricht";
|
||||||
|
|
||||||
SyslogMessage msg = new SyslogMessage(
|
SyslogMessage msg = new SyslogMessage(
|
||||||
SyslogMessage.Facility.USER,
|
SyslogMessage.Facility.USER,
|
||||||
SyslogMessage.Severity.INFORMATIONAL,
|
SyslogMessage.Severity.INFORMATIONAL,
|
||||||
|
|
@ -34,7 +38,7 @@ public class SyslogClient {
|
||||||
new AsciiChars.L128("PID1234"),
|
new AsciiChars.L128("PID1234"),
|
||||||
new AsciiChars.L032("MSG-01"),
|
new AsciiChars.L032("MSG-01"),
|
||||||
new StructuredData().add(StructuredData.Element.newTimeQuality(true, true)),
|
new StructuredData().add(StructuredData.Element.newTimeQuality(true, true)),
|
||||||
new SyslogMessage.TextMessage("Das ist eine standardkonforme Nachricht!")
|
new SyslogMessage.TextMessage(text)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Die Klasse wandelt alles in den perfekten String um (inklusive Zeitstempel)
|
// Die Klasse wandelt alles in den perfekten String um (inklusive Zeitstempel)
|
||||||
|
|
@ -45,7 +49,7 @@ public class SyslogClient {
|
||||||
byte[] data = logString.getBytes(StandardCharsets.UTF_8);
|
byte[] data = logString.getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
DatagramPacket syslogPacket = new DatagramPacket(
|
DatagramPacket syslogPacket = new DatagramPacket(
|
||||||
data, data.length, response.getAddress(), 514
|
data, data.length, response.getAddress(), PORT
|
||||||
);
|
);
|
||||||
|
|
||||||
socket.send(syslogPacket);
|
socket.send(syslogPacket);
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,13 @@ public class SyslogServer {
|
||||||
|
|
||||||
String message = new String(packet.getData(), 0, length, StandardCharsets.UTF_8);
|
String message = new String(packet.getData(), 0, length, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
// 📡 Ausgabe mit Client-IP
|
// BOM-zeichen ersetzten
|
||||||
|
message = message.replace("", "Nachricht: ");
|
||||||
|
|
||||||
|
// Zur Sicherheit AUCH noch das Unicode-Zeichen des BOM-Zeichens
|
||||||
|
message = message.replace("\uFEFF", "Nachricht: ");
|
||||||
|
|
||||||
|
// Ausgabe mit Client-IP
|
||||||
System.out.println(
|
System.out.println(
|
||||||
"[SYSLOG] Von " +
|
"[SYSLOG] Von " +
|
||||||
packet.getAddress().getHostAddress() +
|
packet.getAddress().getHostAddress() +
|
||||||
|
|
@ -78,7 +84,7 @@ public class SyslogServer {
|
||||||
clientAddress.getHostAddress()
|
clientAddress.getHostAddress()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Antwort (leer reicht laut Aufgabe)
|
// Antwort leer(laut Aufgabe)
|
||||||
byte[] responseData = new byte[0];
|
byte[] responseData = new byte[0];
|
||||||
|
|
||||||
DatagramPacket response = new DatagramPacket(
|
DatagramPacket response = new DatagramPacket(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue