IPdetect.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /////////////////////////////////////////////////////
  2. // IPdetect.java
  3. //
  4. // This applet connects back to the server from
  5. // which it came and retrieves the IP address at
  6. // its end of the connection. This address and any
  7. // error message is then available to JavaScript
  8. // code using the getLocal and getMsg class methods,
  9. // respectively.
  10. //
  11. // If there is any error, the address is set to
  12. // "javaerror".
  13. //
  14. // See COPYING for licensing information.
  15. //
  16. /////////////////////////////////////////////////////
  17. import java.applet.*;
  18. import java.net.*;
  19. public class IPdetect extends Applet {
  20. // IP address
  21. private String localaddress;
  22. public String getLocal() {
  23. return localaddress;
  24. }
  25. // error message
  26. private String errormsg;
  27. public String getMsg() {
  28. return errormsg;
  29. }
  30. // constructor
  31. public IPdetect() {
  32. }
  33. // initialization
  34. public void init() {
  35. try {
  36. // get host and port to connect
  37. URL url = getCodeBase();
  38. String host = url.getHost();
  39. int port = url.getPort();
  40. if (port == -1) port = 80;
  41. // do the connect
  42. Socket sck = new Socket(host, port);
  43. // get address at our end
  44. localaddress = sck.getLocalAddress().getHostAddress();
  45. // close the connection
  46. sck.close();
  47. // no error message
  48. errormsg = "";
  49. }
  50. catch(Exception e) {
  51. // indicate an error occured
  52. localaddress = "javaerror";
  53. // provide error message
  54. errormsg = e.getMessage();
  55. }
  56. }
  57. }