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);        }    }}
原创粉丝点击