Android Telnet客户端发送

时间:2018-04-26 05:33:54

标签: java android sockets telnet

嗨,我大部分时间都只是启动android studio。我正在关注设置套接字的this教程,试图熟悉一些东西。我可以在教程中编译和运行应用程序,但我正在尝试将数据发送到的服务器是一个telnet服务器。服务器只是坐着等待特定的字符串来做特定的事情。如何修改此代码以将字符串发送到telnet服务器?请忽略代码中的任何无效缩进来粘贴它。

package com.javacodegeeks.android.androidsocketclient;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class Client extends Activity {

private Socket socket;

private static final int SERVERPORT = 23;
private static final String SERVER_IP = "10.0.0.50";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new Thread(new ClientThread()).start();
}

public void onClick(View view) {
    try {
        EditText et = (EditText) findViewById(R.id.EditText01);
        String str = et.getText().toString();
        PrintWriter out = new PrintWriter(new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream())),
                true);
        out.println(str);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

class ClientThread implements Runnable {

    @Override
    public void run() {

        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

            socket = new Socket(serverAddr, SERVERPORT);

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }

}

}

0 个答案:

没有答案