File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Distributed Java and the fly likes RMI application connection issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "RMI application connection issue" Watch "RMI application connection issue" New topic
Author

RMI application connection issue

Olakunle Oladipo Oni
Greenhorn

Joined: Aug 28, 2008
Posts: 18
I cannot resolve ConnectException i'm having with an RMI application I'm working on. The client application kept giving a phrase like "connection refused to host:127.0.0.1....ConnectException..after taking the appropriate steps such as
(1) starting the RMI registry
(2) starting the server application
(3) starting the client application to connect it with the server application.

I want to build and test the application first on my local system before doing anything further. Both Server and client programs reside in one computer and in the same folder. The OS is windows 7. what prompted my posting this is because i've tried the options i'm aware of like:
(1) referencing the system variable java.rmi.server.hostname when running the server application
(2) Editing the \host folder in my OS

I kept recieving the same error message from the client application. I'll appreciate is someone could give advice on the way out. There is no issue with compiling both programs: This application relies on the client calling the server program to send the data entered on the client side to a database:

The client app is shown below:

mport javax.swing.*;
import java.rmi.*;
import java.awt.event.*;
import java.awt.*;

public class Aclienty
{
static JFrame frame;
static JPanel panel,panel1;
JLabel labelAuthorID,labelLastName,labelFirstName,labelPhone,labelAddress,labelCity,labelState,labelZip;
JTextField textAuthorID,textLastName,textFirstName,textPhone,textAddress,textCity,textState,textZip;
JButton submit;
static String authorID;
static String lastName;
static String firstName;
static String phone;
static String address;
static String city;
static String state;
static String zip;

public Aclienty()
{
frame = new JFrame("Earnest Publishing House");
panel = new JPanel();
panel1 = new JPanel();
panel.setLayout(new GridLayout(8,2));
panel1.setLayout(new GridLayout(1,1));
frame.setVisible(true);
frame.setSize(400,400);
frame.getContentPane().setLayout(new BorderLayout());

labelAuthorID = new JLabel("Author ID");
labelLastName = new JLabel("Last Name");
labelFirstName = new JLabel("First Name");
labelAddress = new JLabel("Address");
labelPhone = new JLabel("Phone");
labelCity = new JLabel("City");
labelState = new JLabel("State");
labelZip = new JLabel("Zip");

textAuthorID = new JTextField(5);
textLastName = new JTextField(15);
textFirstName = new JTextField(15);
textPhone = new JTextField(10);
textAddress = new JTextField(50);
textCity = new JTextField(10);
textState = new JTextField(10);
textZip = new JTextField(6);

submit = new JButton("Submit");

panel.add(labelAuthorID);
panel.add(textAuthorID);
panel.add(labelLastName);
panel.add(textLastName);
panel.add(labelFirstName);
panel.add(textFirstName);
panel.add(labelPhone);
panel.add(textPhone);
panel.add(labelAddress);
panel.add(textAddress);
panel.add(labelCity);
panel.add(textCity);
panel.add(labelState);
panel.add(textState);
panel.add(labelZip);
panel.add(textZip);
panel.add(submit);
ButtonListener blisten = new ButtonListener();
submit.addActionListener(blisten);
frame.getContentPane().add(new JPanel(), BorderLayout.WEST);
frame.getContentPane().add(new JPanel(), BorderLayout.EAST);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.getContentPane().add(panel1, BorderLayout.SOUTH);
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
JButton source = (JButton)evt.getSource();
MyDialog myDialog;
try
{
AuthorServer server = (AuthorServer)Naming.lookup("rmi://127.0.0.1/Authorserver");
authorID = textAuthorID.getText();
lastName = textLastName.getText();
firstName = textFirstName.getText();
phone = textPhone.getText();
address = textAddress.getText();
city = textCity.getText();
state = textState.getText();
zip = textZip.getText();

String str = server.insertDetails(authorID, lastName, firstName, phone, address, city, state, zip);
System.out.println(str);
if(str.equals("success"))
{
myDialog = new MyDialog(frame,"Inserted Successfully");
}
else
{
myDialog = new MyDialog(frame,"No record inserted");
}
myDialog.setVisible(true);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String[]args)
{
new Aclienty();
}
}
class MyDialog extends Dialog implements ActionListener
{
MyDialog(Frame parent, String title)
{
super(parent,title,false);
setLayout(new FlowLayout());
setSize(400,400);
add(new JLabel(title));
JButton buok = new JButton("OK");
add(buok);
buok.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
dispose();
}
}

The server app is shown below:

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;
import java.sql.*;
import java.sql.Connection;

public class AuthorServerImpl extends UnicastRemoteObject implements AuthorServer
{
static ResultSet result;
static Connection con;
static PreparedStatement stat;

public AuthorServerImpl() throws RemoteException
{
super();
}
public String insertDetails(String authorID,String lastName,String firstName,String phone,String address,String city,String state,String zip) throws RemoteException
{
int rowsAffected = 0;
String sReturn = "fail";
try
{

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/publishing","thepubs","eansbok");
stat = con.prepareStatement("insert into authors(AuthorID,Last_Name,First_Name,Phone,Address,City,State,Zip)values(?,?,?,?,?,?,?,?)");
stat.setString(1, authorID);
stat.setString(2, lastName);
stat.setString(3, firstName);
stat.setString(4, phone);
stat.setString(5, address);
stat.setString(6, city);
stat.setString(7, state);
stat.setString(8, zip);
rowsAffected = stat.executeUpdate();
if(rowsAffected > 0)
{
sReturn = "success";
}
}
catch(Exception ex)
{
System.out.println("Error at value insert" + ex);
}
return sReturn;
}
public static void main(String[]args)
{
System.setSecurityManager(new RMISecurityManager());
try
{

AuthorServerImpl instance = new AuthorServerImpl();
Naming.rebind("Authorserver", instance);
System.out.println("Server Registered");

}
catch(Exception ey)
{
System.out.println(ey);
}
}
}
This code was actually adopted from an NIIT Manual that i had wayback 2006 when i was a complete newbie to java. Now i intend trying out the RMI samples i have in the manual. But i feel there is something i'm missing. I'm not sure if there should a restructuring of this code due to API enhancements or anything else. I'm appreciate a rapid response...

Olakunle Oladipo Oni
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Question too difficult for “beginning”: moving.
Sujeeth Pakala
Ranch Hand

Joined: Mar 24, 2009
Posts: 94
Your code snippet is more about swing GUI. Question not clear, please rephrase it sorry !


Regards, Sujeeth Pakala
SCJP, SCWCD, SCBCD, ICAD, XML Master Basic, Certified SOA Professional
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RMI application connection issue