java.nio.channels.DatagramChannel#socket()源码实例Demo

下面列出了java.nio.channels.DatagramChannel#socket() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: j2objc   文件: DatagramChannelTest.java
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();
    }
}
 
源代码2 项目: j2objc   文件: DatagramChannelTest.java
/**
 * 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();
}
 
源代码3 项目: j2objc   文件: DatagramChannelTest.java
/**
 * 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();
}
 
源代码4 项目: j2objc   文件: DatagramChannelTest.java
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();
}
 
源代码5 项目: pinpoint   文件: NioUDPDataSender.java
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);
    }
}
 
源代码6 项目: netty-4.1.22   文件: NioDatagramChannelConfig.java
NioDatagramChannelConfig(NioDatagramChannel channel, DatagramChannel javaChannel) {
    super(channel, javaChannel.socket());
    this.javaChannel = javaChannel;
}
 
源代码7 项目: kcp-netty   文件: UkcpServerChannel.java
public UkcpServerChannel(DatagramChannel socket) {
    super(null, socket, SelectionKey.OP_READ);
    config = new DefaultUkcpServerChannelConfig(this, socket.socket());
}
 
NioDatagramChannelConfig(NioDatagramChannel channel, DatagramChannel javaChannel) {
    super(channel, javaChannel.socket());
    this.javaChannel = javaChannel;
}
 
源代码9 项目: attic-apex-malhar   文件: NetworkManager.java
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;
}