Through the use of many articles and Stack Overflow questions, I was finally able to successfully bind a Java Socket to a port (port 8000) on an Openshift website.
I know the binding was successful because no exceptions were thrown, and I was able to poll the Socket for its port number. I can also successfully connect to the socket from a client application I have, after using the rhc client tools to forward the ports.
The problem is, how do I connect to the server socket without port forwarding? I'm not able to connect to the server from any other computers, or from my own computer without port forwarding.
These are the things that I have tried so far:
Connecting to "localhost" at port 8000, which works after port forwarding.
Connecting to the website URL at port 8000, which always returns null.
Connecting to the website IP at port 8000, which results in a "Connection refused: connect" error.
Client code:
InetAddress ip = InetAddress.getByName("localhost");
//Also tried '127.5.8.129' which openshift says is the website IP, and the website URL itself.
Socket socket = new Socket(ip, 8000);
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("Hello World");
out.flush();
System.out.println(in.readLine());
socket.close();
EDIT: I can also bind the Server Socket to port 8443, but still can't access it remotely.
You need to bind to port 8080, and then access your application at it's public url on port 8000. Check out this kb article for more information about how the ports work on OpenShift: https://help.openshift.com/hc/en-us/articles/203263674-What-external-ports-are-available-on-OpenShift-
Related
I'n using Kotlin coroutines to setup a Java serversocket in Android studio 4.0 Beta 5. I'm running in the emulator on Windows 10. When my very reliable c language socket client attempts to connect using 127.0.0.1 as the IP it receives error 10061. The same client program has worked well for many years with a Java Swing socketserver.
Google give the following explanation for error 10061:
10061 is a Connection Refused error sent to you by the server. You could not make a
connection because the target machine actively refused it. The most common cause is a
misconfigured server, full server, or incorrect Port specified by the client.
Here's my code snippet
int myPort = 8080;
String localIP = InetAddress.getLocalHost().getHostAddress();
ServerSocket srv = new ServerSocket();
String hostname = InetAddress.getLocalHost().getHostName();
srv.bind(new InetSocketAddress(hostname,myPort));
srv.setSoTimeout(socketAcceptTimeOut); //This sets the timeout on the accept
Socket cli = srv.accept();
I used the server bind based on another stackoverflow answer but it did't help when I removed it. In any case I believe the serversocket is listening at 127.0.0.1. I'm using port 8080 but I've tried a few others.
On the Android side the srv.accept is just timing out.
What am I missing?
Thanks
getting the "no route to host exception" when trying to connect to a IPv6 address in Java . Using the simple Java code to connect like below
socket = new Socket();
SocketAddress serverAddress = new InetSocketAddress(host, port);
socket.connect(serverAddress, timedout);
It means that you don't have IPv6 configuration on your computer. To check that, try connecting to ::0 (Loopback) it should work even without any configurations.
If you want to use addresses other than ::0, you should find an ISP that support it or just configure a LAN between two or more computers (or VMs).
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.
I am writing a code where the android phone is the client trying to connect to the server on my pc USING WIFI. I am opening the sockets as follows:
try {
servsock = new ServerSocket(13299);
System.out.println("Listening :13299");
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
Socket sock = servsock.accept();
and on client side:
Socket sock = new Socket("192.168.0.108", 13299);
Log.i("sending","sending");
However I am receiving on the logcat: "No route to host" I have searched and inserted permission to use internet in the manifest.xml and did a ping from my phone with ip 192.168.0.107 to the pc server with ip 192.168.0.108.
What Am I missing? Why isn't the tcp socket connection established ? the server is written in netbeans. Does it have to do with the ports?
EDIT: I think the problem is in the IP addresses. I set the address of the server (private address) according to the output of "ipconfig" in cmd window.
I had the same issue, i changed the internet access point and the app worked. My app was using a local IP to access the server.
It must be some sort of blockage that keeps your connection to the server out of the scope for your client. Try applying different ports, and see what happens then.