Search in sources :

Example 56 with DatagramSocket

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

the class SpanStreamUdpSender method createChannel.

private DatagramChannel createChannel(String host, int port, int timeout, int sendBufferSize) {
    DatagramChannel datagramChannel = null;
    DatagramSocket socket = null;
    try {
        datagramChannel = DatagramChannel.open();
        socket = datagramChannel.socket();
        socket.setSoTimeout(timeout);
        socket.setSendBufferSize(sendBufferSize);
        if (logger.isWarnEnabled()) {
            final int checkSendBufferSize = socket.getSendBufferSize();
            if (sendBufferSize != checkSendBufferSize) {
                logger.warn("DatagramChannel.setSendBufferSize() error. {}!={}", sendBufferSize, checkSendBufferSize);
            }
        }
        InetSocketAddress serverAddress = new InetSocketAddress(host, port);
        datagramChannel.connect(serverAddress);
        return datagramChannel;
    } catch (IOException e) {
        if (socket != null) {
            socket.close();
        }
        if (datagramChannel != null) {
            try {
                datagramChannel.close();
            } catch (IOException ignored) {
            }
        }
        throw new IllegalStateException("DatagramChannel create fail. Cause" + e.getMessage(), e);
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException)

Example 57 with DatagramSocket

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

the class NioUDPDataSender method createChannel.

private DatagramChannel createChannel(String host, int port, int timeout, int sendBufferSize) {
    DatagramChannel datagramChannel = null;
    DatagramSocket socket = null;
    try {
        datagramChannel = DatagramChannel.open();
        socket = datagramChannel.socket();
        socket.setSoTimeout(timeout);
        socket.setSendBufferSize(sendBufferSize);
        if (logger.isWarnEnabled()) {
            final int checkSendBufferSize = socket.getSendBufferSize();
            if (sendBufferSize != checkSendBufferSize) {
                logger.warn("DatagramChannel.setSendBufferSize() error. {}!={}", sendBufferSize, checkSendBufferSize);
            }
        }
        InetSocketAddress serverAddress = new InetSocketAddress(host, port);
        datagramChannel.connect(serverAddress);
        return datagramChannel;
    } catch (IOException e) {
        if (socket != null) {
            socket.close();
        }
        if (datagramChannel != null) {
            try {
                datagramChannel.close();
            } catch (IOException ignored) {
            }
        }
        throw new IllegalStateException("DatagramChannel create fail. Cause" + e.getMessage(), e);
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException)

Example 58 with DatagramSocket

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

the class UDPChecker method check.

@Override
protected boolean check(InetSocketAddress address, byte[] requestData, byte[] expectedResponseData) {
    DatagramSocket socket = null;
    try {
        socket = createSocket();
        write(socket, requestData, address);
        byte[] responseData = read(socket, expectedResponseData.length);
        return Arrays.equals(expectedResponseData, responseData);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (socket != null) {
            socket.close();
        }
    }
    return false;
}
Also used : DatagramSocket(java.net.DatagramSocket) IOException(java.io.IOException)

Example 59 with DatagramSocket

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

the class UDPChecker method createSocket.

private DatagramSocket createSocket(InetSocketAddress socketAddress) throws IOException {
    DatagramSocket socket = new DatagramSocket();
    socket.connect(socketAddress);
    socket.setSoTimeout(3000);
    return socket;
}
Also used : DatagramSocket(java.net.DatagramSocket)

Example 60 with DatagramSocket

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

the class TestUDPReceiver method start.

@PostConstruct
@Override
public void start() {
    logger.info("{} start.", receiverName);
    afterPropertiesSet();
    final DatagramSocket socket = this.socket;
    if (socket == null) {
        throw new IllegalStateException("socket is null.");
    }
    bindSocket(socket, bindAddress, port);
    logger.info("UDP Packet reader:{} started.", ioThreadSize);
    for (int i = 0; i < ioThreadSize; i++) {
        io.execute(new Runnable() {

            @Override
            public void run() {
                receive(socket);
            }
        });
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) PostConstruct(javax.annotation.PostConstruct)

Aggregations

DatagramSocket (java.net.DatagramSocket)294 DatagramPacket (java.net.DatagramPacket)137 IOException (java.io.IOException)112 SocketException (java.net.SocketException)74 InetAddress (java.net.InetAddress)63 InetSocketAddress (java.net.InetSocketAddress)46 UnknownHostException (java.net.UnknownHostException)33 Test (org.junit.Test)27 SocketTimeoutException (java.net.SocketTimeoutException)24 InterruptedIOException (java.io.InterruptedIOException)18 PortUnreachableException (java.net.PortUnreachableException)18 ServerSocket (java.net.ServerSocket)18 BindException (java.net.BindException)16 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)16 DatagramChannel (java.nio.channels.DatagramChannel)14 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 MulticastSocket (java.net.MulticastSocket)10 SocketAddress (java.net.SocketAddress)9 ByteBuffer (java.nio.ByteBuffer)8 ArrayList (java.util.ArrayList)6