Search in sources :

Example 11 with PortUnreachableException

use of java.net.PortUnreachableException in project netty by netty.

the class DatagramConnectNotExistsTest method testConnectNotExists.

public void testConnectNotExists(Bootstrap cb) {
    // Currently not works on windows
    // See https://github.com/netty/netty/issues/11285
    assumeFalse(PlatformDependent.isWindows());
    final Promise<Throwable> promise = ImmediateEventExecutor.INSTANCE.newPromise();
    cb.handler(new ChannelInboundHandlerAdapter() {

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            promise.trySuccess(cause);
        }
    });
    ChannelFuture future = cb.connect(NetUtil.LOCALHOST, SocketTestPermutation.BAD_PORT);
    try {
        Channel datagramChannel = future.syncUninterruptibly().channel();
        assertTrue(datagramChannel.isActive());
        datagramChannel.writeAndFlush(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)).syncUninterruptibly();
        if (!(datagramChannel instanceof OioDatagramChannel)) {
            assertTrue(promise.syncUninterruptibly().getNow() instanceof PortUnreachableException);
        }
    } finally {
        future.channel().close();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PortUnreachableException(java.net.PortUnreachableException) OioDatagramChannel(io.netty.channel.socket.oio.OioDatagramChannel) Channel(io.netty.channel.Channel) OioDatagramChannel(io.netty.channel.socket.oio.OioDatagramChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 12 with PortUnreachableException

use of java.net.PortUnreachableException in project netty by netty.

the class EpollDatagramChannel method translateForConnected.

private IOException translateForConnected(NativeIoException e) {
    // We need to correctly translate connect errors to match NIO behaviour.
    if (e.expectedErr() == Errors.ERROR_ECONNREFUSED_NEGATIVE) {
        PortUnreachableException error = new PortUnreachableException(e.getMessage());
        error.initCause(e);
        return error;
    }
    return e;
}
Also used : PortUnreachableException(java.net.PortUnreachableException)

Example 13 with PortUnreachableException

use of java.net.PortUnreachableException in project mc-dev by Bukkit.

the class RemoteStatusListener method run.

public void run() {
    this.info("Query running on " + this.motd + ":" + this.bindPort);
    this.clearedTime = MinecraftServer.ar();
    this.p = new DatagramPacket(this.o, this.o.length);
    try {
        while (this.running) {
            try {
                this.socket.receive(this.p);
                this.cleanChallenges();
                this.parsePacket(this.p);
            } catch (SocketTimeoutException sockettimeoutexception) {
                this.cleanChallenges();
            } catch (PortUnreachableException portunreachableexception) {
                ;
            } catch (IOException ioexception) {
                this.a(ioexception);
            }
        }
    } finally {
        this.e();
    }
}
Also used : PortUnreachableException(java.net.PortUnreachableException) SocketTimeoutException(java.net.SocketTimeoutException) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException)

Example 14 with PortUnreachableException

use of java.net.PortUnreachableException in project Aeron by real-logic.

the class UdpChannelTransport method receive.

/**
 * Receive a datagram from the media layer.
 *
 * @param buffer into which the datagram will be received.
 * @return the source address of the datagram if one is available otherwise false.
 */
public InetSocketAddress receive(final ByteBuffer buffer) {
    buffer.clear();
    InetSocketAddress address = null;
    try {
        if (receiveDatagramChannel.isOpen()) {
            address = (InetSocketAddress) receiveDatagramChannel.receive(buffer);
        }
    } catch (final PortUnreachableException ignored) {
    } catch (final Exception ex) {
        LangUtil.rethrowUnchecked(ex);
    }
    return address;
}
Also used : PortUnreachableException(java.net.PortUnreachableException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) AeronException(io.aeron.exceptions.AeronException) PortUnreachableException(java.net.PortUnreachableException)

Example 15 with PortUnreachableException

use of java.net.PortUnreachableException in project helios by spotify.

the class DefaultProber method probeUdpPort.

private boolean probeUdpPort(String host, PortMapping portMapping) {
    final Integer port = portMapping.getExternalPort();
    try {
        // Let's send a PING
        // A UDP service should ignore any messages that do not conform to its protocol
        // If it does then you probably should implement your own Prober or
        // skip the probing by using a Dummy prober
        final byte[] pingData = "PING".getBytes("UTF-8");
        // Use ephemeral port number
        final DatagramSocket serverSocket = new DatagramSocket(0);
        final SocketAddress socketAddr = new InetSocketAddress(host, port);
        serverSocket.connect(socketAddr);
        final InetAddress address = InetAddress.getByName(host);
        final DatagramPacket sendPacket = new DatagramPacket(pingData, pingData.length, address, port);
        serverSocket.send(sendPacket);
        // Wait for a response: This will cause either a timeout (OK) or a port not reachable (NOT OK)
        final byte[] receiveData = new byte[8];
        final DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.setSoTimeout(200);
        serverSocket.receive(receivePacket);
    } catch (SocketTimeoutException e) {
        // OK we got a timeout. That means that the UDP port is up and listening
        return true;
    } catch (PortUnreachableException e) {
        // there is no-one listening to the UDP port
        return false;
    } catch (SocketException e) {
        LOG.warn(e.getMessage(), e);
        return false;
    } catch (IOException e) {
        LOG.warn(e.getMessage(), e);
        return false;
    }
    return false;
}
Also used : SocketException(java.net.SocketException) PortUnreachableException(java.net.PortUnreachableException) SocketTimeoutException(java.net.SocketTimeoutException) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Aggregations

PortUnreachableException (java.net.PortUnreachableException)16 IOException (java.io.IOException)13 InterruptedIOException (java.io.InterruptedIOException)7 SocketTimeoutException (java.net.SocketTimeoutException)7 DatagramPacket (java.net.DatagramPacket)6 SocketException (java.net.SocketException)6 InetSocketAddress (java.net.InetSocketAddress)5 UnknownHostException (java.net.UnknownHostException)5 DatagramSocket (java.net.DatagramSocket)4 InetAddress (java.net.InetAddress)4 EOFException (java.io.EOFException)3 MalformedURLException (java.net.MalformedURLException)3 ByteBuffer (java.nio.ByteBuffer)3 CertPathValidatorException (java.security.cert.CertPathValidatorException)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 SSLException (javax.net.ssl.SSLException)3 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)3 Test (org.junit.Test)3 BindException (java.net.BindException)2 NoRouteToHostException (java.net.NoRouteToHostException)2