我想写一个简单的程序来打开一个套接字通道到本地地址。我得到一个连接被拒绝异常,每当我运行这个程序java.net.ConnectException:当SocketChannel.open被调用时连接被拒绝
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();
}
}
}
,我得到的是
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)
我遇到与Sun Solaris和HP这个问题的例外 - UX。它似乎在Linux机器上正常工作。任何人都可以让我知道为什么连接被拒绝?我做了一个netstat -a并确认该端口未被使用。
在此先感谢!