@Override
 public int getReceiveBufferSize() {
   try {
     return socket.getReceiveBufferSize();
   } catch (SocketException e) {
     throw new ChannelException(e);
   }
 }
Example #2
0
 /**
  * Konstruktor
  *
  * @param port UDP-Port, der lokal fuer das Datagramm-Socket verwendet werden soll
  */
 public UdpSocket(int port) throws SocketException {
   socket = new DatagramSocket(port);
   try {
     log.debug(
         "Groesse des Empfangspuffers des Datagram-Sockets: "
             + socket.getReceiveBufferSize()
             + " Byte");
     log.debug(
         "Groesse des Sendepuffers des Datagram-Sockets: " + socket.getSendBufferSize() + " Byte");
   } catch (SocketException e) {
     log.debug("Socketfehler: " + e);
   }
 }
Example #3
0
  /**
   * Konstruktor
   *
   * @param port UDP-Port, der lokal fuer das Datagramm-Socket verwendet werden soll
   * @param sendBufferSize Groesse des Sendepuffers in Byte
   * @param receiveBufferSize Groesse des Empfangspuffers in Byte
   */
  public UdpSocket(int port, int sendBufferSize, int receiveBufferSize) throws SocketException {
    socket = new DatagramSocket(port);
    try {
      socket.setReceiveBufferSize(receiveBufferSize);
      socket.setSendBufferSize(sendBufferSize);

      System.out.println(
          "Groesse des Empfangspuffers des Datagram-Sockets: "
              + socket.getReceiveBufferSize()
              + " Byte");
      System.out.println(
          "Groesse des Sendepuffers des Datagram-Sockets: " + socket.getSendBufferSize() + " Byte");
    } catch (SocketException e) {
      log.debug("Socketfehler: " + e);
    }
  }
 public UdpSocketHandler(
     int listenPort,
     InetAddress bindto,
     Node node,
     long startupTime,
     String title,
     IOStatisticCollector collector)
     throws SocketException {
   this.node = node;
   this.collector = collector;
   this.title = title;
   _bindTo = bindto;
   // Keep the Updater code in, just commented out, for now
   // We may want to be able to do on-line updates.
   //		if (Updater.hasResource()) {
   //			_sock = (DatagramSocket) Updater.getResource();
   //		} else {
   this.listenPort = listenPort;
   _sock = new DatagramSocket(listenPort, bindto);
   int sz = _sock.getReceiveBufferSize();
   if (sz < 65536) {
     _sock.setReceiveBufferSize(65536);
   }
   try {
     // Exit reasonably quickly
     _sock.setReuseAddress(true);
   } catch (SocketException e) {
     throw new RuntimeException(e);
   }
   //		}
   // Only used for debugging, no need to seed from Yarrow
   dropRandom = node.fastWeakRandom;
   logMINOR = Logger.shouldLog(LogLevel.MINOR, this);
   logDEBUG = Logger.shouldLog(LogLevel.DEBUG, this);
   tracker = AddressTracker.create(node.lastBootID, node.runDir(), listenPort);
   tracker.startSend(startupTime);
 }
