afei,if he has installed JRE, the jvm should be changed. 这个wyt应该知道吧,如果JAVA权限没打开,applet只能和包含它的机器进行SOCKET连接。确切的说就是WEB服务器。除非你的WEB服务器,应用程序的服务器与客户端都在一台机器上。而你的情况不是很清楚,再描述清楚一些!你确定你打开了权限吗?
ahfer is good,if you use server. ahfer thinking: socket = new Socket(getCodeBase().getHost(),4000); if you change java.policy,the practical value of your code is zero.
请看JDK Socket的文档 public Socket(InetAddress address,int port) throws IOExceptionCreates a stream socket and connects it to the specified port number at the specified IP address. If the application has specified a socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created. If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException./*注意这里*/Parameters: address - the IP address. port - the port number.Throws: IOException - if an I/O error occurs when creating the socket. SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.See Also: setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int)
请问谁能对这一段加以解释, and how can i get a factory ? If the application has specified a socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.
还有上点建议,想真真正正的学java就不要再用M$的VJ++了,而如果你真的很喜欢M$,就等着学他的C#吧
我用本机的IP地址,不行呀
to bgold:
我的Server端程序和Client程序都是在本机上运行的
Web Server的IP是什么呢?
既不清了,看看书先)
这个问题是因为Java的严格安全性造成的,java有一sandbox模型和一下列安全策略文件进行安全访问控制。
解决很简单,改一下安全策略。如下:
在$JDK\jre\lib\security\java.policy文件加上一行
permission java.net.SocketPermission "YOUIP:4000", "connect,accept,resolve";
(要跑applet的机器都要加)
everything is OK!
这个wyt应该知道吧,如果JAVA权限没打开,applet只能和包含它的机器进行SOCKET连接。确切的说就是WEB服务器。除非你的WEB服务器,应用程序的服务器与客户端都在一台机器上。而你的情况不是很清楚,再描述清楚一些!你确定你打开了权限吗?
ahfer thinking:
socket = new Socket(getCodeBase().getHost(),4000);
if you change java.policy,the practical value of your code is zero.
public Socket(InetAddress address,int port) throws IOExceptionCreates a stream socket and connects it to the specified port number at the specified IP address. If the application has specified a socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created. If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException./*注意这里*/Parameters:
address - the IP address.
port - the port number.Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int)
我觉得如果上述没设置正确的属性的话,小程序根本就没有权限对客房端的网络进行访问,浏览器的虚拟机就抛出SecurityException异常还请指正:
http://expert.csdn.net/Topic/43409.shtm /borz
SocketImplFactory实际上就一个方法,返回一个SocketImpl实例让我们看看Socket的实现代码,其中有一个SocketImpl类型的成员变量impl,
在他一个底层的构造函数中(protected)中会判断该值,如果impl为空则他会创建一个类型为
PlainSocketImpl(对包外部是不可见的)的实例赋给impl,很显然PlainSocketImpl实现了SocketImpl接口,因此只要你实现了一个SocketImpl接口,就很容易用SocketImplFactory
的拉口函数返回,这样就可以自己创建自己有个性的Socket,当然要实现SocketImpl少不了
用到本地方法调用能给加分吗? :)