final final
parent
e7c4f781a2
commit
0828b032ce
|
|
@ -1,48 +0,0 @@
|
|||
package vs;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
|
||||
|
||||
public class DiscoveryServer implements Runnable {
|
||||
private static final int BUFSIZE = 512;
|
||||
|
||||
DatagramPacket packetInDiscover = new DatagramPacket(new byte[BUFSIZE], BUFSIZE);
|
||||
|
||||
private final int port;
|
||||
|
||||
public DiscoveryServer(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Discovery running on " + port);
|
||||
|
||||
try (DatagramSocket discoverySocket = new DatagramSocket(port)) {
|
||||
// discoverySocket.setReuseAddress(true);
|
||||
discoverySocket.setBroadcast(true);
|
||||
|
||||
while (true) {
|
||||
byte[] buffer = new byte[256];
|
||||
DatagramPacket packetIn = new DatagramPacket(buffer, buffer.length);
|
||||
discoverySocket.receive(packetIn);
|
||||
|
||||
InetAddress clientAddress = packetIn.getAddress();
|
||||
int clientPort = packetIn.getPort();
|
||||
System.out.println("DISCOVERY from " + clientAddress.getHostAddress() + ":" + clientPort);
|
||||
|
||||
|
||||
DatagramPacket packetOut = new DatagramPacket(new byte[256], 256);
|
||||
packetOut.setSocketAddress(packetIn.getSocketAddress());
|
||||
packetOut.setData(new byte[0]);
|
||||
packetOut.setLength(0);
|
||||
|
||||
discoverySocket.send(packetOut);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
package vs;
|
||||
|
||||
public class Main {
|
||||
private static final int SYSLOG_PORT = 514;
|
||||
private static final int DISCOVERY_PORT = 8888;
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Thread(new DiscoveryServer(DISCOVERY_PORT), "discovery").start();
|
||||
new Thread(new SyslogUdpServer(SYSLOG_PORT), "syslog").start();
|
||||
}
|
||||
}
|
||||
|
|
@ -54,7 +54,7 @@ public class Server {
|
|||
}
|
||||
|
||||
String receivedSyslog = new String(packetIn.getData(), 0, packetIn.getLength());
|
||||
System.out.println(receivedSyslog);
|
||||
System.out.println(packetIn.getAddress() + ": " + receivedSyslog);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
package vs;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
|
||||
|
||||
public class SyslogUdpServer implements Runnable {
|
||||
private final int port;
|
||||
private static final int MAX_MESSAGE_LENGTH = 1024;
|
||||
|
||||
public SyslogUdpServer(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Syslog running on " + port + "\n");
|
||||
try (DatagramSocket syslogSocket = new DatagramSocket(port)) {
|
||||
syslogSocket.setReuseAddress(true);
|
||||
byte[] buffer = new byte[MAX_MESSAGE_LENGTH + 1];
|
||||
while (true) {
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
syslogSocket.receive(packet);
|
||||
|
||||
int len = packet.getLength();
|
||||
InetAddress clientAddress = packet.getAddress();
|
||||
|
||||
|
||||
if (len > MAX_MESSAGE_LENGTH) {
|
||||
System.err.println("Dropped oversized UDP syslog (" + len + " > " + MAX_MESSAGE_LENGTH
|
||||
+ ") from " + clientAddress.getHostAddress() + ":" + packet.getPort());
|
||||
continue;
|
||||
}
|
||||
|
||||
// Nachricht decodieren und mit Sender-Info ausgeben
|
||||
String message = new String(packet.getData(), 0, len).trim();
|
||||
System.out.println("SYSLOG from " + clientAddress.getHostAddress()
|
||||
+ ":" + packet.getPort() + " -> " + message);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Einfaches Fehler-Logging auf die Konsole
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
import socket
|
||||
|
||||
# Simple one-shot version
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
sock.settimeout(10)
|
||||
|
||||
try:
|
||||
sock.sendto(b"DISCOVER", ('255.255.255.255', 8888))
|
||||
print("Broadcast sent, waiting for response...")
|
||||
|
||||
data, addr = sock.recvfrom(1024)
|
||||
print(f"Response from {addr}: {data.decode()}")
|
||||
|
||||
except socket.timeout:
|
||||
print("No response received")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
finally:
|
||||
sock.close()
|
||||
Loading…
Reference in New Issue