Example #5
0
  String dumpSocketInfo() throws Exception {
    StringBuffer sb = new StringBuffer();
    sb.append("local_addr=").append(local_addr);
    sb.append(", mcast_addr=").append(mcast_addr);
    sb.append(", bind_addr=").append(bind_addr);
    sb.append(", ttl=").append(ip_ttl);

    if (sock != null) {
      sb.append("\nsocket: bound to ");
      sb.append(sock.getLocalAddress().getHostAddress()).append(":").append(sock.getLocalPort());
      sb.append(", receive buffer size=").append(sock.getReceiveBufferSize());
      sb.append(", send buffer size=").append(sock.getSendBufferSize());
    }

    if (mcast_sock != null) {
      sb.append("\nmulticast socket: bound to ");
      sb.append(mcast_sock.getInterface().getHostAddress())
          .append(":")
          .append(mcast_sock.getLocalPort());
      sb.append(", send buffer size=").append(mcast_sock.getSendBufferSize());
      sb.append(", receive buffer size=").append(mcast_sock.getReceiveBufferSize());
    }
    return sb.toString();
  }
 public void run() {
   DatagramSocket socketCopy = socket;
   if (socketCopy != null) {
     try {
       socketCopy.setSoTimeout(getSocketTimeout());
       if (receiveBufferSize > 0) {
         socketCopy.setReceiveBufferSize(Math.max(receiveBufferSize, maxInboundMessageSize));
       }
       if (logger.isDebugEnabled()) {
         logger.debug(
             "UDP receive buffer size for socket "
                 + getAddress()
                 + " is set to: "
                 + socketCopy.getReceiveBufferSize());
       }
     } catch (SocketException ex) {
       logger.error(ex);
       setSocketTimeout(0);
     }
   }
   while (!stop) {
     DatagramPacket packet =
         new DatagramPacket(buf, buf.length, udpAddress.getInetAddress(), udpAddress.getPort());
     try {
       socketCopy = socket;
       try {
         if (socketCopy == null) {
           stop = true;
           continue;
         }
         socketCopy.receive(packet);
       } catch (InterruptedIOException iiox) {
         if (iiox.bytesTransferred <= 0) {
           continue;
         }
       }
       if (logger.isDebugEnabled()) {
         logger.debug(
             "Received message from "
                 + packet.getAddress()
                 + "/"
                 + packet.getPort()
                 + " with length "
                 + packet.getLength()
                 + ": "
                 + new OctetString(packet.getData(), 0, packet.getLength()).toHexString());
       }
       ByteBuffer bis;
       // If messages are processed asynchronously (i.e. multi-threaded)
       // then we have to copy the buffer's content here!
       if (isAsyncMsgProcessingSupported()) {
         byte[] bytes = new byte[packet.getLength()];
         System.arraycopy(packet.getData(), 0, bytes, 0, bytes.length);
         bis = ByteBuffer.wrap(bytes);
       } else {
         bis = ByteBuffer.wrap(packet.getData());
       }
       TransportStateReference stateReference =
           new TransportStateReference(
               DefaultUdpTransportMapping.this,
               udpAddress,
               null,
               SecurityLevel.undefined,
               SecurityLevel.undefined,
               false,
               socketCopy);
       fireProcessMessage(
           new UdpAddress(packet.getAddress(), packet.getPort()), bis, stateReference);
     } catch (SocketTimeoutException stex) {
       // ignore
     } catch (PortUnreachableException purex) {
       synchronized (DefaultUdpTransportMapping.this) {
         listener = null;
       }
       logger.error(purex);
       if (logger.isDebugEnabled()) {
         purex.printStackTrace();
       }
       if (SNMP4JSettings.isFowardRuntimeExceptions()) {
         throw new RuntimeException(purex);
       }
       break;
     } catch (SocketException soex) {
       if (!stop) {
         logger.error(
             "Socket for transport mapping " + toString() + " error: " + soex.getMessage(),
             soex);
       }
       if (SNMP4JSettings.isFowardRuntimeExceptions()) {
         stop = true;
         throw new RuntimeException(soex);
       }
     } catch (IOException iox) {
       logger.warn(iox);
       if (logger.isDebugEnabled()) {
         iox.printStackTrace();
       }
       if (SNMP4JSettings.isFowardRuntimeExceptions()) {
         throw new RuntimeException(iox);
       }
     }
   }
   synchronized (DefaultUdpTransportMapping.this) {
     listener = null;
     stop = true;
     DatagramSocket closingSocket = socket;
     if ((closingSocket != null) && (!closingSocket.isClosed())) {
       closingSocket.close();
     }
   }
   if (logger.isDebugEnabled()) {
     logger.debug("Worker task stopped:" + getClass().getName());
   }
 }
Example #7
0
  public static void main(String[] args) throws IOException {

    long start = System.currentTimeMillis();
    long used = 0;
    long total = 10000000;
    long step = 2000;

    int port = 8080;

    // Create a socket to listen on the port.
    DatagramSocket dsocket = new DatagramSocket(port, InetAddress.getByName("192.168.1.107"));
    // Create a buffer to read datagrams into. If a
    // packet is larger than this buffer, the
    // excess will simply be discarded!
    byte[] buffer = new byte[2048];
    // Create a packet to receive data into the buffer
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    // String[] s = new String[(int)total];

    // Now loop forever, waiting to receive packets and printing them.
    dsocket.setSoTimeout(1000);
    // actually useless
    dsocket.setReceiveBufferSize(3145728);
    System.out.println("dsocket.getReceiveBufferSize():" + dsocket.getReceiveBufferSize());

    while (used < 30000) {
      // Wait to receive a datagram
      try {
        dsocket.receive(packet);
      } catch (Exception e) {
        // continue ;
        used = System.currentTimeMillis() - start;
        continue;
      }

      // Convert the contents to a string, and display them
      String msg = new String(buffer, 0, packet.getLength());
      // s[cnt] = msg;
      // Reset the length of the packet before reusing it.
      packet.setLength(buffer.length);

      cnt++;
      if (cnt % (step) == 0) {
        used = System.currentTimeMillis() - start;
        System.out.println(
            "recive package:" + cnt + " through-put:" + ((double) cnt / (double) used) * 1000);
        System.out.println(msg);
      }

      if (cnt == total) break;
    }

    used = System.currentTimeMillis() - start;
    System.out.print(
        "used time: "
            + used
            + "ms total: "
            + cnt
            + " pkgs through-put:"
            + ((double) cnt / (double) used) * 1000
            + " req/s lost-package:"
            + (total - cnt));
  }