messages are now read

Corinna
smittythekid 2026-04-08 18:42:47 +02:00
parent cb15d6ac69
commit 5e81f4f979
1 changed files with 18 additions and 5 deletions

View File

@ -18,17 +18,30 @@ public class SyslogServer {
System.out.println("Syslog Server started on port " + port);
try{
// Create a DatagramSocket to listen for incoming messages
DatagramSocket socket = new DatagramSocket(port);
// Buffer to hold incoming messages
byte[] buffer = new byte[1024];
while(true){
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while(true){
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Wait for a message to be received (blocking call)
socket.receive(packet);
// Extract the message from the packet + data; how many bytes were actually received
int length = packet.getLength();
socket.receive(packet);
String message = new String(
packet.getData(), // complete byte array be aware: packet.getData() returns the entire buffer, not just the received data
packet.getOffset(), // get the offset where the data starts
length,
StandardCharsets.UTF_8);
System.out.println("Nachricht empfangen!" + message);
}
System.out.println("Nachricht empfangen!");
}
}
catch(IOException e){