I have this RMI server that I am trying to connect to from home and I get this error message:
java.rmi.ConnectException: Connection refused to host: XX.XXX.XX.XXX; nested exception is: java.net.ConnectException: Connection timed out: connect
public GUILogic(GUI gui) throws NotBoundException, MalformedURLException, RemoteException{
super();
this.gui = gui;
String objectname = "chatter";
String url = "rmi://" + hostname + "/" + objectname;
cf = (ChatFront) Naming.lookup(url);
}
I have port forwarded the port 1099 (on the server computer), when I try http://www.canyouseeme.org/, I find the port. Every firewall is down on my computer and the server. The server is working fine if I set it up on a LAN.
What can the problem be?
I've got the exact same issue.
I fixed it by setting a hostname, which can be resolved by both server (behind NAT) and client.
Then, I added this arg to the server, in the java command line :
-Djava.rmi.server.hostname=yourhostname
And it worked.
On the server side, a simple getRegistry worked perfectly.
Registry registry = LocateRegistry.getRegistry();
Don't forget to nat registry and exported object ports.
Related
I have the a docker container (Name CON1) running in the host A, this container is a java RMI server listening on port 1099. I also have another the container (Name CON2) running a java RMI client in the host B. When I telnet CON1 from CON2 using the RMI port 1099 it works fine:
$ telnet 172.30.34.74 1099
Trying 172.30.34.74...
Connected to 172.30.34.74.
Escape character is '^]'.
But when I try to connect through the java RMI client the connection is refused and the error message shows a different IP address for CON1.
java.rmi.ConnectException: Connection refused to host: 172.18.0.2; nested exception is:
java.net.ConnectException: Connection refused (Connection refused)
Although I use 172.30.34.74 to make the lookop in the RMI client code, the error message shows other IP address (172.18.0.2). When I run the RMI server and the RMI client outside a docker container it works good.
What can I check to solve this situation?
I implemented a solution:
Add to the java command line to start the RMI server the argument -Djava.rmi.server.hostname=HOST_IP. Where host HOST_IP is the host IP.
Set the container's network_mode to host. This way, the container would take same IP as host.
How can I connect to internet server from Java desktop application? I need to access MySQL database and upload/download files. The host that I have (one.com), don't support remote database access, so I've tried with SSH. I don't know anything at all about this. I've tried various codes examples but none of them pass further than connection. I add jsch.jar to my project. Is there something else that I have to add/install or what em I missing?
public static void main(String args[])
{
String user = "user";
String password = "pass";
String host = "00.000.00.000";
int port=22;
String remoteFile="/home/mywebsite.com/test.txt";
try
{
JSch jsch = new JSch();
Session session = (Session) jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
System.out.println("Creating SFTP Channel.");
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
System.out.println("SFTP Channel created.");
InputStream out= null;
out= sftpChannel.get(remoteFile);
try (BufferedReader br = new BufferedReader(new InputStreamReader(out))) {
String line;
while ((line = br.readLine()) != null)
System.out.println(line);
}
}
catch(JSchException | SftpException | IOException e){System.err.print(e);}
}
I get the error:
com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect
BUILD SUCCESSFUL (total time: 24 seconds)
Or what other alternatives I have to achieve a remote connection to my site & database?
P.S. I have tried almost all the example-codes from stackoverflow and all get stuck in the same point ...
Thank you!
Connection timed out indicates that you're trying to connect to a server that isn't accepting SSH connections on port 22, or your host is incorrect. Is host = "00.000.00.000"; the actual IP address you're trying to connect to, or did you change it for your Stack Overflow question?
I'm not going to debug your code, but will tell you how to accomplish this.
The solution is to use SSH "port forwarding", which you should first set up from the command line, to learn how it works, before embedding it in your Java code. You may not need to write any code if a command-line tunnel will suffice.
ssh -L3306:localhost:3306 <databaseHost>
This creates an SSH "tunnel" from your computer's port 3306 to the <databaseHost> and then on to localhost from <databaseHost>. Any connection on your computer to your local port 3306 gets tunnelled across SSH to <databaseHost> and then on to whatever <databaseHost> knows as localHost, i.e. back to itself, on port 3306.
When your Java code connects to the database, it should connect to localhost:3306. Note that the localhost in your database connect string refers to your computer, while the one in the ssh command is from the point of view of the remote server, so they mean different things.
In summary, a packet from your Java code to the remote MySQL server travels the following route:
Connect to my computer port 3306
Intercepted by ssh
Encrypted and transmitted to remote database server
Decrypted by sshd and fed to port 3306 on the same server
Received by MySQL running on the remote host
For return packets, this process is reversed, managed by the local ssh client and the remote sshd daemon.
Note that this can be used for more interesting cases. For example, suppose the remote ssh server and database server are on different systems behind the same firewall. Let's say the ssh host is visible as gateway.xyz.com, while the database host is on an internal LAN at 10.0.0.3 which is not reachable from the Internet, but reachable from the gateway.
ssh -L3306:10.0.0.3:3306 gateway.xyz.com
The packet route is then
Your computer 3306
Received by ssh
Encryption and sent to gateway.xyz.com
Decrypted by sshd forwarded to 10.0.0.3:3306
Received at 10.0.0.3:3306 by MySQL
To be clear, this is something you set up from the command line, before executing your Java code, which should happen in a different session from the ssh command. The tunnel remains available as long as the ssh command is running.
I have this tiny java http server:
public class HttpServer {
public static void main(String args[]) {
int port;
ServerSocket server_socket;
try {
port = Integer.parseInt(args[0]);
} catch (Exception e) {
port = 8080;
}
try {
server_socket = new ServerSocket(port, 0, InetAddress.getByName("localhost"));
System.out.println("httpServer running on port "
+ server_socket.getLocalPort()
+ " address " + server_socket.getInetAddress()
);
} catch (IOException e) {
System.out.println(e);
}
}
}
When I connect with google chrome to localhost IDE console writes following:
httpServer running on port 8080 address localhost/127.0.0.1
New connection accepted /127.0.0.1:54839
New connection accepted /127.0.0.1:54840
Seems like google chrome connects two times to the server, but changing its port.
Why is it can be?
Since port 8080 on your client is already taken, the client's operating system will map the connection to a different unused port on the client. Your client connects from port 54839 and 54840 to port 80 on the server. To allow another client to connect to your server, the port is automatically redirected to an untaken port.
Here's a list of what happens...
Client opens up a Socket to connect to your server
Client's OS checks if the port the Socket is trying to connect to is used and if not looks for an unused port.
Client's OS assigns the socket to the unused local port it's detected in step 2.
Server receives connection request and accepts it. Server's OS redirects the connection from port 8080 to another port to allow more clients to connection.
Client and server have a chat and then disconnect.
54839 and 54840 are ports the OS assigned to the two Sockets your browser created when attempting to connect to your local website.
Edit: To correctly answer your question, the resources you send the browser causes it to connect twice. Once to retrieve the first resource and a second time to retrieve the resource the first requires.
My server's code looks like:
Registry r = java.rmi.registry.LocateRegistry.createRegistry(1399);
r.rebind("Chat", new IRC());
and my client's code is
IRCInterface remoteObject = (IRCInterface) Naming.lookup("rmi://localhost:1399/Chat");
String history = remoteObject.read();
on the localhost it works correctly, but I can't connect two remote computers (hosts).
I've turned off all firewalls.
What's wrong?
The console outputs:
Error: java.rmi.ConnectException: Connection refused to host: 150.254.79.20; nested exception is:
java.net.ConnectException: Connection timed out: connect
Naming.lookup("rmi://localhost:1399/Chat");
localhost in above lookup should be replaced with remotehost IP (or) machine name. Otherwise lookup happens on only local machine.
I'am trying to connect to a MS-SQL 2008 R2 server which is located on a 10.x.x.x address in my network.
My java code looks as follows
public void getConnection() throws ClassNotFoundException, SQLException
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://10.x.x.x:1433/TestTv","username","password");
}
But i get an error as follows
com.microsoft.sqlserver.jdbc.SQLServerException: The port number
1433/TestTv is not valid.
I have also tried with port 1443 and same issue. How and where can i check on my 2008 R2 server what port to use? Under sql server configuration manager Protocols for SQLEXPRESS TCP/IP port is set to 1443 and the port under client protocols is set to 1433.
Anyone who can point me in the right direction?
Why are you adding /TestTV onto the end of the address? Try it without that.
If TestTV is a SQL instance, the syntax is jdbc:sqlserver://10.x.x.x\TestTv:1433
Port 1433 is the standard port.