日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

android 连接tftp 服务器

發布時間:2023/12/18 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 连接tftp 服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android 連接TFTP服務器
服務器端:搭建環境:w10 64位系統,服務器為tftp64:
Current Directory 為服務器的根目錄,存放要下載的文件。
Server interface 是服務器的端口
下面選擇Tftp Server 因為這里我們要做Tftp服務器。
Tftp 下載連接 http://tftpd32.jounin.net/
下載完成直接安裝就行

127.0.01 是本機的ip地址 端口口號 69 服務器就配置完成了。
在電腦端測試服務器是否完成

已經配置完成了,并且可以連接
下面開始手機客戶端代碼實現了
連接tftp 使用的是一個commons-net-3.6.jar 里面的 TFTPClient
jar 下載 http://commons.apache.org/proper/commons-net/download_net.cgi

TFTP服務器的端口是69 public final class TFTPExample {static final 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";public static void TftpUtils(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() {@Overrideprotected void trace(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");}private static boolean send(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;}private static boolean receive(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;}private static boolean close(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;}private static void open(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);}}}

總結

以上是生活随笔為你收集整理的android 连接tftp 服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。