java.net.ConnectException: Connection refused: connect java nio - java

**java.net.ConnectException: Connection refused: connect
at sun.nio.ch.Net.connect0(Native Method)
at sun.nio.ch.Net.connect(Net.java:454)
at sun.nio.ch.Net.connect(Net.java:446)
at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
I have this exception when i use socketchannel between two pcs on the same local network strong text .. but when i use on the same PC is connects and send bytes without any exception..any help?
the error happens when calling connect(socketAddress);
**socketChannel.connect(socketAddress);****

First check the connectivity between the two PCs by using ping from one machine using the IP
ping 192.168.x.x
If the ping works, double check the firewall settings of both the PCs

Related

How to deal with the ConnectException in this concrete Server/Client model? (Java)

I am playing with the code https://cs.lmu.edu/~ray/notes/javanetexamples/#chat at the moment.
In the line
var client = new ChatClient(args[0]);
of the clients' main method you pass a host name to the constructor.
I am still getting a "ConnectException: connection refused" trying to connect to my server as a client over the internet (with my public IP), whereas I can connect to it locally (with "localhost"). I have checked the firewall, all ports and the server itself, but still getting it. The ping to my IP works, but the connection is being refused.
Is it possible for me to connect as a client over the internet from my laptop to the server running at my laptop?
The trace:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:503)
at java.base/sun.nio.ch.Net.connect(Net.java:492)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
at java.base/java.net.Socket.connect(Socket.java:648)
at java.base/java.net.Socket.connect(Socket.java:597)
at java.base/java.net.Socket.<init>(Socket.java:520)
at java.base/java.net.Socket.<init>(Socket.java:294)
at ChatClient.run(ChatClient.java:71)
at ChatClient.main(ChatClient.java:97)
So, calling client.run() the exception occurs when trying to open a socket at the passed IP address at
var socket = new Socket(serverAddress, 50000);
and I use the port 50000 for both.
It is extremely unlikely that you would be able to connect directly to your computer from the internet without extra work.
Minimally you would need to do port forwarding in your router to redirect all connections to port 59001 to your machine.
In suggest using something like ngrok for this instead:
ngrok http 59001
and now you should be able to connect to your service
var socket = new Socket("http://CUSTOM_URL_PROVIDED_BY_NGROK.ngrok.io", 59001);

Connection refused when connecting to RabbitMQ via IP address instead of localhost

I'm trying to set up a RabbitMQ server (version 3.7.4, installed via homebrew on macOS 10.13.4) such that my colleagues can work with it via the internal network. We are using the official Java client (com.rabbitmq:amqp-client:5.2.0). Unfortunately, they only get a ConnectException with the message Connection refused:
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:134)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:997)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:956)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:914)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1068)
at my.own.private.RabbitMqConnector.connect(RabbitMqConnector.java:29)
We already set up another user account (not guest) which has admin privileges. With almost the same Java code I can connect locally (setting the host to localhost) but not if I use my IP address. According to the RabbitMQ docs, the RabbitMQ server binds to all network interfaces by default (otherwise I would have said that it's a problem with address binding). I disabled my firewall so that shouldn't be the problem.
Here's the respective code:
final ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setUsername("username");
connectionFactory.setPassword("password");
connectionFactory.setHost("10.10.33.12");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
try (final Connection connection = connectionFactory.newConnection()) {
// doo stuff with the connection
}
I finally found the actual problem and it's not related to Java or how I configured the ConnectionFactory. It was indeed a problem how the RabbitMQ package for homebrew is configured. In the rabbitmq-env.conf file, there was the following setting
NODE_IP_ADDRESS=127.0.0.1
This setting caused RabbitMQ to only listen on 127.0.0.1 for incoming connections. Once I removed the setting and restarted the service, I could connect from other machines as well.
Thank you all for your help!
The issue is not with your code. I would suggest you to check such things:
RabbitMQ Server is up and running
Your custom user can access virtual host "/"
Check firewall settings if any, probably it's refusing connection with remote host
One of these should be the cause, hope it helps!

Android Socket Connection Refused ETIMEDOUT (Connection timed out)

I have created a server on my PC (localhost and port is 8080) and am trying to connect to it on my android device. But every time i try it gives me this error:
java.net.ConnectException: failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
I have been looking online forever and looked at countless pages and have tried everything I could find.
-Switched the android host to 10.0.2.2
-Turned off firewall
-Changed the port
-Made sure both the PC and the android device where connected to the same internet connection
The Server code is written in Java and, just to make sure it works I tried making a connection to the server with another Java app. And it worked.
Thank you for your help, if you need any more info just let me know.
Do not use 10.0.2.2. That is for if your app runs on an emulator.
Use the LAN ip of your pc.
Find out with ipconfig.

java.net.ConnectException: failed to connect to /192.168.10.110 connect failed: ECONNREFUSED (Connection refused

First problem:
I am doing messaging using socket programming.Previously it worked fine but currently it is showing exception when I connect both server and client to same router .
Second problem:
If I connect server and client to different networks it is not working client unable to connect server's ip address
192.168.0.0/16 is a Local Area Network address block.
Moreover, if your server machine is configured to use DHCP instead of a static address, it may receive a different local address each time you connect it to the (same) router.

java RMI connection to server

I have a very simple rmi client / server application. I don't use the "rmiregistry" application though, I use this to create the server:
server = new RemoteServer();
registry = LocateRegistry.createRegistry(PORT);
registry.bind("RemoteServer", server);
The client part is:
registry = LocateRegistry.getRegistry(IPADDRESS, PORT);
remote = (IRemoteServer) registry.lookup("RemoteServer");
Here is the fascinating problem: The application works perfectly when both server and client are running in my (private) local network. As soon as I put the server on a public server, the application hangs for a minute, then gives me the following error:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.ConnectException: Connection refused to host: 192.168.x.y; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source ... (the rest is truncated)
The key I think is that the client (running on my private network) cannot connect to myself (my address is 192.168.x.y where x.y is some other numbers, but the real error message shows my ip address listed there)
If I kill the rmi server on the public internet, then I instantly get a "connection refused to host: a.b.c.d") message, so I know that something at the server end is at least working.
Any suggestions?
EDIT: just to make this a little more clear: 192.168.x.y is the client address, a.b.c.d is the server address. The stacktrace shows the client cannot connect to the client.
Try running the server with this parameter for the virtual machine:
-Djava.rmi.server.hostname=hostname_of_the_server
192.168.* contains private IP addresses (as does 10.*). These will not be routed on the open internet. You need to make sure that you are using an public IP address for the server, and any firewalls (including NAT) are configured for access. You can do a quick check with telnet on the required port.
I would believe that the server tries to open a socket back to the client and it fails, and the exception is a bit unclear on its wording.
RMI is not very NAT-friendly, IIRC.

Categories