java.net.ConnectException:调用SocketChannel.open时拒绝连接

时间:2022-09-05 15:08:14

I am trying to write a simple program to open a socket channel to a local address. I get a connection refused exception whenever I run this program

我正在尝试编写一个简单的程序来打开到本地地址的套接字通道。每当我运行这个程序时,我都会收到连接拒绝异常

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

public class testSocket {

        public static void main(String [] args) {
                try {
                        InetAddress addr = InetAddress.getByName("localhost");
                        InetSocketAddress remoteAddress = new InetSocketAddress(addr, 19015);

                        // Open a new Socket channel and set it to non-blocking
                        SocketChannel socketChannel = SocketChannel.open();
                        socketChannel.configureBlocking(false);

                        // Issue the Connect call on the remote address.
                        socketChannel.connect(remoteAddress);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}

The exception that I get is

我得到的例外是

java.net.ConnectException: Connection refused
        at sun.nio.ch.Net.connect(Native Method)
        at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:464)
        at testSocket.main(testSocket.java:17)

I encounter this issue with Sun Solaris and HP - UX. It seems to work fine on a Linux machine. Can anyone let me know why the connection is being refused? I did a netstat -a and confirmed that the port is not in use.

我在Sun Solaris和HP-UX中遇到了这个问题。它似乎在Linux机器上运行良好。任何人都可以让我知道为什么连接被拒绝?我做了一个netstat -a并确认该端口未被使用。

Thanks in advance!

提前致谢!

2 个解决方案

#1


From the Javadoc for SocketChannel.connect()

来自Javadoc for SocketChannel.connect()

If this channel is in non-blocking mode then an invocation of this method initiates a non-blocking connection operation. If the connection is established immediately, as can happen with a local connection, then this method returns true. Otherwise this method returns false and the connection operation must later be completed by invoking the finishConnect method.

如果此通道处于非阻塞模式,则调用此方法将启动非阻塞连接操作。如果立即建立连接,就像本地连接可能发生的那样,则此方法返回true。否则,此方法返回false,稍后必须通过调用finishConnect方法完成连接操作。

When I run your code on Linux, connect() returns false hence there is no exception. If you add a call to socketChannel.finishConnect() you will see the same connection refused exception as you get on Solaris/HP-UX.

当我在Linux上运行你的代码时,connect()返回false,因此没有异常。如果添加对socketChannel.finishConnect()的调用,您将看到与Solaris / HP-UX上相同的连接拒绝异常。

I suspect that on Solaris/HP-UX connect() returns true hence the exception is thrown immediately.

我怀疑在Solaris / HP-UX上connect()返回true,因此会立即抛出异常。

#2


The "Connection refused" message is what you'll receive when there is no process listening on your specified port (19015). It looks like you're trying to connect to a service that isn't there. netstat is even telling you that the port isn't in use!

当您的指定端口没有进程监听时,您将收到“连接被拒绝”消息(19015)。看起来您正在尝试连接到不存在的服务。 netstat甚至告诉你端口没有被使用!

#1


From the Javadoc for SocketChannel.connect()

来自Javadoc for SocketChannel.connect()

If this channel is in non-blocking mode then an invocation of this method initiates a non-blocking connection operation. If the connection is established immediately, as can happen with a local connection, then this method returns true. Otherwise this method returns false and the connection operation must later be completed by invoking the finishConnect method.

如果此通道处于非阻塞模式,则调用此方法将启动非阻塞连接操作。如果立即建立连接,就像本地连接可能发生的那样,则此方法返回true。否则,此方法返回false,稍后必须通过调用finishConnect方法完成连接操作。

When I run your code on Linux, connect() returns false hence there is no exception. If you add a call to socketChannel.finishConnect() you will see the same connection refused exception as you get on Solaris/HP-UX.

当我在Linux上运行你的代码时,connect()返回false,因此没有异常。如果添加对socketChannel.finishConnect()的调用,您将看到与Solaris / HP-UX上相同的连接拒绝异常。

I suspect that on Solaris/HP-UX connect() returns true hence the exception is thrown immediately.

我怀疑在Solaris / HP-UX上connect()返回true,因此会立即抛出异常。

#2


The "Connection refused" message is what you'll receive when there is no process listening on your specified port (19015). It looks like you're trying to connect to a service that isn't there. netstat is even telling you that the port isn't in use!

当您的指定端口没有进程监听时,您将收到“连接被拒绝”消息(19015)。看起来您正在尝试连接到不存在的服务。 netstat甚至告诉你端口没有被使用!