It concerns me that the code sample you provide is not compilable,
that the classes you show use a very peculiar definition of equals(),
and that that definition is not consistent with their hashCode()
implementations.
Also, for readability, please reduce your indent level to a maximum of
four spaces.
Casting int to int is not really needed. Overriding hashCode() to do
nothing different is also completely unnecessary.
You wrote an entire class in order to implement a method that covers
for one expression:
'new Socket( host, port )'
and you don't even do any error-checking to justify the overhead of
the extra class. That is sub-optimal engineering.
So any two members of this class are considered equal if they merely
belong to the same class? That doesn't make sense. I don't see any
reason to override equals() whatsoever.
The next class has the exact same problems - not compilable, performs
little to no useful work, and uses a peculiar and useless override of
equals() and hashCode().
And, of course, you provide absolutely no context for how you are
using the one line of code from each class that does anything useful.
I suggest that you work up an SSCCE
<
http://pscode.org/sscce.html>
which will do two things - it will help you find your problem, and it
will help us find your problem.
Thanks for your reply, I guess I did it wrong when I tried some code
sample modified not very well and took some code from my project.
I wrote a small standalone sample (without error handling) to
demonstrate the same problem (I hope I did it better now).
In the sample I implemented server socket factory so I'll be able to
bind it to the localhost and a client factory because it didn't work
without it.
The problem is still that it seems that the client is trying to bind
to the DNS address instead of the loopback interface and provides the
following exception:
Exception in thread "main" java.rmi.ConnectException: Connection
refused to host: 143.xx.xx.xx; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:
601)
at
sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:
184)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:110)
at
java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:
178)
at
java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:
132)
at $Proxy0.sayHello(Unknown Source)
at test.Client.main(Client.java:14)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:
195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at test.Server$MyClientSocketFactory.createSocket(Server.java:24)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:
595)
... 7 more
Interface:
==========
package test;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
public abstract String sayHello() throws RemoteException;
}
Server:
=======
package test;
import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
static public class MyClientSocketFactory implements
RMIClientSocketFactory, Serializable {
private static final long serialVersionUID =
-2907595020431154956L;
public Socket createSocket(String host, int port) throws
IOException {
return new Socket(host, port);
}
}
static public class MyServerSocketFactory implements
RMIServerSocketFactory {
public ServerSocket createServerSocket(int port) throws
IOException {
InetAddress addr = InetAddress.getByName(null);
return new ServerSocket(port, 0, addr);
}
}
@Override
public String sayHello() throws RemoteException {
return "Hello";
}
public void init() throws RemoteException {
int port = 1099;
String name = "HelloInterface";
boolean exportRegistry = true;
try {
LocateRegistry.createRegistry(port);
} catch (RemoteException e) {
// failed to create the registry, no need to export it
exportRegistry = false;
}
Registry registry = LocateRegistry.getRegistry(port);
if (exportRegistry) {
UnicastRemoteObject.exportObject(registry, port);
}
RMIClientSocketFactory csf = new MyClientSocketFactory();
RMIServerSocketFactory ssf = new MyServerSocketFactory();
Remote stub = UnicastRemoteObject.exportObject(this, 0, csf,
ssf);
registry.rebind(name, stub);
}
public static void main(String[] args) throws RemoteException,
InterruptedException {
Server server = new Server();
server.init();
// keep server alive for a while
Thread.sleep(1000000);
}
}
Client:
=======
package test;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) throws RemoteException,
NotBoundException {
Client client = new Client();
Hello hello = client.init();
System.out.println(hello.sayHello());
}
private Hello init() throws RemoteException, NotBoundException {
int port = 1099;
String name = "HelloInterface";
String host = null;
Registry registry = LocateRegistry.getRegistry(host, port);
Hello hello = (Hello) registry.lookup(name);
return hello;
}
}