I'm trying to set up a simple test FTPS server in Java using Apache FtpServer and connect to it using a domain name instead of the IP address.
I've pointed the A record to the IP address and set up the SSL certificate. Based on the Apache FtpServer documentation, here is what my code looks like so far:
FtpServerFactory ftpServerFactory = new FtpServerFactory();
ListenerFactory listenerFactory = new ListenerFactory();
listenerFactory.setPort(990);
listenerFactory.setServerAddress("example.com");
SslConfigurationFactory sslConfigurationFactory = new SslConfigurationFactory();
sslConfigurationFactory.setKeystoreFile(JKS);
sslConfigurationFactory.setKeystorePassword(JKS_PASS);
listenerFactory.setSslConfiguration(sslConfigurationFactory.createSslConfiguration());
listenerFactory.setImplicitSsl(true);
ftpServerFactory.addListener("default", listenerFactory.createListener());
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setFile(USERS_PATH.toFile());
BaseUser test = new BaseUser();
sample1.setName("test");
sample1.setPassword("test");
sample1.setHomeDirectory(HOME.getAbsolutePath().toString());
test.setAuthorities(List.of(new WritePermission());
UserManager userManager = userManagerFactory.createUserManager();
try {
userManager.save(test);
}
catch (FtpException e) {
e.printStackTrace();
}
ftpServerFactory.setUserManager(userManager);
FtpServer server = ftpServerFactory.createServer();
try {
server.start();
}
catch (FtpException e) {
e.printStackTrace();
}
However, when I try to connect to the FTPS server, I get an ECONNREFUSED - Connection refused by server from my FTPS client.
Are there any steps that I missed?
If your client reports a 'connection refused' that usually indicates (no guarantee) that no firewall prevented the TCP traffic, the connection request ended up on the intended machine but nothing was accepting the connection on the port you tried to connect to.
Things you can check:
Was the server process running? Was the server process on the correct port? Did the client connect to the correct port?
You might try to connect with another client (e.g. curl) just to see whether the TCP connection can be established.
You might try to connect to another port (e.g. 22 / ssh) to see if the client can establish the connection.
Related
I am trying to use RabbitMQ for posting messages from one application and receiving them in another.
I am able to post messages using localhost.
ex: amqp://guest:guest#localhost:5672
when i trying to post messages using different Ip getting below mention exception.
com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED-Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
sample code:
private Connection getConnection(){
Connection connection = null;
try {
ConnectionFactory factory = new ConnectionFactory();
final URI uri = URI.create(PropertyReader.read("rabbit.mq.uri").trim());
factory.setConnectionTimeout(30000);
factory.setAutomaticRecoveryEnabled(true);
factory.setTopologyRecoveryEnabled(true);
factory.setNetworkRecoveryInterval(10000);
factory.setExceptionHandler(new DefaultExceptionHandler());
factory.setRequestedHeartbeat(360);
factory.setUri(uri);
connection = factory.newConnection();
LOGGER.info("Rabbit MQ Connection established successfully");
} catch (Exception e) {
LOGGER.error("Error { }"+e);
}
return connection;
}
I need help for this.
using telnet i checked provided Ip.
The guest user can only connect to localhost by default: see the documentation.
"guest" user can only connect via localhost
By default, the guest user is prohibited from connecting to the broker remotely; it can only connect over a loopback interface (i.e. localhost). This applies both to AMQP 0-9-1 and to any other protocols enabled via plugins. Any other users you create will not (by default) be restricted in this way.
This is configured via the loopback_users item in the configuration file.
If you wish to allow the guest user to connect from a remote host, you should set the loopback_users configuration to none.
...
Add the user / pass combination to the factory:
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("user_name");
factory.setPassword("super_secret_pass");
I need to make a server and client that connects to the server.
Problem: "the server works. the client can only connect to localhost, it cannot connect to a server on the internet. I want the client to connect to the server, via a public ip-address that the server is hosted on."
First of all, I have made sure that the port is forwarded and reachable i have tested the port, secondly i have disabled firewall completely from the server machine.
below is the test code i am using:
The Server: nothing fancy just simple - terminates if a client is connected, else just awaits a connection.
public class Server {
public static void main(String args[]) {
try {
ServerSocket srvr = new ServerSocket(52000);
srvr.accept();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
The Client: I have used no-ip.com to mask the ip of the server to "biogenserver2.noip.me".
Using .getCanonicalHostName(); will return the ip.
public class Client {
public static void main(String args[]) {
try {
String ip = Inet4Address.getByName("somets.noip.com").getCanonicalHostName();
InetSocketAddress sa = new InetSocketAddress(ip, 52000);
//Socket skt = new Socket("0.0.0.0", 52000); //local - this works fine.
Socket skt = new Socket();
skt.connect(sa);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
When i run this the server connects fine, but the client returns a "connection timeout" exception
Any help will be appreciated. Thanks.
Answer:
"Just for clarity: You have checked the port is open via public IP as returned by no-ip and the server will quit without exception when you run that little testclient (on a machine that is not the server machine) - is that correct?" – Fildor
TL:DR
Don't run the client and server on the same machine and the same network trying to connect to your server through your public ip then to your own local network will result in a client timeout exception
I was running the client and server on the same machine and also the same network. This caused the client timeout exception. I tried running the Client on a different machine and a different network and i was able to connect successfully.
What version of IP protocol your application uses? On linux, you may figure it out with netstat -tunap | grep 52000 and watching whether first field is tcp or tcp6. If latter, then it is possible that problem with IPv6 connectivity exists and you may want to prefer using IPv4 to IPv6 by specifying -Djava.net.preferIPv4Stack=true to JVM.
I am using Java to do the socket programming as below.
Client program is as below:
Socket MyClient;
try {
MyClient = new Socket("Machine name", PortNumber);
}
catch (IOException e) {
System.out.println(e);
}
Server program is as below:
ServerSocket MyService;
try {
MyServerice = new ServerSocket(PortNumber);
}
catch (IOException e) {
System.out.println(e);
}
Socket clientSocket = null;
try {
clientSocket = MyService.accept();
}
catch (IOException e) {
System.out.println(e);
}
Now my question is if I run more than one thread to open several sockets in one port (as the server code above), how my client program know which socket it is connecting to?
Your client connects to the Servers port. So all clients will be having the same code
MyClient = new Socket("Machine name", <port where server is listening>);The port opened at client side is not important. The client will get a free port available in his OS.
how my client program know which socket it is connecting to?
The question doesn't make sense. It doesn't 'connect to a socket' at all, it connects to a listening port, and there is only one of those. Your server only accepts one client, so the second and subsequent threads will get an undefined behaviour ranging from a ConnectException to a ConnectionException to nothing, most probably the latter.
Your application knows it because you set it up with a specific port. There is no "auto discovery" built into TCP/IP, it's up to you to pick a server-port and make sure you set your clients up to connect to that port. Either you hard-code this into your client application or, better yet, have it in some configuration file you include with the client.
This is why you have a bunch of "known ports", like http is port 80. This means that a browser will always connect to port 80 on a web-server, unless you explicitly indicate another port in the URL.
I'm getting following error when my client tries to connect to my server socket:
java.net.ConnectException: Connection refused: connect
But, my server is really running, on the same machine. I try to connect to it by using the external IP of my router. But when I try to connect with "localhost", it works. And, yes I did port forwarding correcly in my router. Even canyouseeme.org can connect to my server (The site says: "success" and in my server-log appears that someone connected with the server.)
So, is it for one or another reason impossible to connect to the same machine (or to a machine in the same network) via an external IP? Or is this something typical for Windows? (Normally, I use Linux)
I also tried to completely disable Windows Firewall.
ServerSocket:
public ServerSocket ssocket;
public List<ClientHandler> handlers;
public Server(int port) { // Constructor
try {
ssocket = new ServerSocket(port);
this.handlers = new ArrayList<ClientHandler>();
IpSharingManager.uploadData(Utilities.getPublicIp(), port);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
Client:
public InvisibleClient(String host, int port) {
try {
System.out.println("Trying to connect to " + host + ":" + port);
this.host = host;
this.socket = new Socket(host, port);
this.bis = new BufferedInputStream(this.socket.getInputStream());
this.bos = new BufferedOutputStream(this.socket.getOutputStream());
this.console = new RemoteConsole(this.socket);
initializeCommunication();
System.out.println("Successfully connected!");
new Thread(this, "Client Thread").start();
} catch (Exception e) {
e.printStackTrace();
System.out.println("No server available");
}
}
Thanks
Some routers doesn't allow the internal network to connect to the external IP address of the router.
You can try to use telnet to connect to your server socket. If telnet isn't able to establish a connection, it's likely a networking problem.
Add the java.exe process and the port to your firewall exception list?
edit: Just read you already tried that. All I can suggest is make sure the network is not blocking that port. (routers)
Have You tried running it with JVM option: java.net.preferIPv4Stack=true ?
For what I see in your code, you missed the part where you accept the conection, after instantiating the server socket you need ssocket.accept() to accept conections and then you have to start reading the outputstrem from the socket
I'm building a simple server application, and I can connect to it in the Local Network. But I can't connect over the internet.
This is my Server code:
ServerSocket server;
try {
server = new ServerSocket(4000);
} catch(IOException ex) {
System.out.printf("Could not bind socket 4000\n");
System.exit(1);
}
try {
Socket socket = server.accept();
ClientThread client = new ClientThread(socket);
client.start();
} catch(IOException ex) {
}
And this is the client:
try {
System.out.printf("connecting...\n");
Socket socket = new Socket("mydomain.org", 4000);
System.out.printf("connected!\n");
} catch(UnknownHostException ex) {
ex.printStackTrace();
} catch(IOException ex) {
ex.printStackTrace();
}
I've forwarded port 4000 on my router, which should work. I've forwarded other ports before like 80 and 22.
When I run the client, I get the "connecting..." string, and it hangs there. I don't get "connected!", or a stack trace. But like I said before, it does work on the local network. It works when connecting to 127.0.0.1 and when using 192.168.1.90.
I used CanYouSeeMe.org to check if the port was open. It was successful on port 80, but it times out on 4000.
Check which ip address port 4000 is bound to. It may only be bound to the loopback address (127.0.0.1) instead of any interface.
Not sure which os you're using, but to check:
linux: netstat -ant
windows and mac: netstat -anp tcp
look for the LISTEN line on port 4000 and see whether it's bound to all interfaces (*:4000 or 0.0.0.0:4000), or to a specific interface (127.0.0.1:4000).
If it's not listening on all interfaces, that's your issue - use the constructor that lets you specify the BindAddress.
You've done the experiments to prove that it is not your java and must therefore be a firewall or firewalls issue. Even if you are properly forwarding the ports to your server, the server firewall may not be allowing inbound connections, so check that also.
You have a firewall rule that's throwing away the incoming packets. That's why your client hangs. Check the IP rules on the server computer and on any gateway system to the outside world.