08-29-2022 10:35 PM
我正在尝试编写一个安卓应用程序作为远程登录客户端思科交换机(2960)使用apache.commons.net.telnet。但是我在连接方面有问题。在安卓模拟器上没有显示输出,但是ConnectBot在同一个模拟器上运行,并且可以很好地远程登录到交换机。以下是代码示例:
MainActivity.java
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);
}
}
activity_main.xml
<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>
LogCat
06-10 00:35:52.976: E/Trace(1938): error opening trace file: No such file or directory (2)
06-10 00:35:53.716: D/libEGL(1938): loaded /system/lib/egl/libEGL_emulation.so
06-10 00:35:53.716: D/libEGL(1938): loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-10 00:35:53.716: D/libEGL(1938): loaded /system/lib/egl/libGLESv2_emulation.so
06-10 00:35:53.726: D/(1938): HostConnection::get() New Host Connection established 0xb8c6d868, tid 1938
06-10 00:35:53.887: W/EGL_emulation(1938): eglSurfaceAttrib not implemented
06-10 00:35:53.906: D/OpenGLRenderer(1938): Enabling debug mode 0
06-10 00:36:26.367: W/EGL_emulation(1938): eglSurfaceAttrib not implemented
06-10 00:36:39.556: D/AndroidRuntime(1938): Shutting down VM
06-10 00:36:39.556: W/dalvikvm(1938): threadid=1: thread exiting with uncaught exception (group=0xb3df9288)
06-10 00:36:39.616: E/AndroidRuntime(1938): FATAL EXCEPTION: main
06-10 00:36:39.616: E/AndroidRuntime(1938): android.os.NetworkOnMainThreadException
06-10 00:36:39.616: E/AndroidRuntime(1938): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-10 00:36:39.616: E/AndroidRuntime(1938): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-10 00:36:39.616: E/AndroidRuntime(1938): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-10 00:36:39.616: E/AndroidRuntime(1938): at libcore.io.IoBridge.connect(IoBridge.java:112)
06-10 00:36:39.616: E/AndroidRuntime(1938): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-10 00:36:39.616: E/AndroidRuntime(1938): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-10 00:36:39.616: E/AndroidRuntime(1938): at java.net.Socket.connect(Socket.java:842)
06-10 00:36:39.616: E/AndroidRuntime(1938): at org.apache.commons.net.SocketClient.connect(SocketClient.java:182)
06-10 00:36:39.616: E/AndroidRuntime(1938): at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
06-10 00:36:39.616: E/AndroidRuntime(1938): at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)
06-10 00:36:39.616: E/AndroidRuntime(1938): at com.example.android2switch.MainActivity$1.onClick(MainActivity.java:45)
06-10 00:36:39.616: E/AndroidRuntime(1938): at android.view.View.performClick(View.java:4084)
06-10 00:36:39.616: E/AndroidRuntime(1938): at android.view.View$PerformClick.run(View.java:16966)
06-10 00:36:39.616: E/AndroidRuntime(1938): at android.os.Handler.handleCallback(Handler.java:615)
06-10 00:36:39.616: E/AndroidRuntime(1938): at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 00:36:39.616: E/AndroidRuntime(1938): at android.os.Looper.loop(Looper.java:137)
06-10 00:36:39.616: E/AndroidRuntime(1938): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-10 00:36:39.616: E/AndroidRuntime(1938): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 00:36:39.616: E/AndroidRuntime(1938): at java.lang.reflect.Method.invoke(Method.java:511)
06-10 00:36:39.616: E/AndroidRuntime(1938): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-10 00:36:39.616: E/AndroidRuntime(1938): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-10 00:36:39.616: E/AndroidRuntime(1938): at dalvik.system.NativeStart.main(Native Method)
我对Android开发完全陌生(Eclipse,露娜),我找不到任何用于思科交换机/路由器的基于android的telnet客户端的参考资料/示例。任何参考,示例代码或指南将非常感谢,谢谢!
Solved! Go to Solution.
08-29-2022 10:36 PM
您是否向清单文件添加了正确的权限?确保您添加了允许应用程序打开网络套接字的权限。
"<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)'
08-29-2022 10:36 PM
您是否向清单文件添加了正确的权限?确保您添加了允许应用程序打开网络套接字的权限。
"<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)'
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide