Java RMI Method Call Connection Refused with OpenVPN - java

Grrr, I feel that this should be so obvious yet I can't find the solution.
For testing purposes I have created a simple Client and Server for an RMI application. I can successfully run the client and server over a local network. I can also successfully run the application when connected through a Windows 10 Built-In VPN connection. However, when connected via an OpenVPN connection I receive a Connection refused to host exception when calling a method (String response = lookup.helloTo(txt); in the Client code) on the remote object returned from the successful call to lookup = (RMIInterface)Naming.lookup("//192.168.10.14/MyServer");.
All my Google searches state that the java.rmi.server.hostname property must be set which I have done in this case. I am thinking that the issue may be between the VPN client IP address (10.8.0.14) and the server IP address (192.168.10.14). I can ping the server IP address and telnet to port 1099 on the server successfully.
Here is my code. I was hoping if someone could see something that I am missing or doing wrong or if I am on the right track regarding the VPN Client IP <=> Server IP.
Remote Interface:
package devel;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RMIInterface extends Remote {
public String helloTo(String name) throws RemoteException;
}
Server:
package devel;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ServerOperation extends UnicastRemoteObject implements RMIInterface {
private static final long serialVersionUID = 1L;
protected ServerOperation() throws RemoteException {
super();
}
#Override
public String helloTo(String name) throws RemoteException {
System.err.println(name + " is trying to contact!");
return "Server says hello to " + name;
}
public static void main(String[] args) {
try {
java.lang.System.setProperty("java.rmi.server.hostname", "192.168.10.14");
java.rmi.registry.LocateRegistry.createRegistry(1099);
Naming.rebind("//192.168.10.14/MyServer", new ServerOperation());
System.err.println("Server is ready");
} catch(Exception exc) {
System.err.println("Server exception: " + exc.toString());
exc.printStackTrace();
}
}
}
Client:
package devel;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import javax.swing.JOptionPane;
public class ClientOperation {
private static RMIInterface lookup;
public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
lookup = (RMIInterface)Naming.lookup("//192.168.10.14/MyServer");
String txt = JOptionPane.showInputDialog("What is your name?");
String response = lookup.helloTo(txt);
JOptionPane.showMessageDialog(null, response);
}
}
Exception received:
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 192.168.10.14; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy0.helloTo(Unknown Source)
at devel.ClientOperation.main(ClientOperation.java:17)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 8 more

Related

Failing to connect client server to local network

