Package java.net

Examples of java.net.DatagramPacket


     */
    public void run() {
        this.done = false;
        while (!this.done) {
            byte[] buf = new byte[65507];
            DatagramPacket dp = new DatagramPacket(buf, buf.length);

            try {
                channel.receive(dp);
            } catch (SocketException e) {
                break;
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }

            String s = new String(dp.getData(), 0, dp.getLength());
            JSONObject jo = null;
            try {
                jo = new JSONObject(s);
            } catch (JSONException e) {
                System.err.println("Got bad JSON data. Contents:\n" + s);
View Full Code Here


    public int receive(byte[] buf, int off, int len, int waitMillis)
        throws IOException
    {
        socket.setSoTimeout(waitMillis);
        DatagramPacket packet = new DatagramPacket(buf, off, len);
        socket.receive(packet);
        return packet.getLength();
    }
View Full Code Here

             * which will be fragmented."
             */
            // TODO Exception
        }

        DatagramPacket packet = new DatagramPacket(buf, off, len);
        socket.send(packet);
    }
View Full Code Here

    }
    for (int portNei : portNeighbours) {
      if (portNei != port) {
        resp = new String("ADD_PORT " + portNei).getBytes();
        DatagramPacket answer = null;
        try {
          answer = new DatagramPacket(resp, resp.length,
              neighbourAddress);
          this.socket.send(answer);
        } catch (SocketException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
View Full Code Here

    try {
      ExecutorService exec = Executors.newFixedThreadPool(100);
      this.socket = new DatagramSocket(this.port);
      while (true) {
        byte[] buf = new byte[256];
        DatagramPacket dp = new DatagramPacket(buf, buf.length);
        this.socket.receive(dp);
        Worker w = new Worker(dp);
        exec.execute(w);
      }
    } catch (SocketException e) {
View Full Code Here

                .substring(ADD_PORT.length() + 1)));
          }
          if (command.equalsIgnoreCase(PUSH)) {
            Peer.this.stack.push(in.substring(PUSH.length() + 1));
          } else if (command.equalsIgnoreCase(PULL)) {
            DatagramPacket answer;
            byte[] resp;
            try {

              resp = Peer.this.stack.pop().getBytes();
              answer = new DatagramPacket(resp, resp.length,
                  this.packet.getSocketAddress());
              Peer.this.socket.send(answer);
            } catch (SocketException e) {
              e.printStackTrace();
            } catch (IOException e) {
View Full Code Here

    public void run()
    {
        try
        {
            byte buf[] = new byte[DNSConstants.MAX_MSG_ABSOLUTE];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            while (this.jmDNSImpl.getState() != DNSState.CANCELED)
            {
                packet.setLength(buf.length);
                this.jmDNSImpl.getSocket().receive(packet);
                if (this.jmDNSImpl.getState() == DNSState.CANCELED)
                {
                    break;
                }
                try
                {
                    if (this.jmDNSImpl.getLocalHost().shouldIgnorePacket(packet))
                    {
                        continue;
                    }

                    DNSIncoming msg = new DNSIncoming(packet);
                    // logger.finest("SocketListener.run() JmDNS in:" + msg.print(true));

                    synchronized (this.jmDNSImpl.getIoLock())
                    {
                        if (msg.isQuery())
                        {
                            if (packet.getPort() != DNSConstants.MDNS_PORT)
                            {
                                this.jmDNSImpl.handleQuery(msg, packet.getAddress(), packet.getPort());
                            }
                            this.jmDNSImpl.handleQuery(msg, this.jmDNSImpl.getGroup(), DNSConstants.MDNS_PORT);
                        }
                        else
                        {
View Full Code Here

     * Send an outgoing multicast DNS message.
     */
    public void send(DNSOutgoing out) throws IOException {
        out.finish();
        if (!out.isEmpty()) {
            final DatagramPacket packet = new DatagramPacket(out.data, out.off, group, DNSConstants.MDNS_PORT);

            try {
                final DNSIncoming msg = new DNSIncoming(packet);
                logger.finest("send() JmDNS out:" + msg.print(true));
            } catch (final IOException e) {
View Full Code Here

        socket.setSoTimeout(definition.getReadTimeoutMs());
        socket.setSendBufferSize(definition.getMessage().length);
        socket.setReceiveBufferSize(definition.getExpectedResponse().length);

        byte[] sendData = definition.getMessage();
        DatagramPacket sendPacket = new DatagramPacket(sendData, 0, sendData.length, socketAddress);

        try {
            socket.send(sendPacket);
            return processDatagramAnswer(definition, socket);
        } finally {
View Full Code Here

        result.setStatus(DatagramActionResultStatus.CAPTURED);

        int readSize = definition.getExpectedResponse().length;
        byte[] readBuffer = new byte[readSize];

        DatagramPacket receivePacket = new DatagramPacket(readBuffer, 0, readSize);

        try {
            socket.receive(receivePacket);
        } catch (InterruptedIOException e) {
            result.setStatus(DatagramActionResultStatus.TIMEOUT);
        }

        if ((result.getStatus() == DatagramActionResultStatus.CAPTURED) && (receivePacket.getLength() > 0)) {
            byte[] resultData = Arrays.copyOfRange(receivePacket.getData(),
                receivePacket.getOffset(), receivePacket.getLength());
            result.setResponse(resultData);
        }

        boolean succeed = (result.getStatus() == DatagramActionResultStatus.CAPTURED) &&
            Arrays.equals(result.getResponse(), definition.getExpectedResponse());
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.