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.
Related
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.
I am implementing a Client application and a Server application on my Windows computer using two terminals that communicate with each other.
However I cannot get the Client to run. The Server and Java RMI registry run successfully. I have 3 files: Client.java, Server.java and Hello.java
import java.rmi.registry.LocateRegistry; //Client.java
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
import java.rmi.registry.Registry; //Server.java
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
import java.rmi.Remote; //Hello.java
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
All three Java files are located in C:\Program Files\Java\jdk1.8.0_321\bin. After compiling them to the same directory, I run the following commands in the command prompt as administrator.
start rmiregistry -J-Djava.class.path=./
Java RMI registry starts successfully in a new command prompt window (no output).
start java Server
Server starts sucessfully in a new command prompt which outputs "Server ready".
java Client
This command is unsuccessful and outputs the following error:
java.net.SocketException: Permission denied: connect
java.rmi.ConnectIOException: Exception creating connection to: 192.168.1.13; nested exception is:
java.net.SocketException: Permission denied: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:635)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:131)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:235)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:180)
at com.sun.proxy.$Proxy0.sayHello(Unknown Source)
at Client.main(Client.java:15)
Caused by: java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:75)
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.PlainSocketImpl.connect(PlainSocketImpl.java:162)
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 sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:617)
... 7 more
C:\Program Files\Java\jdk1.8.0_321\bin>_
I am unsure how to get the Client running successfully. I disabled windows firewall and am not using a VPN.
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.
This is my client code
package com.tutorialspoint.test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.tutorialspoint.stateless.testRemote;
public class test
{
BufferedReader brConsoleReader = null;
Properties props;
static InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args)
{
test _test = new test();
try {
testRemote libraryBean =(testRemote)ctx.lookup("java:global/TestEjb/testBean!
com.tutorialspoint.stateless.testRemote");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This is deployed on server
package com.tutorialspoint.stateless;
import javax.ejb.Stateless;
#Stateless
public class testBean implements testRemote
{
public void hello()
{
System.out.println("hello");
}
}
package com.tutorialspoint.stateless;
import javax.ejb.Remote;
#Remote
public interface testRemote
{
void hello();
}
I am getting exception on server side:
19:09:12,676 ERROR [org.jboss.remoting.remote.connection] (Remoting "cognam-
pc-26" read-1) JBREM000200: Remote connection failed:
java.io.IOException: An existing connection was forcibly closed by the
remote host
I am getting exception on client side:-
javax.naming.CommunicationException: Could not obtain connection
to any of these urls: localhost:4447 and discovery failed with error:
javax.naming.CommunicationException: Receive timed out [Root exception
is java.net.SocketTimeoutException: Receive timed out] [Root exception
is javax.naming.CommunicationException: Failed to retrieve stub from
server localhost:4447 [Root exception is java.io.StreamCorruptedException:
invalid stream header: 0000000F]]
org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1414)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:594)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.tutorialspoint.test.test.main(test.java:37)
It might be that your server is not actually allowing connections to 4447. Check if you can telnet to that port or use netstat -an to query this.
Also ensure that there is no firewall blocking access to the port.
Try shutdown any http traffic capture tools if running (e.g. Fiddler)
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.