-2

If another application on the PC is connected to the same remote IP address, a java application will fail to connect properly.

This can also happen when a exits abruptly without closing the socket channel. The connection can be blocked and it is impossible to connect during a subsequent session.

What can I do to ensure that no matter the state of the connection in the underlying OS, my program will connect 100% of the time ?

I am looking for a cross platform solution (Windows & Ubuntu)

public void connect() throws CommunicationIOException {

    try {

        if (isConnected()) {
            return;
        }

        socket = SocketChannel.open();
        socket.socket().connect(new InetSocketAddress(getHostname(), getPort()), getConnectionTimeout());

        if (!isConnected()) {
            throw new CommunicationIOException("Failed to establish the connection");
        }

        socket.configureBlocking(false);

    } catch (final IOException ex) {

        throw new CommunicationIOException(
                "An error occurred while connecting to " + getHostname() + " on port " + getPort(), ex);
    }

}

.

public boolean isConnected() {
    if (socket == null) {
        return false;
    } else {
        return socket.isConnected();
    }
}

.

public void close() throws CommunicationIOException {

    if (socket != null) {

        try {

            socket.close();

        } catch (final IOException ex) {

            throw new CommunicationIOException(
                    MessageFormat.format(
                            "An error occurred while attempting to close the connection to {}:{}",
                            getHostname(), getPort()), ex);
        }

    }
}
klonq
  • 3,535
  • 4
  • 36
  • 58

1 Answers1

0

If another application on the PC is connected to the same remote IP address, a java application will fail to connect properly.

No it won't, unless the server is improperly programmed.

This can also happen when a exits abruptly without closing the socket channel.

No it can't, again unless something is improperly programmed.

The connection can be blocked

No it can't.

and it is impossible to connect during a subsequent session.

No it isn't.

What can I do to ensure that no matter the state of the connection in the underlying OS, my program will connect 100% of the time ?

Nothing in this life will give you a 100% guarantee. However your fears as expressed above are baseless.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • In fact you are wrong : One of the two following exceptions will occur – klonq Nov 02 '17 at 10:10
  • Caused by: java.net.ConnectException: Connection refused: no further information at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:111) at sotec.automation.io.conn.tcp.TcpConnection.connect(TcpConnection.java:96) – klonq Nov 02 '17 at 10:10
  • Caused by: java.nio.channels.ClosedChannelException: null at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:698) at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:111) at sotec.automation.io.conn.tcp.TcpConnection.connect(TcpConnection.java:96) – klonq Nov 02 '17 at 10:10
  • @klong The 'connection refused' case is covered by 'unless the server is improperly programmed', specifically if the server has closed the listening socket. The 'closed channel' exception is unrelated, and is caused by, err, closing the channel at the client during the connect phase, which is another piece of improper programming. You seem to have some very curious code in your application. I suggest you post it for comment, instead of just posting baseless speculation. – user207421 Nov 02 '17 at 10:11
  • Which server are you refering to ? – klonq Nov 02 '17 at 10:12
  • I am referring to the server you are trying to connect to. What other server could I possibly be referring to? You aren't making much sense here. – user207421 Nov 02 '17 at 10:13
  • If I restart my local PC it works again (without restarting remote server) – klonq Nov 02 '17 at 10:13
  • 'It' being ***what*** exactly? I'm not interested in discussing buggy programming that I can't even see. – user207421 Nov 02 '17 at 10:14
  • The connection works again - what else could I be refering to ? Do you want to provide anything to support your point of view ? – klonq Nov 02 '17 at 10:16
  • Do you want to provide your code so we can review it for ourselves? instead of having to guess what is wrong with it? My 'point of view' is supported by 30 years of experience, writing two books, and by RFC 793, 1122, 1123, and successors. Where is *your* support? *Your* evidence? – user207421 Nov 02 '17 at 10:17
  • There is no server code, its a Moxa MGate if that helps (https://www.moxa.com/product/MGate_MB3180_3280_3480.htm) – klonq Nov 02 '17 at 10:27