use of java.nio.channels.DatagramChannel in project webpieces by deanhiller.
the class FixedTestAfterBindGetPortReturnsZero method testAfterBindGetPortReturnsZero.
/**
* External Bug ID: 4739238
* This tests that after the TCP bind on port 0(which results in grabbing
* any available port) that getLocalPort returns 0 instead of the port
* we bound to like ServerSocket does. This test also proves
* DatagramChannel does this correctly and returns the port that
* the jdk bound to.
*/
public void testAfterBindGetPortReturnsZero() throws Exception {
String fixVersion = "1.6.0_02";
String jdkVersion = System.getProperty("java.vm.version");
if (jdkVersion.compareTo(fixVersion) >= 0)
return;
SocketChannel chan1 = SocketChannel.open();
DatagramChannel chan2 = DatagramChannel.open();
chan1.socket().setReuseAddress(true);
chan2.socket().setReuseAddress(true);
chan1.configureBlocking(false);
chan2.configureBlocking(false);
InetAddress loopBack = InetAddress.getByName("127.0.0.1");
InetSocketAddress addr = new InetSocketAddress(loopBack, 0);
chan1.socket().bind(addr);
chan2.socket().bind(addr);
// port doesn't return the port we bound too for tcp, but
// does for udp
int port1 = chan1.socket().getLocalPort();
int port2 = chan2.socket().getLocalPort();
assertTrue("TCP should not return 0 but does", port1 == 0);
assertTrue("Udp should not return 0 and doesn't(good)", port2 != 0);
SocketAddress tcpAddr = chan1.socket().getLocalSocketAddress();
log.info("addr=" + tcpAddr);
InetSocketAddress expected = new InetSocketAddress(loopBack, 0);
assertEquals("local addr port is returning 0, this is bad", expected, tcpAddr);
}
use of java.nio.channels.DatagramChannel in project webpieces by deanhiller.
the class TestBasicUDP method xxxtestUDPWithDisconnectConnect.
// On MAC this was failing while on other platforms it was fine....
public void xxxtestUDPWithDisconnectConnect() throws Exception {
client1.oldConnect(remoteAddr);
DatagramChannel svrChan = mockServer.getUDPServerChannel();
client1.registerForReads((DataListener) mockHandler);
verifyDataPassing(svrChan);
// now disconnect, have server send some udp packets which should be rejected
client1.disconnect();
writeFromServer(svrChan);
try {
client1.oldWrite(createBuffer());
fail("Should have thrown a NotYetConnectedException");
} catch (IllegalStateException e) {
// should land here
}
// try {
// HandlerForTests.checkForWarnings();
// fail("log should have had warnings");
// } catch (LogHasWarningException e) {
// }
}
use of java.nio.channels.DatagramChannel 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) {
IOUtils.closeQuietly(socket);
IOUtils.closeQuietly(datagramChannel);
throw new IllegalStateException("DatagramChannel create fail. Cause" + e.getMessage(), e);
}
}
use of java.nio.channels.DatagramChannel in project netty by netty.
the class NioDatagramChannel method doReadMessages.
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
DatagramChannel ch = javaChannel();
DatagramChannelConfig config = config();
RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
ByteBuf data = allocHandle.allocate(config.getAllocator());
allocHandle.attemptedBytesRead(data.writableBytes());
boolean free = true;
try {
ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
int pos = nioData.position();
InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
if (remoteAddress == null) {
return 0;
}
allocHandle.lastBytesRead(nioData.position() - pos);
buf.add(new DatagramPacket(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()), localAddress(), remoteAddress));
free = false;
return 1;
} catch (Throwable cause) {
PlatformDependent.throwException(cause);
return -1;
} finally {
if (free) {
data.release();
}
}
}
use of java.nio.channels.DatagramChannel in project jeromq by zeromq.
the class TestZLoop method testZLoopWithUDP.
@Test
public void testZLoopWithUDP() throws IOException {
final int port = Utils.findOpenPort();
final InetSocketAddress addr = new InetSocketAddress("127.0.0.1", port);
ZLoop loop = new ZLoop(ctx);
DatagramChannel udpIn = DatagramChannel.open();
assertThat(udpIn, notNullValue());
udpIn.configureBlocking(false);
udpIn.socket().bind(new InetSocketAddress(port));
DatagramChannel udpOut = DatagramChannel.open();
assertThat(udpOut, notNullValue());
udpOut.configureBlocking(false);
udpOut.socket().connect(addr);
final AtomicInteger counter = new AtomicInteger();
final AtomicBoolean done = new AtomicBoolean();
loop.addPoller(new PollItem(udpIn, ZMQ.Poller.POLLIN), new IZLoopHandler() {
@Override
public int handle(ZLoop loop, PollItem item, Object arg) {
DatagramChannel udpIn = (DatagramChannel) arg;
ByteBuffer bb = ByteBuffer.allocate(3);
try {
udpIn.receive(bb);
String read = new String(bb.array(), 0, bb.limit(), ZMQ.CHARSET);
assertThat(read, is("udp"));
done.set(true);
counter.incrementAndGet();
} catch (IOException e) {
e.printStackTrace();
fail();
}
return -1;
}
}, udpIn);
loop.addPoller(new PollItem(udpOut, ZMQ.Poller.POLLOUT), new IZLoopHandler() {
@Override
public int handle(ZLoop loop, PollItem item, Object arg) {
DatagramChannel udpOut = (DatagramChannel) arg;
try {
ByteBuffer bb = ByteBuffer.allocate(3);
bb.put("udp".getBytes(ZMQ.CHARSET));
bb.flip();
int written = udpOut.send(bb, addr);
assertThat(written, is(3));
counter.incrementAndGet();
} catch (IOException e) {
e.printStackTrace();
fail();
}
return 0;
}
}, udpOut);
loop.start();
assertThat(done.get(), is(true));
assertThat(counter.get(), is(2));
udpIn.close();
udpOut.close();
}
Aggregations