启动我的 RMI 服务器(在 ISP 下)以便它可以通过 Internet 接收远程调用时出现问题
我正在创建一个客户端/服务器应用程序,其中我的服务器和客户端可以位于同一台或不同的计算机上,但两者都在 ISP 下。
我的 RMI 程序:- -
远程接口:-
//Calculator.java
public interface Calculator
extends java.rmi.Remote {
public long add(long a, long b)
throws java.rmi.RemoteException;
public long sub(long a, long b)
throws java.rmi.RemoteException;
public long mul(long a, long b)
throws java.rmi.RemoteException;
public long div(long a, long b)
throws java.rmi.RemoteException;
}
远程接口实现:-
//CalculatorImpl.java
public class CalculatorImpl
extends
java.rmi.server.UnicastRemoteObject
implements Calculator {
public CalculatorImpl()
throws java.rmi.RemoteException {
super();
}
public long add(long a, long b)
throws java.rmi.RemoteException {
return a + b;
}
public long sub(long a, long b)
throws java.rmi.RemoteException {
return a - b;
}
public long mul(long a, long b)
throws java.rmi.RemoteException {
return a * b;
}
public long div(long a, long b)
throws java.rmi.RemoteException {
return a / b;
}
}
服务器:-
//CalculatorServer.java
import java.rmi.Naming;
import java.rmi.server.RemoteServer;
public class CalculatorServer {
public CalculatorServer(String IP)
{
try {
Calculator c = new CalculatorImpl();
Naming.rebind("rmi://"+IP+":1099/CalculatorService", c);
} catch (Exception e)
{
System.out.println("Trouble: " + e);
}
}
public static void main(String args[]) {
new CalculatorServer(args[0]);
}
}
客户端:-
//CalculatorClient.java
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient {
public static void main(String[] args) {
try {
Calculator c = (Calculator)Naming.lookup("rmi://"+args[0]+"/CalculatorService");
System.out.println( c.sub(4, 3) );
System.out.println( c.add(4, 5) );
System.out.println( c.mul(3, 6) );
System.out.println( c.div(9, 3) );
}
catch (MalformedURLException murle) {
System.out.println();
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println("RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println("NotBoundException");
System.out.println(nbe);
}
catch (java.lang.ArithmeticException ae) {
System.out.println();
System.out.println("java.lang.ArithmeticException");
System.out.println(ae);
}
}
}
当服务器和客户端程序都在同一台计算机上时:-
我通过传递路由器静态 IP 地址来启动服务器程序:- 192.168.1.35 在 args[0] 中,我的服务器启动......很好。
通过在我的客户端的 args[0] 中传递相同的静态 IP 地址也可以正常工作。
但是:-
当服务器和客户端程序位于不同的计算机上时:-
现在,我尝试通过在 args[0] 中传递它的公共 IP 地址:59.178.198.247 来启动我的服务器程序,以便它可以通过互联网接收呼叫。
但我无法启动它。
并且发生以下异常:-
Trouble: java.rmi.ConnectException: Connection refused to host: 59.178.198.247;
nested exception is:
java.net.ConnectException: Connection refused: connect
我认为这是由于 NAT 问题,因为我在 ISP 下。
所以,我的问题是,如何在 ISP 下启动 RMI 服务器,以便它可以接收来自互联网的远程调用???
i m creating a Client/Server application in which my server and client can be on the same or on different machines but both are under ISP.
My RMI programs:-
-Remote Intreface:-
//Calculator.java
public interface Calculator
extends java.rmi.Remote {
public long add(long a, long b)
throws java.rmi.RemoteException;
public long sub(long a, long b)
throws java.rmi.RemoteException;
public long mul(long a, long b)
throws java.rmi.RemoteException;
public long div(long a, long b)
throws java.rmi.RemoteException;
}
Remote Interface Implementation:-
//CalculatorImpl.java
public class CalculatorImpl
extends
java.rmi.server.UnicastRemoteObject
implements Calculator {
public CalculatorImpl()
throws java.rmi.RemoteException {
super();
}
public long add(long a, long b)
throws java.rmi.RemoteException {
return a + b;
}
public long sub(long a, long b)
throws java.rmi.RemoteException {
return a - b;
}
public long mul(long a, long b)
throws java.rmi.RemoteException {
return a * b;
}
public long div(long a, long b)
throws java.rmi.RemoteException {
return a / b;
}
}
Server:-
//CalculatorServer.java
import java.rmi.Naming;
import java.rmi.server.RemoteServer;
public class CalculatorServer {
public CalculatorServer(String IP)
{
try {
Calculator c = new CalculatorImpl();
Naming.rebind("rmi://"+IP+":1099/CalculatorService", c);
} catch (Exception e)
{
System.out.println("Trouble: " + e);
}
}
public static void main(String args[]) {
new CalculatorServer(args[0]);
}
}
Client:-
//CalculatorClient.java
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient {
public static void main(String[] args) {
try {
Calculator c = (Calculator)Naming.lookup("rmi://"+args[0]+"/CalculatorService");
System.out.println( c.sub(4, 3) );
System.out.println( c.add(4, 5) );
System.out.println( c.mul(3, 6) );
System.out.println( c.div(9, 3) );
}
catch (MalformedURLException murle) {
System.out.println();
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println("RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println("NotBoundException");
System.out.println(nbe);
}
catch (java.lang.ArithmeticException ae) {
System.out.println();
System.out.println("java.lang.ArithmeticException");
System.out.println(ae);
}
}
}
when both Server and client programs are on same machine:-
i start my server program by passing my router static IP address:-192.168.1.35
in args[0] and my server starts...fine.
and by passing the same Static IP address in my Client's args[0] also works fine.
but:-
when both Server and client programs are on different machines:-
now,i m trying to start my Server Program by passing it's public IP address:59.178.198.247 in args[0] so that it can recieve call over internet.
but i am unable to start it.
and the following exception occurs:-
Trouble: java.rmi.ConnectException: Connection refused to host: 59.178.198.247;
nested exception is:
java.net.ConnectException: Connection refused: connect
i think it is due to NAT Problem because i am under ISP.
so,my problem is that how can i start my RMI Server under ISP so that it can recieve remote calls from internet????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大概您的 ISP 阻止了连接。你必须和他们谈谈。
Presumably your ISP is blocking the connection. You'll have to talk to them.
尝试查看您的端口 (1099) 是否可见(例如,您可以使用这个)。如果没有,请使用开放端口,或编辑防火墙或路由器配置(您可能需要设置端口转发)。如果是,请检查您的 RMI 安全策略。
Try to see if your port (1099) is visible (you can use this for example). If not, use an open port, or edit your firewall or router configuration (you might need to set up port forwarding). If it is, look into your RMI security policy.