Java Code Examples for java.net.datagramsocket#getReceiveBufferSize()

The following examples show how to use java.net.datagramsocket#getReceiveBufferSize() . 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: UDPReceiver.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private DatagramSocket createSocket0(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);
    }
}
 
Example 2
Source File: SyslogServer.java    From freeacs with MIT License 5 votes vote down vote up
private DatagramPacket initServer() {
  do {
    logger.info("Will try to bind server to port " + properties.getPort());
    try {
      socket = new DatagramSocket(properties.getPort());
      socket.setSoTimeout(SOCKET_TIMEOUT);
      socket.setReceiveBufferSize(properties.getReceiveBufferSize() * 1024);
      logger.info("Created a socket and bound to port " + properties.getPort());
      byte[] log_buffer = new byte[socket.getReceiveBufferSize()];
      logger.info("Created receive buffer, size " + log_buffer.length / 1024 + " KB");
      DatagramPacket packet = new DatagramPacket(log_buffer, log_buffer.length);
      FailoverFileReader failoverFileReader = new FailoverFileReader(properties);
      Thread failoverFileReaderThread = new Thread(failoverFileReader);
      failoverFileReaderThread.setName("FailoverFileReader");
      failoverFileReaderThread.start();
      logger.info(
          "Created FailoverFileReader thread to read from any failover files created by the syslog server");
      return packet;
    } catch (Throwable t) {
      throwable = t;
      ok = false;
      if (!Sleep.isTerminated()) {
        logger.error("Error occured, server did not start, will try again in 60 sec", t);
      } else {
        break;
      }
      try {
        Thread.sleep(60000); // Try again in 60 sec
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  } while (true);
  return null; // unreachable code
}
 
Example 3
Source File: BufferOptions.java    From netcrusher-java with Apache License 2.0 5 votes vote down vote up
public void checkDatagramSocket(DatagramSocket socket) throws SocketException {
    final long sizeTotal = count * size;

    if (sizeTotal < socket.getReceiveBufferSize()) {
        LOGGER.warn("Total buffer size {} is less than UDP socket SO_RCVBUF buffer size {}. Increase buffer size",
            sizeTotal, socket.getReceiveBufferSize());
    }

    if (sizeTotal < socket.getSendBufferSize()) {
        LOGGER.warn("Total buffer size {} is less than UDP socket SO_SNDBUF buffer size {}. Increase buffer size",
            sizeTotal, socket.getSendBufferSize());
    }
}
 
Example 4
Source File: UDPReceiverTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@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();
}