I face a timeout when trying to connect from my java websocket client to my websocket server that is deployed in a EC2 instance.
The connection works fine in my local machine and the security group allows all TCP incoming traffic.
My assumption it is that there's a networking step I'm missing.
Steps I followed to deploy my server socket on EC2:
Launch EC2 instance (Amazon Linux)
Install java and maven
Exec command mvn clean -X and mvn install
Exec command java -jar mywebserver.jar
--> result: The code is running in port xxx
Server socket creation:
try {
s = new ServerSocket(port); //listen on specified port
Log.add("Port " + port + " address: " + s.getInetAddress() + ": server started");
} catch (IOException ex) {
Log.add("Server error " + ex + "(port " + port + ")");
throw new Exception("Error "+ex);
}
Client web
public Client(String serverIp, int serverPort) throws UnknownHostException, IOException {
s = new Socket(serverIp, serverPort);
}
Thanks a lot for your help.
Felix
I'd suggest :
Double-check firewall settings
try to ping/telnet server machine from client etc..
Use InetAddress in your Socket
initialization on client code as you could also use its isReachable()
method
I think you're also missing the below part on server side :
Socket socket = s.accept();
Please see some client/server socket examples, eg :
https://www.journaldev.com/741/java-socket-programming-server-client
Related
I'm currently trying to do some network coding for an android (java) application and I'm facing some problems. I use the Apache library commons.net in order to establish an ftp connection to a server I'm hosting for file transfer to the android unit. this is my code:
public class Server {
public static void main(String[] args)
{
String username = "Username";
String password = "Password";
String host = "AddressString";
FTPSClient ftps;
ftps = new FTPSClient();
System.out.println("trying to connect...");
try{
System.out.println("trying to connect...");
ftps.connect(host, 21);
System.out.println("Connected");
System.out.println("logging in...");
ftps.login(username, password);
System.out.println("logged in!");
ftps.enterLocalPassiveMode();
catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftps.isConnected()) {
System.out.print("LOggin out");
ftps.logout();
ftps.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println("Terminated");
}
}
The program never gets passed the line "ftps.connect(host, 21);", with the error "Connection closed without indication", I do belive I have configured my server correctly since I can connect to it via "Putty" from another network etc. What am I missing here?
Note: I am not trying to connect through an Android device, I'm currently using eclipse for testing.
"I do belive I have configured my server correctly since I can connect to it via "Putty" from another network etc. What am I missing here?"
The "putty" utility talking to an SSH server on port 22 not to an FTP server on port 21. You can say that basic network connectivity / routing appears to be working, but that's not necessarily enough.
Possible problems you may be having include:
the FTP port being blocked by firewalling on the client, the server or somewhere in between, or
the FTP server may be configured incorrectly.
I noticed that the client is trying to use FTP/S but it it using the default port for FTP (21) not FTP/S (991). This is not necessarily wrong (see https://serverfault.com/questions/10807/what-firewall-ports-do-i-need-to-open-when-using-ftps for details) but maybe you should check that your server is configured to support explicit FTP/S.
I would advise:
look at the server and client side logs for clues
temporarily turn on "debug level" logging (client & server side)
see if you can establish a raw TCP connection on port 21; e.g. using telnet <server-host> 21
try changing the client to use FTP rather than FTP/S
if all else fails, try using a packet sniffer to capture the network traffic.
UPDATE
... it seems i confused sftp with ftps ...
Yes, they are very different. SFTP is in effect FTP tunneled over an SSH connection. It requires a client-side library that understands SSH.
So if you are trying to use FTP / FTPS client-side libraries and settings to talk an SSH (SFTP) service, you are probably failing because port 21 is blocked (which it should be if there is no FTP service) or because no FTP service is running (which should be the default for a typical out-of-the-box Linux server).
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.
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.
hiho,
i intend to develop just a litte network application, something like a chat. so i downloaded xampp for windows and installed it (also as service), mysql included. well, i started the apache (and mysql) as service and just wrote the short line in java:
try {
Socket sock = new Socket("127.0.0.1", 21);
System.out.println("connection established");
} catch ( UnknownHostException e ) {
System.out.println("Can't find host.");
} catch ( IOException e ) {
System.out.println("Error connecting to host. " + e.toString());
}
but directly i got the answer:
Error connecting to host. java.net.ConnectException: Connection refused: connect
the server is runnin'. the localhost is accessible on the browser.
did i forgot something? any ideas?
In your java code you're trying to connect to port 21 (ftp). Don't know what you want to do, but perhaps you should try port 80 (http).
Did you download and install the Tomcat add-on for XAMPP? It's not part of the default install.
After it's extracted into the XAMPP folder you have to run first setup_xampp.bat and then tomcat_start.bat.
Also, Tomcat in XAMPP will use the 8080 port by default.