use of java.nio.channels.DatagramChannel 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);
}
}
use of java.nio.channels.DatagramChannel in project opennms by OpenNMS.
the class SelectorTrackerTest method test.
@Test
public void test() throws IOException {
Selector selector = Selector.open();
assertTrue(selector.isOpen());
selector.close();
assertFalse(selector.isOpen());
DatagramChannel c = DatagramChannel.open();
DatagramSocket s = c.socket();
s.setSoTimeout(1000);
byte[] buf = new byte[1024];
DatagramPacket p = new DatagramPacket(buf, 1024, InetAddress.getLocalHost(), 7);
s.send(p);
}
use of java.nio.channels.DatagramChannel in project voltdb by VoltDB.
the class ExportBenchmark method setupSocketListener.
/**
* Sets up a UDP socket on a certain port to listen for connections.
*/
private void setupSocketListener() {
DatagramChannel channel = null;
// Setup Listener
try {
statsSocketSelector = SelectorProvider.provider().openSelector();
channel = DatagramChannel.open();
channel.configureBlocking(false);
} catch (IOException e) {
exitWithException("Couldn't set up network channels", e);
}
// Bind to port & register with a channel
try {
InetSocketAddress isa = new InetSocketAddress(CoreUtils.getLocalAddress(), config.statsPort);
channel.socket().setReuseAddress(true);
channel.socket().bind(isa);
channel.register(statsSocketSelector, SelectionKey.OP_READ);
} catch (IOException e) {
exitWithException("Couldn't bind to socket", e);
}
}
use of java.nio.channels.DatagramChannel in project jdk8u_jdk by JetBrains.
the class JdpClient method main.
public static void main(String[] args) {
try {
String discoveryPort = System.getProperty("com.sun.management.jdp.port");
String discoveryAddress = System.getProperty("com.sun.management.jdp.address");
if (discoveryAddress == null || discoveryPort == null) {
System.out.println("Test failed. address and port must be specified");
return;
}
int port = Integer.parseInt(discoveryPort);
InetAddress address = InetAddress.getByName(discoveryAddress);
ProtocolFamily family = (address instanceof Inet6Address) ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
DatagramChannel channel;
channel = DatagramChannel.open(family);
channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
channel.bind(new InetSocketAddress(port));
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface interf : Collections.list(nets)) {
if (interf.supportsMulticast()) {
try {
channel.join(address, interf);
} catch (IOException e) {
// Skip not configured interfaces
}
}
}
PacketListener listener = new PacketListener(channel);
new Thread(listener, "Jdp Client").start();
} catch (RuntimeException e) {
System.out.println("Test failed.");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Test failed. unexpected error " + e);
}
}
use of java.nio.channels.DatagramChannel in project robovm by robovm.
the class DatagramChannelTest method test_read_LByteBuffer_NotConnected_nullBuf.
/**
* @tests DatagramChannel#read(ByteBuffer)
*/
public void test_read_LByteBuffer_NotConnected_nullBuf() throws Exception {
// regression test for Harmony-754
ByteBuffer c = null;
DatagramChannel channel = DatagramChannel.open();
try {
channel.read(c);
fail("Should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
}
Aggregations