0

我是 android OS 开发的新手。我尝试为 android OS 开发我的 telnet 客户端。现在,我只尝试连接 telnet 服务器,并接收登录消息。

但是当我启动套接字连接远程登录服务器时,我收到了奇怪的符号??????!????,而不是来自远程登录服务器的登录消息。

public class MainActivity extends Activity {
    protected static final int TCP_SERVER_PORT = 23;
    protected String inMsg, str;
    static Editable sentence;
    static String modifedSentence;
    BufferedReader inFromUser;
    Socket clientSocket = null;
    DataOutputStream outToServer=null;
    BufferedReader inFromServer=null;        

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button startButton = (Button)findViewById(R.id.startButton);
        final TextView textView = (TextView)findViewById(R.id.textView);

        startButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                new Thread(new Runnable() {                   

                    @Override
                    public void run() {             

       //Create socket
        try {
        clientSocket = new Socket("192.168.1.1",23);
       //Create out stream for ClientSocket
            outToServer = new DataOutputStream(clientSocket.getOutputStream());
       //Create ib stream for ClientSocket          
        inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
       //Receive login message from telnet server
            modifedSentence = inFromServer.readLine();
        } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
                 //Update UI activity with login message
         MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                          textView.setText(modifedSentence);
                    }
                });   
        }
             }).start();
    }   
        });         
    }

   protected void onDestroy()
    {
        try {
            clientSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
4

2 回答 2

0

Telnet 应用程序需要 telnet 协议才能连接到 telnet 服务器。这不是一个简单的 tcp 连接。我使用为 telnet 访问提供类的 apache commonse 库。谢谢

于 2013-03-29T11:34:40.657 回答
0

这些“奇怪”的符号??????!????意味着您使用了错误的编码。创建时,InputStreamReader您应该明确指出该流应该使用哪种编码。我不知道您的telnet服务中使用了哪种编码,但您可以尝试各种编码,例如windows-1252

new InputStreamReader(clientSocket.getInputStream(), Charset.forName("windows-1252"))

于 2013-03-01T11:07:50.050 回答