pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

helma private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Anonymous on Thu 23 Aug 16:58
report abuse | download | new post

  1. // Returns the local machine's IP address as a dotted-quad string, by
  2. // sending myself a UDP packet with a unique string.  This function
  3. // UDP-broadcasts, otherwise the remote address of a received packet
  4. // is 127.0.0.1.
  5. function findMyIP() {
  6.   var sock = new java.net.DatagramSocket();
  7.   var sendSock = new java.net.DatagramSocket();
  8.   var secret = java.util.UUID.randomUUID().toString();
  9.   var place = new java.util.Hashtable(1); // thread-safe
  10.   // Receive UDP packet with secret in looping background thread.
  11.   spawn(function() {
  12.     var sb = new java.lang.StringBuffer();
  13.     sb.setLength(secret.length);
  14.     while(true) {
  15.       var p = new java.net.DatagramPacket(new java.lang.String(sb.toString()).getBytes(), secret.length);
  16.       sock.receive(p);
  17.       if (new java.lang.String(p.getData()) == secret) {
  18.         place.put("ip", p.getAddress().getHostAddress());
  19.         break;
  20.       }
  21.     }
  22.   });
  23.   // Send packets with secret until receiver thread puts IP into table.
  24.   while(true) {
  25.     var p = new java.net.DatagramPacket(new java.lang.String(secret).getBytes(),
  26.                                         secret.length,
  27.                                         java.net.InetAddress.getByName("255.255.255.255"),
  28.                                         sock.getLocalPort());
  29.     sendSock.send(p);
  30.     app.log("Waiting for my IP...");
  31.     java.lang.Thread.sleep(500);
  32.     var myIP = place.get("ip");
  33.     if (myIP != null) {
  34.       return myIP;
  35.     }
  36.   }
  37. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me