Exemple #1
0
  public static void main(String[] args) {

    if (args.length < 2) {
      System.out.println("Usage: <port> <filename>");
      System.exit(0);
    }

    int port = Integer.parseInt(args[0]);
    String filename = args[1];

    DataOutputPacketManager dopm = null;
    IncomingConnection conn = null;

    try {
      dopm = new DataOutputPacketManager(filename);
      conn = new IncomingConnection(port);
    } catch (Exception e) {
      System.err.println(e.getMessage());
      e.printStackTrace();
      System.exit(99);
    }

    System.out.println("Ready to receive");

    Packet p;
    do {

      p = conn.getNextPacket();
      if (p == null) continue;

      try {
        System.out.println("Received packet (" + p.getShort(0) + ")");
      } catch (PacketException e1) {
        System.err.println(e1.getMessage());
        e1.printStackTrace();
      }

      try {
        dopm.storePacket(p);
      } catch (PacketException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        continue;
      }

    } while (!dopm.complete());

    System.out.println("Receiver complete");

    conn.close();
  }
  public void listen() {
    try {
      LocalServerSocket server = new LocalServerSocket(SOCKET_ADDRESS);

      LocalSocket receiver = server.accept();
      if (receiver != null) {
        InputStream input = receiver.getInputStream();

        byte[] buffer = new byte[1024];
        // simply for java.util.ArrayList
        boolean rec = true;
        while (rec) {
          int read = input.read(buffer);
          if (read == -1) {
            rec = false;
            Log.e(TAG, "-1 read in socket");
            break;
          }
          if (!isRecording) rec = false;
          if (!headerfound) {
            int i = ByteArrayFinder.find(buffer, pattern);
            if (i != -1) {
              headerfound = true;
              handleData(tmpch, header);
              handleData(tmpch, pattern);
              try {
                tmp = new byte[read - (i + pattern.length)];
                System.arraycopy(buffer, i + pattern.length, tmp, 0, read - (i + pattern.length));
                handleData(tmpch, tmp);
                Log.i(TAG, "Paquet: " + count);
                count++;
              } catch (Exception e) {
              }
            }
          } else {
            tmp = new byte[read];
            System.arraycopy(buffer, 0, tmp, 0, read);
            handleData(tmpch, tmp);
            Log.i(TAG, "Paquet: " + count);
            count++;
          }
          // out.write(buffer, 0, readed);
        }
        headerfound = false;
      }
    } catch (IOException e) {
      Log.e(getClass().getName(), e.getMessage());
    }
  }
 public void sendError(
     String error) { // Methode que le Client doit implémenter pour envoyer des informations
   conn.sendData(1, new LogPacket(System.currentTimeMillis(), (byte) 1, error).build());
 }