Search in sources :

Example 61 with DatagramSocket

use of java.net.DatagramSocket 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 62 with DatagramSocket

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

the class UDPReceiverTest method socketBufferSize.

@Test
public void socketBufferSize() throws SocketException {
    DatagramSocket datagramSocket = new DatagramSocket();
    int receiveBufferSize = datagramSocket.getReceiveBufferSize();
    logger.debug("{}", receiveBufferSize);
    datagramSocket.setReceiveBufferSize(64 * 1024 * 10);
    logger.debug("{}", datagramSocket.getReceiveBufferSize());
    datagramSocket.close();
}
Also used : DatagramSocket(java.net.DatagramSocket) Test(org.junit.Test)

Example 63 with DatagramSocket

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

the class UDPReceiverTest method sendSocketBufferSize.

@Test
public void sendSocketBufferSize() throws IOException {
    DatagramPacket datagramPacket = new DatagramPacket(new byte[0], 0, 0);
    DatagramSocket datagramSocket = new DatagramSocket();
    datagramSocket.connect(new InetSocketAddress("127.0.0.1", 9995));
    datagramSocket.send(datagramPacket);
    datagramSocket.close();
}
Also used : DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(java.net.DatagramPacket) Test(org.junit.Test)

Example 64 with DatagramSocket

use of java.net.DatagramSocket in project openhab1-addons by openhab.

the class PifaceBinding method sendCommand.

private byte sendCommand(PifaceNode node, byte command, byte commandAck, int pinNumber, int pinValue, int attempt) throws ErrorResponseException {
    logger.debug("Sending command (" + command + ") to " + node.host + ":" + node.listenerPort + " for pin " + pinNumber + " (value=" + pinValue + ")");
    logger.debug("Attempt " + attempt + "...");
    DatagramSocket socket = null;
    try {
        socket = new DatagramSocket();
        socket.setSoTimeout(node.socketTimeout);
        InetAddress inetAddress = InetAddress.getByName(node.host);
        // send the packet
        byte[] sendData = new byte[] { command, (byte) pinNumber, (byte) pinValue };
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, inetAddress, node.listenerPort);
        socket.send(sendPacket);
        // read the response
        byte[] receiveData = new byte[16];
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        socket.receive(receivePacket);
        // check the response is valid
        if (receiveData[0] == PifaceCommand.ERROR_ACK.toByte()) {
            String msg = "Error 'ack' received";
            logger.error(msg);
            throw new ErrorResponseException(msg);
        }
        if (receiveData[0] != commandAck) {
            String msg = "Unexpected 'ack' code received - expecting " + commandAck + " but got " + receiveData[0];
            logger.error(msg);
            throw new ErrorResponseException(msg);
        }
        if (receiveData[1] != pinNumber) {
            String msg = "Invalid pin received - expecting " + pinNumber + " but got " + receiveData[1];
            logger.error(msg);
            throw new ErrorResponseException(msg);
        }
        // return the data value
        logger.debug("Command successfully sent and acknowledged (returned " + receiveData[2] + ")");
        return receiveData[2];
    } catch (IOException e) {
        String msg = "Failed to send command (" + command + ") to " + node.host + ":" + node.listenerPort + " (attempt " + attempt + ")";
        logger.error(msg, e);
        throw new ErrorResponseException(msg);
    } finally {
        if (socket != null) {
            socket.close();
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 65 with DatagramSocket

use of java.net.DatagramSocket in project openhab1-addons by openhab.

the class MilightBinding method sendMessage.

protected void sendMessage(String messageBytes, String bridgeId) {
    String bridgeIp = bridgeIpConfig.get(bridgeId);
    Integer bridgePort = bridgePortConfig.get(bridgeId);
    if (bridgePort == null) {
        bridgePort = DEFAULT_PORT;
    }
    try {
        byte[] buffer = getMessageBytes(messageBytes);
        InetAddress IPAddress = InetAddress.getByName(bridgeIp);
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, IPAddress, bridgePort);
        DatagramSocket datagramSocket = new DatagramSocket();
        datagramSocket.send(packet);
        datagramSocket.close();
        logger.debug("Sent packet '{}' to bridge '{}' ({}:{})", new Object[] { messageBytes, bridgeId, bridgeIp, bridgePort });
    } catch (Exception e) {
        logger.error("Failed to send Message to '{}': ", new Object[] { bridgeIp, e.getMessage() });
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress) ConfigurationException(org.osgi.service.cm.ConfigurationException)

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