android 连接tftp 服务器
来源:互联网 发布:文档识别软件 编辑:程序博客网 时间:2024/06/06 18:19
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服务器的端口是69public 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() { @Override protected 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 seconds tftp.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 reading try { 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 TFTP try { 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 file closed = 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 writing try { 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 TFTP try { 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 file closed = 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); } }}
阅读全文
0 0
- android 连接tftp 服务器
- tftp 服务器
- Android连接远程服务器
- Android连接服务器Servlet
- Android连接服务器Servlet
- android http连接服务器
- Android中连接服务器
- Android连接Tomcat服务器
- TFTP 服务器安装设置
- TFTP服务器接收文件
- tftp服务器设置
- ubuntu tftp服务器配置
- tftp服务器建立
- TFTP服务器安装
- 安装TFTP服务器
- Fedora14架设tftp服务器
- tftp服务器搭建
- ubuntu10.04 tftp服务器
- 【jQuery】jQuery 简介
- linux中安装软件,查看、卸载已安装软件方法
- 13
- 单模模块和多模模块的区别
- Hbase通过命令将数据批量导入的方法
- android 连接tftp 服务器
- bzoj 4455: [Zjoi2016]小星星
- 【jQuery】jQuery 安装
- 《android framework常用api源码分析》之HandlerThread handler线程
- Linux core 文件介绍
- C语言结构体+数组实现学生学号成绩的统计、排序
- 解决Properties乱码
- 集合(Collection)
- TypeScript 和 JavaScript 的区别?