我想寫一個簡單的程序來打開一個套接字通道到本地地址。我得到一個連接被拒絕異常,每當我運行這個程序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並確認該端口未被使用。
在此先感謝!