在Android上发送Telnet命令

时间:2014-10-27 19:57:04

标签: java android sockets telnet

我正在尝试通过telnet发送一些命令。 在基于swing GUI的PC上本地执行时,一切正常,但在Android上使用相同的代码时,应用程序冻结几秒钟并且命令不会执行(模拟器和真实设备上的结果相同),尽管它运行到最后。没有抛出错误消息,但是在此过程之后,无法通过Telnet访问设备,甚至不能通过PC访问(因此需要重新启动目标设备)。我不明白的是,似乎我在Android上忽略了我设置的500毫秒限制。

我是Android的新手所以请耐心等待。很高兴得到任何帮助。

package com.test.test.remote;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        setContentView(R.layout.activity_main);
    }

    public void calculate(View view) throws Exception {

        System.out.println("Calc button executed");
        command("MO");
    }

    public String command(String cmd) {
        String output = "test";

        Log.e("Msg", "command reached");
        Log.e("Msg", cmd);

        Socket pingSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        pingSocket = new Socket();
        try {
            pingSocket.connect(new InetSocketAddress("10.10.10.107", 23), 500);
            out = new PrintWriter(pingSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                    pingSocket.getInputStream()));
            out.println(cmd);
            output = in.readLine();
            out.close();
            in.close();
            pingSocket.close();
            System.out.println(output);
            Log.e("Msg", "end of command reached");
        } catch (Exception e) {
            Log.e("com.example.test.MainActivity", e.getMessage());
            e.printStackTrace();
        }

        return output;
    }
}

XML:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="31dp"
        android:ems="10"
        android:inputType="numberDecimal|numberSigned" >

        <requestFocus />
    </EditText>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="28dp" >

    <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/kmh" />

    </RadioGroup>

    <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/radioGroup1"
            android:layout_centerVertical="true"
            android:onClick="calculate"
            android:text="@string/calc" />

</RelativeLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.test.remote"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="21" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

0 个答案:

没有答案