Last active
February 18, 2023 02:22
-
-
Save jphalip/3fb2bcff6292e0dd23b049524c519d63 to your computer and use it in GitHub Desktop.
SSDP sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) throws IOException { | |
System.setProperty("java.net.preferIPv4Stack", "true"); | |
int PORT = 1900; | |
String MULTICAST_GROUP_IPV4 = "239.172.243.75"; | |
InetAddress LOCALHOST = Inet4Address.getByName("127.0.0.1"); | |
byte[] requestBytes = SEARCH_MESSAGE; | |
MulticastSocket socket = new MulticastSocket(new InetSocketAddress(LOCALHOST, 0)); | |
socket.setLoopbackMode(false); | |
socket.setInterface(LOCALHOST); | |
DatagramPacket packet = new DatagramPacket( | |
requestBytes, requestBytes.length, | |
new InetSocketAddress( | |
InetAddress.getByName(MULTICAST_GROUP_IPV4), PORT)); | |
try { | |
socket.send(packet); | |
} | |
catch (Exception e) { | |
System.err.println(e.getMessage()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
from server import PORT, MULTICAST_GROUP_IPV4, LOCALHOST | |
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
client.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(LOCALHOST)) | |
search_message = ( | |
"M-SEARCH * HTTP/1.1\r\n" + | |
"MAN: \"ssdp:discover\"\r\n" + | |
"MX: 1\r\n" + | |
"ST: fusion_idea:debug\r\n" + | |
"HOST: 127.0.0.1:" + str(PORT) + "\r\n\r\n" | |
) | |
client.sendto(search_message.encode(), (MULTICAST_GROUP_IPV4, PORT)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import struct | |
PORT = 1900 | |
MULTICAST_GROUP_IPV4 = "239.172.243.75" | |
LOCALHOST = "127.0.0.1" | |
if __name__ == "__main__": | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
sock.bind(("", PORT)) | |
# req = struct.pack("=4s4s", socket.inet_aton(MULTICAST_GROUP_IPV4), socket.inet_aton("0.0.0.0")) | |
req = struct.pack("=4s4s", socket.inet_aton(MULTICAST_GROUP_IPV4), socket.inet_aton(LOCALHOST)) | |
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, req) | |
print("Bound to IP address: %s, Port: %d" % sock.getsockname()) | |
while True: | |
data, addr = sock.recvfrom(1024) | |
print("Received data from %s: %s" % (addr, data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment