使用Android java应用程序的Cisco交换机的Telnet客户端

时间:2015-06-09 15:16:44

标签: java android telnet cisco eclipse-luna

我正在尝试使用apache.commons.net.telnet将Android应用程序编写为cisco switch(2960)的telnet客户端。但我有连接问题。 Android模拟器上没有显示输出,但ConnectBot在同一个模拟器上运行,可以完美地telnet到交换机。这是代码示例:

MainActivity.java

SELECT *
FROM mdl_config_plugins
WHERE plugin LIKE '%pagenew%';

activity_main.xml中

package com.example.android2switch;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;

import org.apache.commons.net.telnet.EchoOptionHandler;
import org.apache.commons.net.telnet.InvalidTelnetOptionException;
import org.apache.commons.net.telnet.SuppressGAOptionHandler;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TelnetOptionHandler;
import org.apache.commons.net.telnet.TerminalTypeOptionHandler;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;

public class MainActivity extends Activity {

    Button button;
    TextView textview;
    EditText edit1;
    Editable server;
    static TelnetClient telnetClient = new TelnetClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    setOptionHandlers();
                    //telnetClient.connect("192.168.0.1");
                    edit1 = (EditText)findViewById(R.id.edit1);
                    server = edit1.getEditableText();
                    telnetClient.connect(server.toString());

                    read();
                    telnetClient.disconnect();
                } catch (SocketException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
              }});      
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private static void setOptionHandlers() throws IOException {
        ArrayList<TelnetOptionHandler> optionHandlers = new ArrayList<TelnetOptionHandler>();
        optionHandlers.add(new TerminalTypeOptionHandler("VT100", false, false, true, false));
        optionHandlers.add(new EchoOptionHandler(true, false, true, false));
        optionHandlers.add(new SuppressGAOptionHandler(true, true, true, true));
        for (TelnetOptionHandler handler : optionHandlers) {
            try {
                telnetClient.addOptionHandler(handler);
            }
            catch (InvalidTelnetOptionException e) {
                System.err.println("Error registering option handler " + handler.getClass().getSimpleName());
            }
        }
    }

    public static void write(byte[] data) throws IOException {
        telnetClient.getOutputStream().write(data);
        telnetClient.getOutputStream().flush();
    }

    public void read() throws IOException {

        TextView tv1=(TextView)findViewById(R.id.textView1);
        tv1.setText("Read");
        byte[] buff = new byte[1024];
        int read;
        if((read = telnetClient.getInputStream().read(buff)) > 0) {
            tv1.append(new String(buff));
        }
        tv1.append("read="+read);
    }

}

logcat的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
tools:context="com.example.android2switch.MainActivity" >

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/edit1"
android:hint="Command"
android:text="ip address" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:text="Run Program"
android:id="@+id/button1" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
 />

</LinearLayout>
</ScrollView>
</LinearLayout>

我完全不熟悉Android开发(Eclipse Luna),我找不到任何针对cisco开关/路由器的android base telnet客户端的参考/示例。 非常感谢任何参考,示例代码或指南,谢谢!

1 个答案:

答案 0 :(得分:1)

您是否为清单文件添加了正确的权限? 确保已添加允许应用程序打开网络套接字的权限。

"<uses-permission android:name="android.permission.INTERNET"/>

详情请见此处: http://developer.android.com/reference/android/Manifest.permission.html

此外,此处解决了类似的问题:Error message 'java.net.SocketException: socket failed: EACCES (Permission denied)'