private void sendMessage() { if (cs == null) return; try { cs.sendMessage(sendMessage); } catch (Exception e) { System.out.print("Fail to send message!" + "\n"); } }
private boolean createConnection() { cs = new socket(ip, port); try { cs.CreateConnection(); System.out.print("Conneted with helicopter!" + "\n"); return true; } catch (Exception e) { System.out.print("Fail to conncet with helicopter!" + "\n"); return false; } }
private void getMessage() { if (cs == null) return; DataInputStream inputStream = null; try { inputStream = cs.getMessageStream(); } catch (Exception e) { System.out.print("Error\n"); return; } try { // save the file locally, will keep the file name unchanged. String savePath = "d:\\"; int bufferSize = 8192; byte[] buf = new byte[bufferSize]; int passedlen = 0; long len = 0; savePath += inputStream.readUTF(); DataOutputStream fileOut = new DataOutputStream( new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)))); len = inputStream.readLong(); System.out.println("filelength:" + len + "\n"); System.out.println("Receiving the file!" + "\n"); while (true) { int read = 0; if (inputStream != null) { read = inputStream.read(buf); } passedlen += read; if (read == -1) { break; } // display the transmission prosess System.out.println("File has been transmitted" + (passedlen * 100 / len) + "%\n"); fileOut.write(buf, 0, read); } System.out.println("Done, file has been saved in " + savePath + "\n"); fileOut.close(); } catch (Exception e) { System.out.println("Error" + "\n"); return; } }