Hi, 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.
Thanks in advance!