Package java.net

Examples of java.net.DatagramPacket


      _socket.send(packet);
    }
   
    public DnsMessage waitAnswer(int timeout) throws IOException
    {
      DatagramPacket packet = new DatagramPacket(new byte[MAX_PACKET_SIZE], MAX_PACKET_SIZE);
      _socket.setSoTimeout(timeout);
      _socket.receive(packet);
      DnsMessage message = new DnsMessage();
      message.decode(new ByteArrayBuffer(packet.getData()));
      _socket.close();
      return message;
    }
View Full Code Here


  protected void doStart() throws Exception
  {   
    _packets = new DatagramPacket[getAcceptors()];
    for (int i = 0; i < getAcceptors(); i++)
    {
      _packets[i] = new DatagramPacket(new byte[MAX_UDP_SIZE], MAX_UDP_SIZE);
    }
    super.doStart();
  }
View Full Code Here

    _datagramSocket.close();
  }
 
  public void accept(int acceptorID) throws IOException, InterruptedException
  {
    DatagramPacket p = _packets[acceptorID];
   
    _datagramSocket.receive(p);

    int length = p.getLength();
    if (length == 2 || length == 4) return;
   
    byte[] b = new byte[length];
    System.arraycopy(p.getData(), 0, b, 0, length);
   
    Buffer buffer = new ByteArrayBuffer(b);
   
    EventHandler handler = new EventHandler();
    SipParser parser = new SipParser(buffer, handler);
   
    try
    {
      parser.parse();
     
      SipMessage message = handler.getMessage();
      message.setConnection(new UdpConnection(p.getAddress(), p.getPort()));
     
      /*
      message.set5uple(getTransportOrdinal(), getAddr(), getPort(), p.getAddress(), p.getPort());
   
      if (message.isRequest())
        ((SipRequest) message).setEndpoint(this);
       
      */
     
      process(message);
    }
    catch (Throwable t)
    {
      if (_statsStartedAt != -1)
      {
        synchronized (_statsLock)
        {
          _nbParseErrors++;
        }
      }
      Log.warn(t);
      //if (handler.hasException())
        //Log.warn(handler.getException());
       
      if (Log.isDebugEnabled())
        Log.debug("Buffer content: \r\n" + HexString.toDetailedHexString(buffer.array(), p.getLength()));
    }
   
  }
View Full Code Here

      return _remotePort;
    }
   
    public void write(Buffer buffer) throws IOException
    {
      DatagramPacket packet = new DatagramPacket(
          buffer.array(),
          buffer.length(),
          _remoteAddr,
          _remotePort);
      _datagramSocket.send(packet);
View Full Code Here

    }

    private static void waitForSignals(final JMeterEngine engine, DatagramSocket socket) {
        byte[] buf = new byte[80];
        System.out.println("Waiting for possible shutdown message on port "+socket.getLocalPort());
        DatagramPacket request = new DatagramPacket(buf, buf.length);
        try {
            while(true) {
                socket.receive(request);
                InetAddress address = request.getAddress();
                // Only accept commands from the local host
                if (address.isLoopbackAddress()){
                    String command = new String(request.getData(), request.getOffset(), request.getLength(),"ASCII");
                    System.out.println("Command: "+command+" received from "+address);
                    log.info("Command: "+command+" received from "+address);
                    if (command.equals("StopTestNow")){
                        engine.stopTest(true);
                    } else if (command.equals("Shutdown")) {
View Full Code Here

        init();
    }

    public void init() throws IOException {
        setupSocket();
        sendPacket = new DatagramPacket(new byte[MAX_PACKET_SIZE],MAX_PACKET_SIZE);
        sendPacket.setAddress(address);
        sendPacket.setPort(port);
        receivePacket = new DatagramPacket(new byte[MAX_PACKET_SIZE],MAX_PACKET_SIZE);
        receivePacket.setAddress(address);
        receivePacket.setPort(port);
        member.setCommand(new byte[0]);
        member.getData(true, true);
        if ( membership == null ) membership = new Membership(member);
View Full Code Here

        //if ( (startLevel&Channel.MBR_TX_SEQ) != Channel.MBR_TX_SEQ ) return;
        member.inc();
        if(log.isTraceEnabled())
            log.trace("Mcast send ping from member " + member);
        byte[] data = member.getData();
        DatagramPacket p = new DatagramPacket(data,data.length);
        p.setAddress(address);
        p.setPort(port);
        socket.send(p);
        if ( checkexpired ) checkExpired();
    }
View Full Code Here

      // The receiving end had better be expecting whatever encoding
      // is used here on the sending end!
      rawData = string.getBytes(encoding);
    }

    DatagramPacket packet =
      new DatagramPacket(
                 rawData,
                 rawData.length,
                 address,
                 port);
View Full Code Here

        }

        // write some messages
        for (int i = 0; i < CLIENT_COUNT; i++) {
            byte[] data = ("test:" + i).getBytes();
            clients[i].send(new DatagramPacket(data, data.length, serverAddy));
        }

        // does the session open message was fired ?
        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // test is message was received by the server
        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // does response was wrote and sent ?
        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // read the echos
        final byte[] buffer = new byte[1024];

        for (int i = 0; i < CLIENT_COUNT; i++) {
            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
            clients[i].receive(dp);
            final String text = new String(buffer, 0, dp.getLength());
            assertEquals("test:" + i, text);
        }

        msgReadLatch = new CountDownLatch(CLIENT_COUNT);

        // try again
        // write some messages again
        for (int i = 0; i < CLIENT_COUNT; i++) {
            byte[] data = ("test:" + i).getBytes();
            clients[i].send(new DatagramPacket(data, data.length, serverAddy));
        }

        // test is message was received by the server
        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
View Full Code Here

        }

        // write some messages
        for (int i = 0; i < CLIENT_COUNT; i++) {
            byte[] data = ("test:" + i).getBytes();
            clients[i].send(new DatagramPacket(data, data.length, serverAddy));
        }

        // does the session open message was fired ?
        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // test is message was received by the server
        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // does response was wrote and sent ?
        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // read the echos
        final byte[] buffer = new byte[1024];

        for (int i = 0; i < CLIENT_COUNT; i++) {
            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
            clients[i].receive(dp);
            final String text = new String(buffer, 0, dp.getLength());
            assertEquals("test:" + i, text);
        }

        msgReadLatch = new CountDownLatch(CLIENT_COUNT);

        // try again
        // write some messages again
        for (int i = 0; i < CLIENT_COUNT; i++) {
            byte[] data = ("test:" + i).getBytes();
            clients[i].send(new DatagramPacket(data, data.length, serverAddy));
        }

        // test is message was received by the server
        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
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.