List of usage examples for java.net DatagramPacket DatagramPacket
public DatagramPacket(byte buf[], int offset, int length, InetAddress address, int port)
From source file:Main.java
public static void main(String args[]) { try {/*from w ww.j a v a 2s . c o m*/ int port = 80; DatagramSocket ds = new DatagramSocket(port); while (true) { byte buffer[] = new byte[BUFSIZE]; DatagramPacket dp = new DatagramPacket(buffer, 0, buffer.length, InetAddress.getByName("google.com"), 8080); ds.receive(dp); String str = new String(dp.getData()); System.out.println(str); } } catch (Exception e) { e.printStackTrace(); } }
From source file:ca.viaware.dlna.ssdp.SSDPService.java
public void run() { try {// w ww. j av a 2 s . co m this.listener = new MulticastListener(this); this.listener.start(); Searcher searcher = new Searcher(this.outSock); searcher.start(); } catch (IOException e) { e.printStackTrace(); } Thread advertiser = new Thread(new Runnable() { public void send(String data) { try { byte[] bytes = data.getBytes("UTF-8"); DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, InetAddress.getByName("239.255.255.250"), 1900); outSock.send(packet); } catch (IOException e) { e.printStackTrace(); } } public void sendNotify(String usn, String nt, String uid) { String http = "NOTIFY * HTTP/1.1\n"; http += baseAnnounce; http += "NT: " + nt + "\r\n"; http += "USN: " + usn + "\r\n"; http += "LOCATION: http://" + httpConfig.getString("host") + ":" + httpConfig.getInt("port") + "/" + uid + "/\r\n"; http += "\r\n"; send(http); } @Override public void run() { while (true) { Log.info("SSDP: Advertising..."); for (int i = 0; i < 3; i++) { for (Device d : DeviceManager.getDevices()) { sendNotify("uuid: " + d.getUid() + "::upnp:rootdevice", "upnp:rootdevice", d.getUid()); sendNotify("uuid:" + d.getUid(), "uuid:" + d.getUid(), d.getUid()); sendNotify( "uuid:" + d.getUid() + "::urn:schemas-upnp-org:device:" + d.getType() + ":" + d.getVersion(), "urn:schemas-upnp-org:device:" + d.getType() + ":" + d.getVersion(), d.getUid()); for (Service s : d.getServices()) { sendNotify( "uuid:" + d.getUid() + "::urn:schemas-upnp-org:service:" + s.getType() + ":" + s.getVersion(), "urn:schemas-upnp-org:service:" + s.getType() + ":" + s.getVersion(), d.getUid()); } } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(30 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); advertiser.start(); Log.info("Started SSDP Service"); }