I was learning socket programming in Java and had written a simple program for connecting a client server to a socket server on a local host. But every time when I run the ClientServer program it gives an error stating connection :refused.
I am enclosing both the client code and server code that i wrote and also enclosing the console output.
package Classes;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientSer {
/**
* #param args
* #throws IOException
* #throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
Socket s = new Socket("localhost",1029);
OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
String str = "tEST mESSAGE";
os.write(str);
os.flush();
}
}
This is the server code:
package Classes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketSer {
public static void main(String args[]) throws Exception{
ServerSocket ss = new ServerSocket(1029);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = br.readLine();
System.out.print(str);
}
}
Console output:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Classes.ClientSer.main(ClientSer.java:18)
In the server, do you know what interface it's listening on?
I would start the server and then use telnet to try to connect rather than your client. You might find the server is NOT listening on localhost. Or maybe it's listening on every interface (127.0.0.1 plus your local network). I just don't know if I trust SocketServer(portNumber).
I would consider creating an unbound SocketServer and then use socket.bind(), feeding it an InetSocketAddress object constructed with both host and port.

Java SSL Client - Server: unable to find valid certification

I built a client-server SSL app in which I want to make some tests but I have problem with the client. The server is running ok, but when I try to run the client I have this message:
Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at sun.security.ssl.AppOutputStream.write(Unknown Source)
at java.io.OutputStream.write(Unknown Source)
at ssl.Client.main(Client.java:17)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 10 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 16 more
Server:
package ssl;
import java.io.PrintStream;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
public class Server {
private static final String HOST = "localhost";
private static final int PORT = 3443;
public static void main(String[] args) throws Exception {
System.setProperty("javax.net.ssl.keyStore", "DebKeyStore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "iliebc");
SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
ServerSocket ss = ssf.createServerSocket(PORT, 0, InetAddress.getByName(HOST));
System.out.println("Server started on port " + PORT);
while (true) {
Socket s = ss.accept();
SSLSession session = ((SSLSocket) s).getSession();
//System.out.println(session.getLocalCertificates());
Certificate[] cchain2 = session.getLocalCertificates();
for (int i = 0; i < cchain2.length; i++) {
System.out.println(((X509Certificate) cchain2[i]).getSubjectDN());
}
System.out.println("Peer host is " + session.getPeerHost());
System.out.println("Cipher is " + session.getCipherSuite());
System.out.println("Protocol is " + session.getProtocol());
System.out.println("ID is " + new BigInteger(session.getId()));
System.out.println("Session created in " + session.getCreationTime());
System.out.println("Session accessed in " + session.getLastAccessedTime());
PrintStream out = new PrintStream(s.getOutputStream());
out.println("Hi");
out.close();
s.close();
}
}
}
Client:
package ssl;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.net.ssl.SSLSocketFactory;
public class Client {
private static final String HOST = "localhost";
private static final int PORT = 3443;
public static void main(String[] args) throws Exception {
SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket socket = sf.createSocket(InetAddress.getByName(HOST), PORT);
OutputStream out = socket.getOutputStream();
System.out.println(out);
out.write("\nConnection established.\n\n".getBytes());
out.flush();
int theCharacter = 0;
theCharacter = System.in.read();
while (theCharacter != '~') { // The '~' is an escape character to exit
out.write(theCharacter);
out.flush();
theCharacter = System.in.read();
}
out.close();
socket.close();
}
}
How may I solve this exception?
Solution:
System.setProperty("javax.net.ssl.keyStore", "DebKeyStore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "iliebc");
System.setProperty("javax.net.ssl.trustStore", "DebKeyStore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "iliebc");

Sending messages using sockets on local host

I am working on messaging over sockets on my local machine. I am trying to message myself, which is emulating me messaging over the internet. Anyhow, I had gotten a bind exception earlier but it seems I may have gotten past that. Now I am getting the :
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at class2.main(class2.java:10)
How do I get this connection to work and my messages to pass. Here are the two classes in my program:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class IPMessenger {
public static void main(String[] args) throws IOException{
ServerSocket SC = new ServerSocket(4800);
Socket socket = SC.accept();
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
System.out.println(in.readDouble());
System.out.println("hi");
// out.writeChars("hello");
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class class2 {
public static void main(String[] args) throws UnknownHostException, IOException{
Socket sock = new Socket("localhost",4800);
DataInputStream in = new DataInputStream(sock.getInputStream());
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
out.writeDouble(5);
//System.out.println(in.readChar());
}
}
java.net.ConnectException: Connection refused: connect
Your server isn't running when you run the client.

Connecting Java App to my online database JDBC MySql

I am trying to fetch data from my Online database, but following error is coming. However it is working fine for my local database.
Java Code
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.sql.*;
import javax.swing.JOptionPane;
public class BankingSystem extends JFrame {
public static void main(String[] args) throws Exception
{
int ur=0;
int PIN;
String ID;
int userSelection=0;
Boolean pinCorrect = false;
int amountDeposite;
int amountWithdraw;
int updateResult;
Boolean isExit=false;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://mysql11.000webhost.com/a46854323_javaapp";
String user = "a46854323_javaapp";
String pass="password";
Connection con = DriverManager.getConnection(url,user,pass);
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
}
and I get the following error:
`
Exception in thread "main" java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.ConnectException: Connection timed out: connect
java.net.SocketException
MESSAGE: java.net.ConnectException: Connection timed out: connect
STACKTRACE:
java.net.SocketException: java.net.ConnectException: Connection timed out: connect at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:143)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at BankingSystem.main(BankingSystem.java:22)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1875)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at BankingSystem.main(BankingSystem.java:22)
What can be the solution ?
I have gone through all the comments above and found that 000host.com doesn't provide the facility that its database can get accessed by outside its own space. So with little googling i found http://www.db4free.net that provide free mySql database for testing purpose. And in this way my problem get solved. Thanks to all comentators.

How to fix java.lang.VerifyError?

I am doing a project in java called visual speech recognition . while iam trying to run the below code in eclipse indigo..
package main.java.edu.lipreading;
import com.googlecode.javacpp.BytePointer;
import com.googlecode.javacv.cpp.opencv_core;
import main.java.edu.lipreading.vision.AbstractFeatureExtractor;
import main.java.edu.lipreading.vision.NoMoreStickersFeatureExtractor;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketHandler;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.logging.Logger;
import static com.googlecode.javacv.cpp.opencv_core.CV_8UC1;
import static com.googlecode.javacv.cpp.opencv_core.cvMat;
import static com.googlecode.javacv.cpp.opencv_highgui.cvDecodeImage;
/**
* Created with IntelliJ IDEA.
* User: Sagi
* Date: 25/04/13
* Time: 21:47
*/
public class WebFeatureExtractor extends Server {
private final static Logger LOG = Logger.getLogger(WebFeatureExtractor.class.getSimpleName());
private final static AbstractFeatureExtractor fe = new NoMoreStickersFeatureExtractor();
public WebFeatureExtractor(int port) {
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(port);
addConnector(connector);
WebSocketHandler wsHandler = new WebSocketHandler() {
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
return new FeatureExtractorWebSocket();
}
};
setHandler(wsHandler);
}
/**
* Simple innerclass that is used to handle websocket connections.
*
* #author jos
*/
private static class FeatureExtractorWebSocket implements WebSocket, WebSocket.OnBinaryMessage, WebSocket.OnTextMessage {
private Connection connection;
public FeatureExtractorWebSocket() {
super();
}
/**
* On open we set the connection locally, and enable
* binary support
*/
#Override
public void onOpen(Connection connection) {
LOG.info("got connection open");
this.connection = connection;
this.connection.setMaxBinaryMessageSize(1024 * 512);
}
/**
* Cleanup if needed. Not used for this example
*/
#Override
public void onClose(int code, String message) {
LOG.info("got connection closed");
}
/**
* When we receive a binary message we assume it is an image. We then run this
* image through our face detection algorithm and send back the response.
*/
#Override
public void onMessage(byte[] data, int offset, int length) {
//LOG.info("got data message");
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
bOut.write(data, offset, length);
try {
String result = convert(bOut.toByteArray());
this.connection.sendMessage(result);
} catch (Exception e) {
LOG.severe("Error in facedetection, ignoring message:" + e.getMessage());
}
}
#Override
public void onMessage(String data) {
LOG.info("got string message");
}
}
public static String convert(byte[] imageData) throws Exception {
opencv_core.IplImage originalImage = cvDecodeImage(cvMat(1, imageData.length, CV_8UC1, new BytePointer(imageData)));
List<Integer> points = fe.getPoints(originalImage);
if(points == null)
return "null";
String ans = "";
for (Integer point : points) {
ans += point + ",";
}
return ans;
}
/**
* Start the server on port 999
*/
public static void main(String[] args) throws Exception {
WebFeatureExtractor server = new WebFeatureExtractor(9999);
server.start();
server.join();
}
}
i am getting this error..
Exception in thread "main" java.lang.VerifyError: class org.eclipse.jetty.server.nio.AbstractNIOConnector overrides final method newBuffer.(I)Lorg/eclipse/jetty/io/Buffer;
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at main.java.edu.lipreading.WebFeatureExtractor.<init>(WebFeatureExtractor.java:33)
at main.java.edu.lipreading.WebFeatureExtractor.main(WebFeatureExtractor.java:118)
i got dis code from https://github.com/sagioto/LipReading/blob/master/lipreading-core/src/main/java/edu/lipreading/WebFeatureExtractor.java..
can anyone help me plz..
java.lang.VerifyError can happen if you're running against a different version of a library than the code was compiled against.
Looking in the github repository, I see the files are dated 25 April 2013. Apparently, Jetty has received an update during these last 10 months. Find the version of Jetty that was current on that date and try again.

Categories