Skip to content

Instantly share code, notes, and snippets.

@nabe-abk
Last active May 4, 2020 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nabe-abk/58c7d641bfda1ff0775d to your computer and use it in GitHub Desktop.
Save nabe-abk/58c7d641bfda1ff0775d to your computer and use it in GitHub Desktop.
SSDP discover sample program for perl/socket
#!/usr/bin/perl
################################################################################
# SSDP discover Written by nabe@abk / This is PDS
################################################################################
use strict;
use Socket;
my $CAST = '239.255.255.250';
my $PORT = 1900;
################################################################################
my $msg =<<SSDP;
M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1900
MAN: "ssdp:discover"
MX: 3
ST: ssdp:all
SSDP
# ST: ssdp:all
# ST: urn:schemas-upnp-org:device:MediaServer:1
# ST: urn:schemas-upnp-org:device:MediaRenderer:1
$msg =~ s/\r?\n/\r\n/g;
################################################################################
my $sock;
my $addr = pack_sockaddr_in($PORT, inet_aton($CAST));
socket($sock, AF_INET, SOCK_DGRAM, 0);
setsockopt($sock, SOL_SOCKET, SO_BROADCAST, 1);
bind($sock, pack_sockaddr_in(0, INADDR_ANY));
send($sock, $msg, 0, $addr);
while (1){
my $buf;
recv($sock, $buf, 65536, 0);
print "$buf\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment