当前位置: 技术问答>java相关
哪位大虾知道在Applet中如何使用InetAddress类来查询IP地址,谢谢。
来源: 互联网 发布时间:2015-03-25
本文导语: import java.awt.*; import java.applet.*; import java.net.*; public class MyApp extends Applet { InetAddress ipa; public void init() { try { ipa=InetAddress.getByName("xgnsv"); } catch(Unknow...
import java.awt.*;
import java.applet.*;
import java.net.*;
public class MyApp extends Applet
{
InetAddress ipa;
public void init()
{
try
{
ipa=InetAddress.getByName("xgnsv");
}
catch(UnknownHostException e)
{
System.out.println(e.toString());
}
}
public void paint(Graphics g)
{
g.drawString(ipa.getHostName(), 20, 20);
g.drawString(ipa.toString(), 30, 30);
}
}
总是出错
java.security.AccessControlException: access denied (java.net.SocketPermission xgnsv resolve)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:272)
at java.security.AccessController.checkPermission(AccessController.java:399)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:545)
at java.lang.SecurityManager.checkConnect(SecurityManager.java:1042)
at java.net.InetAddress.getAllByName0(InetAddress.java:559)
at java.net.InetAddress.getAllByName0(InetAddress.java:540)
at java.net.InetAddress.getByName(InetAddress.java:449)
at MyApp.init(MyApp.java:12)
at sun.applet.AppletPanel.run(AppletPanel.java:344)
at java.lang.Thread.run(Thread.java:484)
Exit code: 0
No Errors
import java.applet.*;
import java.net.*;
public class MyApp extends Applet
{
InetAddress ipa;
public void init()
{
try
{
ipa=InetAddress.getByName("xgnsv");
}
catch(UnknownHostException e)
{
System.out.println(e.toString());
}
}
public void paint(Graphics g)
{
g.drawString(ipa.getHostName(), 20, 20);
g.drawString(ipa.toString(), 30, 30);
}
}
总是出错
java.security.AccessControlException: access denied (java.net.SocketPermission xgnsv resolve)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:272)
at java.security.AccessController.checkPermission(AccessController.java:399)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:545)
at java.lang.SecurityManager.checkConnect(SecurityManager.java:1042)
at java.net.InetAddress.getAllByName0(InetAddress.java:559)
at java.net.InetAddress.getAllByName0(InetAddress.java:540)
at java.net.InetAddress.getByName(InetAddress.java:449)
at MyApp.init(MyApp.java:12)
at sun.applet.AppletPanel.run(AppletPanel.java:344)
at java.lang.Thread.run(Thread.java:484)
Exit code: 0
No Errors
|
APPLET中不能用!
具体看下面!
import java.net.*;
public class InetAddressTest
{ public static void main(String[] args)
{ try
{
if (args.length > 0)
{ String host = args[0];
InetAddress[] addresses
= InetAddress.getAllByName(host);
for (int i = 0; i
具体看下面!
import java.net.*;
public class InetAddressTest
{ public static void main(String[] args)
{ try
{
if (args.length > 0)
{ String host = args[0];
InetAddress[] addresses
= InetAddress.getAllByName(host);
for (int i = 0; i