Java Reference
In-Depth Information
String
s
=
new
String
(
dp
.
getData
(),
"8859_1"
);
System
.
out
.
println
(
s
);
}
}
catch
(
IOException
ex
)
{
System
.
err
.
println
(
ex
);
}
finally
{
if
(
ms
!=
null
)
{
try
{
ms
.
leaveGroup
(
group
);
ms
.
close
();
}
catch
(
IOException
ex
)
{}
}
}
}
}
The program begins by reading the name and port of the multicast group from the first
two command-line arguments. Next, it creates a new
MulticastSocket ms
on the
specified port. This socket joins the multicast group at the specified
InetAddress
. Then
it enters a loop in which it waits for packets to arrive. As each packet arrives, the program
reads its data, converts the data to an ISO Latin-1
String
, and prints it on
Sys
tem.out
. Finally, when the user interrupts the program or an exception is thrown, the
socket leaves the group and closes itself.
When a Universal Plug and Play (UPnP) device joins a network, it sends an HTTPU
(HTTP over UDP) message to the multicast address 239.255.255.250 on port 1900. You
can use this program to listen to those messages. If such a device is broadcasting, you
should see a message pop through within the first minute or two. In fact, you'll probably
see a lot more. I collected about a megabyte and a half of announcements within the
first couple of minutes I had this program running. I show only the first two here:
$
java MulticastSniffer 239.255.255.250 1900
NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=1800
LOCATION: http://192.168.1.2:23519/Ircc.xml
NT: upnp:rootdevice
NTS: ssdp:alive
SERVER: Android/3.2 UPnP/1.0 Internet TV Box NSZ-GT1/1.0
USN: uuid:34567890-1234-1010-8000-544249cb49fd::upnp:rootdevice
X-AV-Server-Info: av=5.0; hn=""; cn="Sony Corporation";
mn="Internet TV Box NSZ-GT1"; mv="1.0";
NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=1800
LOCATION: http://192.168.1.2:23519/Ircc.xml
NT: uuid:34567890-1234-1010-8000-544249cb49fd
NTS: ssdp:alive
SERVER: Android/3.2 UPnP/1.0 Internet TV Box NSZ-GT1/1.0