Search in sources :

Example 91 with SocketException

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

the class OioDatagramChannel method doReadMessages.

@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannelConfig config = config();
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf data = config.getAllocator().heapBuffer(allocHandle.guess());
    boolean free = true;
    try {
        tmpPacket.setData(data.array(), data.arrayOffset(), data.capacity());
        socket.receive(tmpPacket);
        InetSocketAddress remoteAddr = (InetSocketAddress) tmpPacket.getSocketAddress();
        allocHandle.lastBytesRead(tmpPacket.getLength());
        buf.add(new DatagramPacket(data.writerIndex(allocHandle.lastBytesRead()), localAddress(), remoteAddr));
        free = false;
        return 1;
    } catch (SocketTimeoutException e) {
        // Expected
        return 0;
    } catch (SocketException e) {
        if (!e.getMessage().toLowerCase(Locale.US).contains("socket closed")) {
            throw e;
        }
        return -1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            data.release();
        }
    }
}
Also used : RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) DatagramChannelConfig(io.netty.channel.socket.DatagramChannelConfig) DefaultDatagramChannelConfig(io.netty.channel.socket.DefaultDatagramChannelConfig) ByteBuf(io.netty.buffer.ByteBuf)

Example 92 with SocketException

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

the class BootstrapTest method testLateRegisterSuccessBindFailed.

@Test
public void testLateRegisterSuccessBindFailed() throws Exception {
    TestEventLoopGroup group = new TestEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(group);
        bootstrap.channelFactory(new ChannelFactory<ServerChannel>() {

            @Override
            public ServerChannel newChannel() {
                return new LocalServerChannel() {

                    @Override
                    public ChannelFuture bind(SocketAddress localAddress) {
                        // Close the Channel to emulate what NIO and others impl do on bind failure
                        // See https://github.com/netty/netty/issues/2586
                        close();
                        return newFailedFuture(new SocketException());
                    }

                    @Override
                    public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) {
                        // Close the Channel to emulate what NIO and others impl do on bind failure
                        // See https://github.com/netty/netty/issues/2586
                        close();
                        return promise.setFailure(new SocketException());
                    }
                };
            }
        });
        bootstrap.childHandler(new DummyHandler());
        bootstrap.localAddress(new LocalAddress("1"));
        ChannelFuture future = bootstrap.bind();
        assertFalse(future.isDone());
        group.promise.setSuccess();
        final BlockingQueue<Boolean> queue = new LinkedBlockingQueue<Boolean>();
        future.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                queue.add(future.channel().eventLoop().inEventLoop(Thread.currentThread()));
                queue.add(future.isSuccess());
            }
        });
        assertTrue(queue.take());
        assertFalse(queue.take());
    } finally {
        group.shutdownGracefully();
        group.terminationFuture().sync();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketException(java.net.SocketException) LocalAddress(io.netty.channel.local.LocalAddress) ChannelPromise(io.netty.channel.ChannelPromise) LocalServerChannel(io.netty.channel.local.LocalServerChannel) ServerChannel(io.netty.channel.ServerChannel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ChannelFutureListener(io.netty.channel.ChannelFutureListener) SocketException(java.net.SocketException) ConnectException(java.net.ConnectException) UnknownHostException(java.net.UnknownHostException) LocalServerChannel(io.netty.channel.local.LocalServerChannel) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 93 with SocketException

use of java.net.SocketException in project pinpoint by naver.

the class UDPReceiver method bindSocket.

private void bindSocket(DatagramSocket socket, String bindAddress, int port) {
    if (socket == null) {
        throw new NullPointerException("socket must not be null");
    }
    try {
        logger.info("DatagramSocket.bind() {}/{}", bindAddress, port);
        socket.bind(new InetSocketAddress(bindAddress, port));
    } catch (SocketException ex) {
        throw new IllegalStateException("Socket bind Fail. port:" + port + " Caused:" + ex.getMessage(), ex);
    }
}
Also used : SocketException(java.net.SocketException) InetSocketAddress(java.net.InetSocketAddress)

Example 94 with SocketException

use of java.net.SocketException in project pinpoint by naver.

the class TestUDPReceiver method createSocket.

private DatagramSocket createSocket(int receiveBufferSize) {
    try {
        DatagramSocket socket = new DatagramSocket(null);
        socket.setReceiveBufferSize(receiveBufferSize);
        if (logger.isWarnEnabled()) {
            final int checkReceiveBufferSize = socket.getReceiveBufferSize();
            if (receiveBufferSize != checkReceiveBufferSize) {
                logger.warn("DatagramSocket.setReceiveBufferSize() error. {}!={}", receiveBufferSize, checkReceiveBufferSize);
            }
        }
        socket.setSoTimeout(1000 * 5);
        return socket;
    } catch (SocketException ex) {
        throw new RuntimeException("Socket create Fail. Caused:" + ex.getMessage(), ex);
    }
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket)

Example 95 with SocketException

use of java.net.SocketException in project webmagic by code4craft.

the class ProxyUtils method getNetworkInterface.

private static String getNetworkInterface() {
    String networkInterfaceName = ">>>> modify networkInterface in us.codecraft.webmagic.utils.ProxyUtils";
    Enumeration<NetworkInterface> enumeration = null;
    try {
        enumeration = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        e1.printStackTrace();
    }
    while (enumeration.hasMoreElements()) {
        NetworkInterface networkInterface = enumeration.nextElement();
        Enumeration<InetAddress> addr = networkInterface.getInetAddresses();
        while (addr.hasMoreElements()) {
            String s = addr.nextElement().getHostAddress();
            Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
            if (s != null && IPV4_PATTERN.matcher(s).matches()) {
                networkInterfaceName += networkInterface.toString() + "IP:" + s + "\n\n";
            }
        }
    }
    return networkInterfaceName;
}
Also used : SocketException(java.net.SocketException) Pattern(java.util.regex.Pattern) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Aggregations

SocketException (java.net.SocketException)523 IOException (java.io.IOException)189 InetAddress (java.net.InetAddress)127 NetworkInterface (java.net.NetworkInterface)110 Socket (java.net.Socket)100 UnknownHostException (java.net.UnknownHostException)93 ServerSocket (java.net.ServerSocket)82 DatagramSocket (java.net.DatagramSocket)78 SocketTimeoutException (java.net.SocketTimeoutException)77 InetSocketAddress (java.net.InetSocketAddress)62 Test (org.junit.Test)50 ConnectException (java.net.ConnectException)39 DatagramPacket (java.net.DatagramPacket)38 ArrayList (java.util.ArrayList)35 BindException (java.net.BindException)31 Inet4Address (java.net.Inet4Address)24 SocketAddress (java.net.SocketAddress)24 InterruptedIOException (java.io.InterruptedIOException)23 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)23 SmallTest (android.test.suitebuilder.annotation.SmallTest)20