连接被拒绝:将java客户端连接到Android服务器(仿真器)时

连接被拒绝:将java客户端连接到Android服务器(仿真器)时,java,android,sockets,Java,Android,Sockets,我几乎什么都试过了 使用ADB转发TCP:12345 TCP:12345从ADB外壳转发 使用10.0.2.2(不转发)侦听我的主机 在清单中设置INTERNET权限 将线程策略设置为允许所有函数 我在12345端口运行我的客户机java程序,我在Android程序中有一个ServerSocket,它通过同一端口监听。但是,当我运行我的客户机(在模拟器上运行服务器程序之后)并输入要传输的字符串时,我会得到一个异常,即“连接被拒绝” java.net.ConnectException: C

我几乎什么都试过了

  • 使用ADB转发TCP:12345 TCP:12345从ADB外壳转发
  • 使用10.0.2.2(不转发)侦听我的主机
  • 在清单中设置INTERNET权限
  • 将线程策略设置为允许所有函数
  • 我在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);
            }
        }
    }
    

    有没有可能Windows防火墙(或同等防火墙)会阻止连接?没有,我已经关闭了防火墙,而且它可以在两个java程序之间工作。。。