java通过telnet发送固件

时间:2012-08-07 14:27:43

标签: java android networking telnet

你好我试图通过telnet将固件文件发送到调制解调器板。 这是我的代码:

    Socket s = null;
    try {
                SocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName(addr), 23);
                s = new Socket();
                s.connect(socketAddress, 1000);

        InputStream inputStream = s.getInputStream();
        OutputStream outputStream = s.getOutputStream();

        outputStream.write( (login + "\n") .getBytes());

        Thread.sleep(300);

        outputStream.write( (password + "\n") .getBytes());

        Thread.sleep(300);

                    outputStream.write(("swupdate" + "\n").getBytes());
                     Thread.sleep(300);                   

                      // Open the file that is the first
        // command line parameter
        FileInputStream fstream = new FileInputStream(path_frm_vdsl);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;
        // Read File Line By Line
        while ((line = br.readLine()) != null) {
            // Print the content on the console
            line = br.readLine();
            if (line == null) {
                Thread.sleep(1000);
            } else {
                //System.out.println(line);
                outputStream.write(line.getBytes());
                Log.v("---", line.getBytes() + "" + consumeInput(500, inputStream));
                //Log.v("Update_Modem","Updated " + consumeInput(500, inputStream));
                //outputStream.write(line.getBytes());
                Thread.sleep(10);
            }
        }

它只需登录发送swupdate命令并将固件文件转储到输出。在第一行输入后我有java.net.SocketException:断管 而我无法一次读取所有文件,没有足够的内存异常。 (3MB)

3 个答案:

答案 0 :(得分:2)

您知道telnet使用信道内信令吗?数据流包含转义命令指令。当您打开telnet连接时,会有大量的初始命令来回传递,因为服务器和客户端会确定彼此的功能和首选项。

你可能会发现你的字节流已经损坏,如果你只是拿出你在服务器上出现的任何东西。您需要遵守telnet协议,例如:正确理解你收到的字节流。

答案 1 :(得分:1)

Telnet protcol中的行终止符是\ r \ n。

但是为什么要将整个文件读入内存?为什么所有的睡眠?你为什么要跳过每一秒?什么是consumeInput()?

只需读取和写入字节。

答案 2 :(得分:0)

不确定您的接收端如何需要固件,纯二进制,十六进制编码,base64编码?

无论如何,这是你用普通二进制文件发送它的方式

Socket s = null;
try {
    SocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName(addr), 23);
    s = new Socket();
    s.connect(socketAddress, 1000);

    InputStream inputStream = s.getInputStream();
    OutputStream outputStream = s.getOutputStream();

    outputStream.write((login + "\n").getBytes());

    Thread.sleep(300);

    outputStream.write((password + "\n").getBytes());

    Thread.sleep(300);

    outputStream.write(("swupdate" + "\n").getBytes());
    Thread.sleep(300);

    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream(path_frm_vdsl);
    byte[] buffer = new byte[1024];
    int fillSize;

    // Read File chunk by chunk
    while ((fillSize = fstream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, fillSize);
    }
    outputStream.close();
} finally {
    s.close();
}

因此,以块为单位读取文件,直到没有更多数据(读取返回-1)并写入块(读取返回实际读取的数量)。