已經配置完成了,并且可以連接 下面開始手機客戶端代碼實現了 連接tftp 使用的是一個commons-net-3.6.jar 里面的 TFTPClient jar 下載 http://commons.apache.org/proper/commons-net/download_net.cgi
TFTP服務器的端口是69publicfinalclassTFTPExample
{staticfinal String USAGE ="Usage: tftp [options] hostname localfile remotefile\n\n" +"hostname - The name of the remote host [:port]\n" +"localfile - The name of the local file to send or the name to use for\n" +"\tthe received file\n" +"remotefile - The name of the remote file to receive or the name for\n" +"\tthe remote server to use to name the local file being sent.\n\n" +"options: (The default is to assume -r -b)\n" +"\t-t timeout in seconds (default 60s)\n" +"\t-s Send a local file\n" +"\t-r Receive a remote file\n" +"\t-a Use ASCII transfer mode\n" +"\t-b Use binary transfer mode\n" +"\t-v Verbose (trace packets)\n";publicstaticvoidTftpUtils(Context context){boolean receiveFile = true, closed;int transferMode = TFTP.BINARY_MODE, argc;String arg, hostname, localFilename, remoteFilename;final TFTPClient tftp;int timeout = 60000;boolean verbose = false;hostname = "192.168.31.10:69";localFilename = Environment.getExternalStorageDirectory().getAbsolutePath()+"/test1.rar";Log.i("localFilename", localFilename);remoteFilename = "test.rar";// Create our TFTP instance to handle the file transfer.if (verbose) {tftp = new TFTPClient() {@Overrideprotectedvoidtrace(String direction, TFTPPacket packet) {System.out.println(direction + " " + packet);}};} else {tftp = new TFTPClient();}// We want to timeout if a response takes longer than 60 secondstftp.setDefaultTimeout(timeout);// We haven't closed the local file yet.closed = false;// If we're receiving a file, receive, otherwise send.if (receiveFile){closed = receive(transferMode, hostname, localFilename, remoteFilename, tftp);} else {// We're sending a file// closed = send(transferMode, hostname, localFilename, remoteFilename, tftp);}System.out.println("Recd: "+tftp.getTotalBytesReceived()+" Sent: "+tftp.getTotalBytesSent());if (!closed) {System.out.println("Failed");System.exit(1);}System.out.println("OK");}privatestaticbooleansend(int transferMode, String hostname, String localFilename, String remoteFilename,TFTPClient tftp) {boolean closed;FileInputStream input = null;// Try to open local file for readingtry{input = new FileInputStream(localFilename);}catch (IOException e){tftp.close();System.err.println("Error: could not open local file for reading.");System.err.println(e.getMessage());System.exit(1);}open(tftp);// Try to send local file via TFTPtry{String [] parts = hostname.split(":");if (parts.length == 2) {tftp.sendFile(remoteFilename, transferMode, input, parts[0], Integer.parseInt(parts[1]));} else {tftp.sendFile(remoteFilename, transferMode, input, hostname);}}catch (UnknownHostException e){System.err.println("Error: could not resolve hostname.");System.err.println(e.getMessage());System.exit(1);}catch (IOException e){System.err.println("Error: I/O exception occurred while sending file.");System.err.println(e.getMessage());System.exit(1);}finally{// Close local socket and input fileclosed = close(tftp, input);}return closed;}privatestaticbooleanreceive(int transferMode, String hostname, String localFilename, String remoteFilename,TFTPClient tftp) {boolean closed;FileOutputStream output = null;File file;file = new File(localFilename);// If file exists, don't overwrite it.if (file.exists()){System.err.println("Error: " + localFilename + " already exists.");System.exit(1);}// Try to open local file for writingtry{output = new FileOutputStream(file);}catch (IOException e){tftp.close();System.err.println("Error: could not open local file for writing.");System.err.println(e.getMessage());System.exit(1);}open(tftp);// Try to receive remote file via TFTPtry{String [] parts = hostname.split(":");if (parts.length == 2) {tftp.receiveFile(remoteFilename, transferMode, output, parts[0], Integer.parseInt(parts[1]));} else {tftp.receiveFile(remoteFilename, transferMode, output, hostname);}}catch (UnknownHostException e){System.err.println("Error: could not resolve hostname.");System.err.println(e.getMessage());System.exit(1);}catch (IOException e){System.err.println("Error: I/O exception occurred while receiving file.");System.err.println(e.getMessage());System.exit(1);}finally{// Close local socket and output fileclosed = close(tftp, output);}return closed;}privatestaticbooleanclose(TFTPClient tftp, Closeable output) {boolean closed;tftp.close();try{if (output != null) {output.close();}closed = true;}catch (IOException e){closed = false;System.err.println("Error: error closing file.");System.err.println(e.getMessage());}return closed;}privatestaticvoidopen(TFTPClient tftp) {try{tftp.open();}catch (SocketException e){System.err.println("Error: could not open local UDP socket.");System.err.println(e.getMessage());System.exit(1);}}}