示例#1
0
文件: Receiver1.java 项目: wpond/COMN
  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();
  }
示例#2
0
  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());
    }
  }
示例#3
0
 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());
 }