Package java.net

Examples of java.net.DatagramPacket


      this.addr = addr;
      this.port = port;
  }

  public int read(byte buffer[], int offset, int length) {
      DatagramPacket p = new DatagramPacket(buffer, offset, length, addr, port);
      try {
    sock.receive(p);
      } catch (IOException e) {
    return -1;
      }
      synchronized (this) {
    dataRead = true;
    notify();
      }
      return p.getLength();
  }
View Full Code Here


        for (int i = 0; i < remoteAddrs.size(); ++i) {
            InetAddress remoteAddr = remoteAddrs.elementAt(i);
            int remotePort = (remotePorts.elementAt(i)).intValue();

            try {
                socket.send(new DatagramPacket(buffer, offset, length, remoteAddr, remotePort));
            } catch (Exception e) {
                // TODO error handling
                return -1;
            }
        }
View Full Code Here

     */
    public int writeToRemote(byte[] buffer, int offset, int length,
            InetAddress remoteAddr, int remotePort) {

        try {
            socket.send(new DatagramPacket(buffer, offset, length, remoteAddr,
                    remotePort));
        } catch (Exception e) {
            // TODO error handling
            return -1;
        }
View Full Code Here

      if (o instanceof DatagramPacket)

      {

        final DatagramPacket packet = (DatagramPacket) o;

        if (length < packet.getLength())

          throw new IllegalArgumentException("Length must be at least " + packet.getLength());

        System.arraycopy(packet.getData(), packet.getOffset(), buffer, offset, packet.getLength());

        logger.finer("packet length: " + packet.getLength());

        return packet.getLength();

      }

      else if (o instanceof IOException)
View Full Code Here

          // TODO: avoid memory allocatioin for each packet:

          final byte[] data = new byte[getMinimumTransferSize()];

          final DatagramPacket packet = new DatagramPacket(data, data.length);

              try

        {
View Full Code Here

                try {

                    if (buffersUsed < buffer.length) {

                        DatagramPacket packet =

                            new DatagramPacket(buffer[currentReceive],

                                    0, buffer[currentReceive].length);

                        socket.receive(packet);

                        bufferSize[currentReceive] = packet.getLength();

                        currentReceive = (currentReceive + 1) % buffer.length;

                        buffersUsed++;
View Full Code Here

               if (trace)
               {
                  this.log.trace("HA-JNDI AutomaticDiscovery waiting for queries...");
               }
               byte[] buf = new byte[256];
               DatagramPacket packet = new DatagramPacket(buf, buf.length);
               this.socket.receive(packet);
               if (trace)
               {
                  this.log.trace("HA-JNDI AutomaticDiscovery Packet received.");
               }
View Full Code Here

                     this.log.trace("DiscoveryRequestHandler end");
                  }
                  return;
               }
            }
            DatagramPacket p = new DatagramPacket(this.ipAddress, this.ipAddress.length,
               this.packet.getAddress(), this.packet.getPort());
            if (trace)
            {
               this.log.trace("Sending AutomaticDiscovery answer: " + new String(this.ipAddress) +
                         " to " + this.packet.getAddress() + ":" + this.packet.getPort());
View Full Code Here

   private void test() throws Exception
   {
      try
      {
         byte[] data = "Hello".getBytes();
         DatagramPacket datagram = new DatagramPacket(data, 0, data.length, goodMulticast, mcastPort);
        
         System.out.println("Sending 'Hello' on " + goodMulticast + ":" + mcastPort);
         sender.send(datagram);
        
         Thread.sleep(1000);
View Full Code Here

         try
         {
            System.out.println("Listening on address " + group);
           
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
           
            value = new String(packet.getData()).trim();
           
            System.out.println(group + ":" + mcastPort + " -- Received " + value);
         }
         catch (Exception e)
         {
View Full Code Here

TOP

Related Classes of java.net.DatagramPacket

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.