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);
}
}
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();
}
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();
}
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();
}
}
}
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() });
}
}
Aggregations