// ------------------------
  // Handler for timer
  // ------------------------
  public void actionPerformed(ActionEvent e) {

    // if the current image nb is less than the length of the video
    if (imagenb < VIDEO_LENGTH) {
      // update current imagenb
      imagenb++;

      try {
        // get next frame to send from the video, as well as its size
        int image_length = video.getnextframe(buf);

        // Builds an RTPpacket object containing the frame
        RTPpacket rtp_packet =
            new RTPpacket(MJPEG_TYPE, imagenb, imagenb * FRAME_PERIOD, buf, image_length);

        // get to total length of the full rtp packet to send
        int packet_length = rtp_packet.getlength();

        // retrieve the packet bitstream and store it in an array of bytes
        byte[] packet_bits = new byte[packet_length];
        rtp_packet.getpacket(packet_bits);

        // send the packet as a DatagramPacket over the UDP socket
        senddp = new DatagramPacket(packet_bits, packet_length, ClientIPAddr, RTP_dest_port);
        RTPsocket.send(senddp);

        // System.out.println("Send frame #"+imagenb);
        // print the header bitstream
        rtp_packet.printheader();

        // update GUI
        label.setText("Send frame #" + imagenb);
      } catch (Exception ex) {
        System.out.println("Exception caught: " + ex);
        System.exit(0);
      }
    } else {
      // if we have reached the end of the video file, stop the timer
      timer.stop();
    }
  }
Esempio n. 2
0
    @Override
    public void run() {

      try {
        // Create a packet for sending data
        DatagramPacket sendPacket = new DatagramPacket(buffer, buffer.length);

        OutputStream outputStream = null;
        String fileName = "";
        boolean createFile = true;
        int bytesReceived = 0;
        long totalBytesReceived = 0;
        long fileSize = 0;

        socket.receive(receivePacket);

        // Display the client number
        textArea.append("Starting thread for UDP client " + clientNo + " at " + new Date() + '\n');

        textArea.append(
            "The client host name is "
                + receivePacket.getAddress().getHostName()
                + " and port number is "
                + receivePacket.getPort()
                + '\n');

        // Continuously serve the client
        while (true) {
          bytesReceived = receivePacket.getLength();

          if (bytesReceived
              > 0) { // Get the file transmission header from the initial client packet
            String transmitHeader = new String(receivePacket.getData(), 0, bytesReceived);
            // transmitHeader = transmitHeader.substring(0, bytesReceived).trim().;
            String[] header = transmitHeader.split(HEADER_DEL);
            fileSize = Long.parseLong(header[0]);
            fileName = header[1];

            // Send receipt acknowledgment back to the client. Just send back the number of bytes
            // received.
            setSendPacketAddress(sendPacket, receivePacket);
            sendPacket.setData(String.valueOf(bytesReceived).getBytes());
            socket.send(sendPacket);
          }

          while (totalBytesReceived < fileSize) {
            // Wait for client to send bytes
            // socket.setSendBufferSize(BUFFER_SIZE);
            socket.receive(receivePacket);
            bytesReceived = receivePacket.getLength();

            if (totalBytesReceived == 0) {
              if (createFile) { // Get a unique name for the file to be received
                // fileName = getUniqueFileName();
                fileName = textFolder.getText() + fileName;
                outputStream = createFile(fileName);
                createFile = false;
                textArea.append("Receiving file from client.\n");
              }

              // Write bytes to file
              outputStream.write(receivePacket.getData(), 0, bytesReceived);
            } else {
              if (outputStream != null) { // Write bytes to file, if any
                outputStream.write(receivePacket.getData(), 0, bytesReceived);
              }
            }

            // Increment total bytes received
            totalBytesReceived += bytesReceived;

            // Tell the client to send more data. Just send back the number of bytes received.
            sendPacket.setData(String.valueOf(bytesReceived).getBytes());
            socket.send(sendPacket);

            // buffer = new byte[BUFFER_SIZE];
            Arrays.fill(buffer, (byte) 0);

            receivePacket = new DatagramPacket(buffer, buffer.length);
          }

          outputStream.flush();
          outputStream.close();

          textArea.append("Received file successfully. Saved as " + fileName + "\n");

          // Tell the client transmission is complete. Just send back the total number of bytes
          // received.
          sendPacket.setData(String.valueOf(totalBytesReceived).getBytes());
          socket.send(sendPacket);

          // Reset creation flag
          createFile = true;
          totalBytesReceived = 0;

          // Wait for client to send another file
          socket.receive(receivePacket);
        }
      } catch (IOException e) {
        System.err.println(e);
      }
    }