【发布时间】:2015-06-26 07:34:50
【问题描述】:
我正在尝试使用 Java RMI 构建分布式应用程序,并且我在这些领域做了很多实践和研究。这么多的例子工作得很好。 这里的代码是用于服务器端的,当我通过 cmd 运行应用程序时它没有任何问题,但是它会在 2-4 秒后关闭,然后才能像这样绑定服务,因此客户端给出“NOTBOUND Exception” .疯狂的部分是相同的代码在 anth 应用程序中运行非常顺利。
这是我正在使用的所有代码
RInterface.java
package rmi;
import java.rmi.*;
public interface RInterface extends Remote {
public boolean log(String Uname, String code) throws RemoteException;
}
RClass.java
package rmi;
import java.rmi.*;
import java.rmi.server.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class RClass extends UnicastRemoteObject implements RInterface {
private Connection connect = null;
private Statement statement = null;
private ResultSet resultSet = null;
private boolean stat = false;
public String state = "Waiting Confirmation ...";
public RClass() throws RemoteException {
super();
connect();
}
public boolean log(String Uname, String code) throws RemoteException {
try {
String sql = "select name,code from logusers where name='"+Uname+"' and code ='"+code+"'";
resultSet = statement.executeQuery(sql);
int count=0;
while (resultSet.next()) {
count+=1;
}
if (count==0) {
stat = false;
state = "NO User Found!! Access Denied";
}else
if (count>1) {
stat = false;
state = "duplicate User!! NOT allowed";
}else
if (count == 1){
stat = true;
state = "WelCome "+ Uname +"!! Access Granted";
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,e);
}
return stat;
}
public final void connect(){
try{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/eysa?"+"user=root&password=");
statement = connect.createStatement();
} catch(ClassNotFoundException | SQLException e){
System.exit(0);
}
}
}
myServer.java(我的痛点)
package rmi;
import java.net.MalformedURLException;
import java.rmi.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class myServer {
myServer() {
new Thread(){
public void run(){
try{
RInterface stub = new RClass();
Naming.rebind("rmi://localhost/LOGIN",stub);
} catch(RemoteException e){
JOptionPane.showMessageDialog(null, e);
} catch (MalformedURLException ex) {
Logger.getLogger(myServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}.start();
}
public static void main(String[] args) throws InterruptedException {
myServer myS = new myServer();
JOptionPane.showMessageDialog(null, "Server is Ready...");
}
}
myClient.java 是很简单的代码,我觉得没必要附上。
【问题讨论】: