Connection refused to host exception in java RMI [duplicate] - java

I have written following code for the client of RMI. But getting
java.rmi.ConnectException: Connection refused to host: localhost; nested
exception is:
java.net.ConnectException: Connection refused: connect
code :
import java.rmi.*;
import java.net.*;
import java.rmi.registry.*;
class client
{
public static void main(String [] ars)
{
Iface serv;
Registry r;
String serveraddr = ars[0];
String serverport = ars[1];
String text = "Hey jude";
System.out.println("Sending" + text);
try{
r = LocateRegistry.getRegistry(
serveraddr,
(new Integer(serverport)).intValue()
);
serv = (Iface) r.lookup("rmi://server");
serv.receive(text);
}
catch(Exception e){
System.out.println(e);
}
}
}

If you're getting that on bind, rebind, or lookup, the Registry isn't running. If you get it doing the remote call, see item A.1 in the RMI FAQ supplied with the Javadoc, and if you're running Linux also check that your /etc/hosts file maps 127.0.0.1 to localhost and your real ip address to your real hostname - this has been a common problem in some Linux distributions.

I met the same problem. It's silly but just that I forgot to start the RMI registry process.
So, you also need to run RMI Registry process
rmiregistry
Before you try to rebind(address, obj) with RMI registry.

Related

Simple SocketHandler for java8 logging(java.util.logging) throws ConnectionException

The intention is to stream the log during runtime on a specific host:port, so that the logs are accessible to users outside the running system, from browser.
As you can see, i have created a simple SocketHandler for java8 logging(java.util.logging), is there something that i have missed?
import java.net.MalformedURLException;
import java.net.URL;
import java.io.IOException;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SocketHandler;
import java.util.logging.XMLFormatter;
public class Main {
public static void main(String[] args) throws Exception {
Logger logger = Logger.getLogger("concrete.log");
SocketHandler handler = new SocketHandler("HOSTNAME", 19004);
LogRecord logRec = new LogRecord(Level.INFO, "Log recorded");
handler.publish(logRec);
handler.setFormatter(new XMLFormatter());
logger.addHandler(handler);
logger.info("socket handler info message");
}
}
When i run the code, i see the following exception, i have tried checking the system firewall settings on both local(mac/windows) and remote(Linux) and seen that the settings do not block 19004 port
Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at java.net.Socket.connect(Socket.java:555)
at java.net.Socket.<init>(Socket.java:451)
at java.net.Socket.<init>(Socket.java:228)
at java.util.logging.SocketHandler.connect(SocketHandler.java:167)
at java.util.logging.SocketHandler.<init>(SocketHandler.java:154)
at Main.main(Main.java:16)
UPDATE
As suggested by bosowski
When i create Socket to listen to a specific port, the log messages are getting printed on the console of the host. However, am unable to access hostname:port for the log to be streamed from the browser. Is there anything specific that needs to be performed after this step?
Please let me know
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args)
{
try {
ServerSocket ss = new ServerSocket(19004);
Socket soc = ss.accept();
DataInputStream dis
= new DataInputStream(soc.getInputStream());
String str = (String)dis.readUTF();
System.out.println("message= " + str);
ss.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
SocketHandler does not open up a port to connect to, if that's what you're assuming. It tries to connect to the specified host and port, so you need to have a port that is listening on the host that you are trying to connect to.
https://docs.oracle.com/javase/8/docs/api/java/util/logging/SocketHandler.html#SocketHandler-java.lang.String-int-
<handler-name>.host specifies the target host name to connect to (no default).
<handler-name>.port specifies the target TCP port to use (no default).
If you do indeed have a listening TCP port on the hostname that you're trying to connect to, you can try running sudo nmap -F hostname to check if the port is indeed accessible from your machine.

Java RMI problem connecting a client to server

I am trying to make a simple program that connects two computers; one as a server and the other one as a client, but when I run the client program I get the following error:
Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused
java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:626)
at java.rmi/sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:217)
at java.rmi/sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:204)
at java.rmi/sun.rmi.server.UnicastRef.newCall(UnicastRef.java:345)
at java.rmi/sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:116)
at clienteRMI.main(clienteRMI.java:17)
Caused by: java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:579)
at java.base/sun.nio.ch.Net.connect(Net.java:568)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:576)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
at java.base/java.net.Socket.connect(Socket.java:666)
at java.base/java.net.Socket.connect(Socket.java:600)
at java.base/java.net.Socket.<init>(Socket.java:509)
at java.base/java.net.Socket.<init>(Socket.java:289)
at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createSocket(TCPDirectSocketFactory.java:40)
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:620)
Reading I have found the error may be the /etc/hosts file in the server, but I don't think something is wrong.
My /etc/hosts file:
127.0.0.1 localhost
::1 localhost
127.0.0.1 archjoni.localhost archjoni
I have also tried to write the IP of my server in the client java file before my registry by writing
System.setProperty("java.rmi.server.hostname", "192.168.1.13");
However, the problem remains the same.
My server java file:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* Servidor
*/
public class servidorRMI {
public static void main(String[] args) {
try {
Registry rmi = LocateRegistry.createRegistry(8080);
rmi.rebind("Chat", (Remote) new implementacionChat());
System.out.println("Servidor activo");
} catch (Exception e) {
e.printStackTrace();
}
}
}
My client java file:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import javax.swing.JOptionPane;
/**
* clienteRMI
*/
public class clienteRMI {
public static void main(String[] args) {
try {
String nombre = JOptionPane.showInputDialog("Ingresa tu nombre");
String nom = nombre;
System.setProperty("java.rmi.server.hostname", "192.168.1.13");
Registry rmii = LocateRegistry.getRegistry(8080);
chatServidor servidor = (chatServidor) rmii.lookup("Chat");
new Thread(new implementacionChatCliente(nom, servidor)).start();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
I don't know if it may be relevant but when I try the program with the localhost in the program work fine, the issue is that it takes my localhost IP when I try to connect to another computer.
Another thing to note is that both the server and the client are on Linux.

Java RMI for remote ip (host)

I am newbie. I cannot understand RMI correctly. There are tons of tutorials available on the internet ,but all of them are for the local host as I can understand. Both server and client run on the same machine.
I want to run client on any machine and the host will be on the one computer lets consider IP - 11.11.11.11. On the 1099.
But how can I achieve this, where should I specify my IP on the client side. As I understand naming convertion is used, like DNS but anyway when I need to connect to some machine remotely I need to know at least IP address (+mask) and port.
I guess I missed something really important.
Please give some example how to configure RMI remotly not on the same host.
First you have to setup a server whose method or object can be accessed by any remote client
Below is example code for the server.
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyCalc extends Remote{
int add(int a, int b) throws RemoteException;
}
import java.rmi.RemoteException;
public class MyCalcImpl implements MyCalc {
#Override
public int add(int a, int b) throws RemoteException {
return (a + b);
}
}
Start the rmi registry on server machine so you can register your object to this registry and better you run it where you have placed your classes otherwise you will get ClassNotFound.
rmiregistry 1099
Note: you might need to change the port if port is already in use.
Register you object to rmi registry with name 'calculator'.
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class MyRMIServer {
public static void main(String[] args) throws Exception {
System.setProperty("java.security.policy","file:///tmp/test.policy");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Calculator";
MyCalc engine = new MyCalcImpl();
MyCalc stub = (MyCalc) UnicastRemoteObject.exportObject(engine, 0);
Registry registry = LocateRegistry.getRegistry(1099);
System.out.println("Registering Calculator Object");
registry.rebind(name, stub);
} catch (Exception e) {
System.err.println("Exception:" + e);
e.printStackTrace();
}
}
}
Note: To run the program you have to setup a security policy file and for that creat a file e.g. test.policy and copy below content.
grant {
permission java.security.AllPermission;
permission java.net.SocketPermission "localhost:1099", "connect, resolve";
permission java.net.SocketPermission "127.0.0.1:1099", "connect, resolve";
permission java.net.SocketPermission "localhost:80", "connect, resolve";
};
You can change IP and port as per your case.
After starting the server, suppose your server's IP address is 11.11.11.11 then you can invoke the MyCalc's add() on the server. So on your client machine your client code would be like:
Copy the MyCalc class from server to client machine so you can set it to the classpath while compiling client's code.
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class MyRMIClient {
public static void main(String args[]) {
System.setProperty("java.security.policy","file:///tmp/test.policy");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Calculator";
String serverIP = "11.11.11.11"; // or localhost if client and server on same machine.
int serverPort = 1099;
Registry registry = LocateRegistry.getRegistry(serverIP, serverPort);
MyCalc mycalc = (MyCalc) registry.lookup(name);
int result = mycalc.add(10, 20);
System.out.println("Result:" + result);
} catch (Exception e) {
System.err.println("ComputePi exception:");
e.printStackTrace();
}
}
}
compile and test the client's code.
EDIT: edited to remove dependency on rmi compiler (rmic)
You only have to specify the server's IP address in one place: the lookup string supplied to Naming.lookup().
[Unless you have the Linux problem referred to in the RMI FAQ item A.1.]

Java JNDI DNS error: "Receive timed out"

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
public class DNSLookup
{
public static void main(String args[])
{
String host = "www.google.com";
try
{
InetAddress inetAddress = InetAddress.getByName(host);
// show the Internet Address as name/address
System.out.println(inetAddress.getHostName() + " " + inetAddress.getHostAddress());
// get the default initial Directory Context
InitialDirContext iDirC = new InitialDirContext();
// get the DNS records for inetAddress
Attributes attributes = iDirC.getAttributes("dns://8.8.8.8/www.google.com", new String[] {"A"});
// get an enumeration of the attributes and print them out
NamingEnumeration<?> attributeEnumeration = attributes.getAll();
System.out.println("");
while (attributeEnumeration.hasMore())
{
System.out.println("" + attributeEnumeration.next());
}
attributeEnumeration.close();
}
catch (UnknownHostException exception)
{
System.err.println("ERROR: Cannot access '" + host + "'");
}
catch (NamingException exception)
{
System.err.println("ERROR: No DNS record for '" + host + "'");
exception.printStackTrace();
}
}
}
If I run this code, I get error like below...
www.google.com 74.125.128.103
ERROR: No DNS record for 'www.google.com'
javax.naming.CommunicationException: DNS error [Root exception is java.net.SocketTimeoutException: Receive timed out]; remaining name 'www.google.com'
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:300)
at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:430)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:231)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:139)
at com.sun.jndi.toolkit.url.GenericURLDirContext.getAttributes(GenericURLDirContext.java:103)
at javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:142)
at DNSLookup.main(DNSLookup.java:24)
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:121)
at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:145)
at java.net.DatagramSocket.receive(DatagramSocket.java:786)
at com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:411)
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:203)
... 7 more
But if I use "dns:/www.google.com" as query, not "dns://8.8.8.8/www.google.com", it works perfectly without any error.
Error only happens when I try to specify DNS server to use.
"dns://8.8.8.8/www.google.com" -> ERROR
"dns:/www.google.com" -> Work!
http://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html
In this document, there is exact same usase what I want to use. I cannot understand why this makes problem.
DirContext ictx = new InitialDirContext();
Attributes attrs3 = ictx.getAttributes("dns://server1.example.com/host3.example.com",
new String[] {"MX"});
There is nothing wrong with your code, but I suspect that you're running it from behind a proxy or firewall that blocks outgoing DNS.
When you ask for dns:/www.google.com you're essentially asking your local DHCP-supplied resolver for the IP of www.google.com - just as when typing the address in the browser address line.
However, when using dns://8.8.8.8/www.google.com you're asking to use Google DNS to resolve google.com which requires that your proxy/firewall allows outgoing TCP/UDP port 53 traffic, which is usually not the case in corporate environments...
Cheers,

Java RMI : connection refused

I have written following code for the client of RMI. But getting
java.rmi.ConnectException: Connection refused to host: localhost; nested
exception is:
java.net.ConnectException: Connection refused: connect
code :
import java.rmi.*;
import java.net.*;
import java.rmi.registry.*;
class client
{
public static void main(String [] ars)
{
Iface serv;
Registry r;
String serveraddr = ars[0];
String serverport = ars[1];
String text = "Hey jude";
System.out.println("Sending" + text);
try{
r = LocateRegistry.getRegistry(
serveraddr,
(new Integer(serverport)).intValue()
);
serv = (Iface) r.lookup("rmi://server");
serv.receive(text);
}
catch(Exception e){
System.out.println(e);
}
}
}
If you're getting that on bind, rebind, or lookup, the Registry isn't running. If you get it doing the remote call, see item A.1 in the RMI FAQ supplied with the Javadoc, and if you're running Linux also check that your /etc/hosts file maps 127.0.0.1 to localhost and your real ip address to your real hostname - this has been a common problem in some Linux distributions.
I met the same problem. It's silly but just that I forgot to start the RMI registry process.
So, you also need to run RMI Registry process
rmiregistry
Before you try to rebind(address, obj) with RMI registry.

Categories

Resources