Java Code Examples for java.nio.channels.DatagramChannel#socket()

The following examples show how to use java.nio.channels.DatagramChannel#socket() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testInitialState() throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    try {
        DatagramSocket socket = dc.socket();
        assertFalse(socket.isBound());
        assertFalse(socket.getBroadcast());
        assertFalse(socket.isClosed());
        assertFalse(socket.isConnected());
        assertEquals(0, socket.getLocalPort());
        assertTrue(socket.getLocalAddress().isAnyLocalAddress());
        assertNull(socket.getLocalSocketAddress());
        assertNull(socket.getInetAddress());
        assertEquals(-1, socket.getPort());
        assertNull(socket.getRemoteSocketAddress());
        assertFalse(socket.getReuseAddress());

        assertSame(dc, socket.getChannel());
    } finally {
        dc.close();
    }
}
 
Example 2
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for 'DatagramChannelImpl.socket()'
 */
public void testSocket_BasicStatusBeforeConnect() throws Exception {
    final DatagramChannel dc = DatagramChannel.open();

    assertFalse(dc.isConnected());// not connected
    DatagramSocket s1 = dc.socket();

    assertFalse(s1.isBound());
    assertFalse(s1.isClosed());
    assertFalse(s1.isConnected());
    assertFalse(s1.getBroadcast());
    assertFalse(s1.getReuseAddress());
    assertNull(s1.getInetAddress());
    assertTrue(s1.getLocalAddress().isAnyLocalAddress());
    assertEquals(s1.getLocalPort(), 0);
    assertNull(s1.getLocalSocketAddress());
    assertEquals(s1.getPort(), -1);
    assertTrue(s1.getReceiveBufferSize() >= 8192);
    assertNull(s1.getRemoteSocketAddress());
    assertFalse(s1.getReuseAddress());
    assertTrue(s1.getSendBufferSize() >= 8192);
    assertEquals(s1.getSoTimeout(), 0);
    assertEquals(s1.getTrafficClass(), 0);

    DatagramSocket s2 = dc.socket();
    // same
    assertSame(s1, s2);

    dc.close();
}
 
Example 3
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for 'DatagramChannelImpl.socket()'
 */
public void testSocket_Block_BasicStatusAfterConnect() throws IOException {
    final DatagramChannel dc = DatagramChannel.open();

    dc.connect(datagramSocket1Address);
    DatagramSocket s1 = dc.socket();
    assertSocketAfterConnect(s1);
    DatagramSocket s2 = dc.socket();
    // same
    assertSame(s1, s2);

    dc.close();
}
 
Example 4
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testSocket_NonBlock_BasicStatusAfterConnect() throws IOException {
    final DatagramChannel dc = DatagramChannel.open();
    dc.connect(datagramSocket1Address);
    dc.configureBlocking(false);

    DatagramSocket s1 = dc.socket();
    assertSocketAfterConnect(s1);
    DatagramSocket s2 = dc.socket();
    // same
    assertSame(s1, s2);

    dc.close();
}
 
Example 5
Source File: NioUDPDataSender.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 6
Source File: NioDatagramChannelConfig.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
NioDatagramChannelConfig(NioDatagramChannel channel, DatagramChannel javaChannel) {
    super(channel, javaChannel.socket());
    this.javaChannel = javaChannel;
}
 
Example 7
Source File: UkcpServerChannel.java    From kcp-netty with MIT License 4 votes vote down vote up
public UkcpServerChannel(DatagramChannel socket) {
    super(null, socket, SelectionKey.OP_READ);
    config = new DefaultUkcpServerChannelConfig(this, socket.socket());
}
 
Example 8
Source File: NioDatagramChannelConfig.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
NioDatagramChannelConfig(NioDatagramChannel channel, DatagramChannel javaChannel) {
    super(channel, javaChannel.socket());
    this.javaChannel = javaChannel;
}
 
Example 9
Source File: NetworkManager.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
public synchronized <T extends SelectableChannel> ChannelAction<T> registerAction(int port, ConnectionType type, ChannelListener<T> listener, int ops) throws IOException
{
  boolean startProc = (channels.size() == 0);
  SelectableChannel channel = null;
  SocketAddress address = new InetSocketAddress(port);
  ConnectionInfo connectionInfo = new ConnectionInfo();
  connectionInfo.address =  address;
  connectionInfo.connectionType = type;
  ChannelConfiguration channelConfiguration = channels.get(connectionInfo);
  if (channelConfiguration == null) {
    Object socket = null;
    if (type == ConnectionType.TCP) {
      SocketChannel schannel = SocketChannel.open();
      schannel.configureBlocking(false);
      Socket ssocket = schannel.socket();
      ssocket.bind(address);
      socket = ssocket;
      channel = schannel;
    } else if (type == ConnectionType.UDP) {
      DatagramChannel dchannel = DatagramChannel.open();
      dchannel.configureBlocking(false);
      DatagramSocket dsocket = dchannel.socket();
      dsocket.bind(address);
      socket = dsocket;
      channel = dchannel;
    }
    if (channel == null) {
      throw new IOException("Unsupported connection type");
    }
    channelConfiguration = new ChannelConfiguration();
    channelConfiguration.actions = new ConcurrentLinkedQueue<ChannelAction>();
    channelConfiguration.channel = channel;
    channelConfiguration.connectionInfo = connectionInfo;
    channels.put(connectionInfo, channelConfiguration);
    channelConfigurations.put(channel, channelConfiguration);
  } else {
    channel = channelConfiguration.channel;
  }
  ChannelAction channelAction = new ChannelAction();
  channelAction.channelConfiguration = channelConfiguration;
  channelAction.listener = listener;
  channelAction.ops = ops;
  channelConfiguration.actions.add(channelAction);
  if (startProc) {
    startProcess();
  }
  if (listener != null) {
    channel.register(selector, ops);
  }
  return channelAction;
}