Example #1
0
    public void run() {
      try {
        while (true) {
          StringBuffer sb = new StringBuffer();
          DataInputStream is = new DataInputStream(new BufferedInputStream(s.getInputStream()));
          String inputLine = "";
          int i;
          // System.out.println("server prepare accept msg");
          while ((i = is.read()) != 59) {

            if (i == -1) {

              continue;
            }
            sb.append((char) i);
          }
          if (sb.toString().indexOf("NETTST") == -1) {
            System.out.println("服务器收到信息为" + sb.toString());
          }
          System.out.println("服务器收到信息为" + sb.toString());
          // System.out.println("server has accept msg");
          this.sleep(2000);
          // is.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
Example #2
0
  public void run() {

    try {
      DataOutputStream out = new DataOutputStream(client.getOutputStream());
      FileInputStream file_in = new FileInputStream(file);
      DataInputStream in = new DataInputStream(file_in);

      byte buffer[] = new byte[512];

      while (in.read(buffer) != -1) {
        System.out.print("\nSending buffer to: " + client_id);
        out.write(buffer, 0, buffer.length);
      }
    } catch (Exception exception) {
      System.out.println("\nException" + exception);
      System.exit(0);
    } finally {
      try {
        client.close();
      } catch (Exception exception) {
        System.out.println("\nException" + exception);
        System.exit(0);
      }
    }
  }
Example #3
0
 private void exchangeKeys() {
   try {
     output.write(modulus.toByteArray());
     byte[] buffer = new byte[ciphertextBlockSize];
     input.read(buffer);
     recipModulus = new BigInteger(1, buffer);
   } catch (IOException ioe) {
     System.err.println("Error establishing keys");
   }
 }
  public static void read_stream(DataInputStream is, byte[] stream, int length) {
    int remlen = length;
    int off = 0;

    try {
      while (remlen > 0) {
        int redlen = is.read(stream, off, remlen);

        if (redlen == -1) {
          System.err.println("    failed to read_stream()");
          break;
        }
        off += redlen;
        remlen -= redlen;
      }
    } catch (IOException e) {
      System.err.println("IOException: " + e);
    }
  }
Example #5
0
 private void go() {
   try {
     // Read as long as there is input
     byte[] buffer = new byte[ciphertextBlockSize];
     boolean disconnectMsgSent = false;
     while (connection != null && !disconnectMsgSent && input.read(buffer) != -1) {
       // Decipher the bytes read in
       byte[] plaintext = Ciphers.RSADecipherWSalt(buffer, decipherExp, modulus);
       if (plaintext.length == 1 && plaintext[0] == 0) {
         disconnectMsgSent = true;
         closeAll();
       } else {
         // convert to a string and display
         message = new String(plaintext);
         displayArea.append("\n" + message);
       }
     }
   } catch (IOException ioe) {
     // Server disconnected-we can reconnect if we wish
   }
 }
Example #6
0
    @Override
    public void run() {

      try {
        // Create data input and output streams
        DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
        DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());

        // Set up a byte buffer to capture the file from the client
        byte[] buffer = new byte[BUFFER_SIZE];
        OutputStream outputStream = null;
        String fileName = "";
        boolean createFile = true;
        int bytesReceived = 0;
        long totalBytesReceived = 0;
        long fileSize = 0;

        // Continuously serve the client
        while (true) {
          bytesReceived = inputFromClient.read(buffer);

          if (bytesReceived
              > 0) { // Get the file transmission header from the initial client packet
            String transmitHeader = new String(buffer, 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.
            outputToClient.writeInt(bytesReceived);

            // Reinitialize buffer values
            buffer = new byte[BUFFER_SIZE];
            bytesReceived = 0;
          }

          // Wait for client to send bytes
          while ((bytesReceived = inputFromClient.read(buffer)) != -1) {
            if (inputFromClient.available() > 0) {
              if (createFile) { // Get a unique name for the file to be received
                fileName = textFolder.getText() + fileName; // getUniqueFileName();
                outputStream = createFile(fileName);
                createFile = false;
                textArea.append("Receiving file from client.\n");
              }

              // Write bytes to file
              outputStream.write(buffer, 0, bytesReceived);
            } else { // We get here if no more data is available, but some bytes were already
              // received

              // If bytes were received and the file wasn't already created,
              // it means that the file was smaller than our buffer size.
              // Create the file...
              if (bytesReceived > 0
                  && createFile) { // Get a unique name for the file to be received
                fileName = textFolder.getText() + fileName; // getUniqueFileName();
                outputStream = createFile(fileName);
                createFile = false;
                textArea.append("Receiving file from client.\n");
              }

              if (outputStream != null) {
                if (bytesReceived > 0) { // Write remaining bytes to file, if any
                  outputStream.write(buffer, 0, bytesReceived);
                }

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

                // Return success to client.
                outputToClient.writeInt(0);
              }

              // Reset creation flag
              createFile = true;
              break;
            }

            // Reinitialize buffer values
            buffer = new byte[BUFFER_SIZE];
            bytesReceived = 0;
          }
        }
      } catch (IOException e) {
        System.err.println(e);
      }
    }