0

我已经尝试了几乎所有的东西。

  1. 使用 adb forward TCP:12345 TCP:12345 从 ADB Shell 转发
  2. 使用 10.0.2.2(无转发)监听我的主机
  3. 在清单中设置 INTERNET 权限
  4. 设置线程策略以允许所有功能

我在 12345 端口上运行我的客户端 java 程序,并且我在 Android 程序中有一个 ServerSocket,它通过同一个端口进行侦听。但是当我运行我的客户端(在模拟器上运行服务器程序之后)并输入我想要传输的字符串时,我得到一个异常说'连接被拒绝'。

    java.net.ConnectException: Connection refused: connect

这是我的服务器程序:

public class MainActivity extends Activity {
    TextView tv;
    ServerSocket ss = null;
    String mClientMsg = "";
    Thread myCommsThread = null;

    public static final int SERVERPORT = 12345;
    protected static final int MSG_ID = 0x1337;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = new TextView(this);
        tv.setText("Nothing from client yet");
        setContentView(tv);
        this.myCommsThread = new Thread(new CommsThread());
        this.myCommsThread.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        try {
            // make sure you close the socket upon exiting
            ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Handler myUpdateHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_ID:
                // TextView tv = (TextView) findViewById(R.id.TextView01);
                tv.setText(mClientMsg);
                setContentView(tv);
                break;
            default:
                break;
            }
            super.handleMessage(msg);
        }
    };

    class CommsThread implements Runnable {
        public void run() {
            Socket s = null;
            try {
                ss = new ServerSocket(SERVERPORT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (!Thread.currentThread().isInterrupted()) {
                Message m = new Message();
                m.what = MSG_ID;
                try {
                    if (s == null)
                        s = ss.accept();
                    BufferedReader input = new BufferedReader(
                            new InputStreamReader(s.getInputStream()));
                    String st = null;
                    st = input.readLine();
                    mClientMsg = st;
                    myUpdateHandler.sendMessage(m);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
               }

我的Java客户端程序如下:

public class TCPSender {
    public static void main(String args[]) {
        try {
            DataInputStream dis = new DataInputStream(System.in);
            System.out.println("Enter the file name:");
            @SuppressWarnings("deprecation")
            String f = dis.readLine();
            File f1 = new File(f);
            FileReader fr = new FileReader(f1);

            Socket s = new Socket("127.0.0.1", 12345);
            PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
            pw.println(f);
            int c = 0;
            while ((c = fr.read()) != -1)
                pw.println(c);
            System.out.println("File content are sent....");
            fr.close();
            s.close();
        } catch (Exception e) {
            System.out.println("" + e);
        }
    }
}
4

0 回答 0