通过IP到达一个网络设备,并使用Android模拟器口 [英] Reaching a network device by IP and port using the Android emulator

查看:306
本文介绍了通过IP到达一个网络设备,并使用Android模拟器口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的Java应用程序为Android无法连接到服务器?

我运行在轻钢结构Android模拟器,并在我的电脑服务器至极端口9999和主机127.0.0.1,但就是无法连接,我认为这种方法不利于Android应用程序。

更新:我用API 8 ......工作了Android 2.2

这是工作的好! !谢谢大家;)

这是我的源$ C ​​$ C:

 进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
// Java的进口
//进口android.util.Log;
进口java.io. *;
进口java.net *。
导入的java.util。*;
进口java.util.logging.Level中;
进口java.util.logging.Logger中;


公共类MainActivity延伸活动{
// Variaveis接口
私人按钮ligar;
私人按钮投递;
私人的EditText文本1;
私人TextView的文本2;
// Variaveis
静态插座CSocket类;
静态PrintWriter的输出;
静态的BufferedReader的;


/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    //Declaraçãobutões
    ligar =(按钮)findViewById(R.id.ligar);
    投递=(按钮)findViewById(R.id.enviar);
    文本1 =(EditText上)findViewById(R.id.text1);
    文本2 =(TextView中)findViewById(R.id.text2);

    // Interacao
    ligar.setOnClickListener(新OnClickListener(){
        公共无效的onClick(查看为arg0){
             连接();
        }
    });
    enviar.setOnClickListener(新OnClickListener(){
        公共无效的onClick(查看为arg0){
               通过out.println(你好);
               text2.setText();
        }
    });
  }
  // Outras Funcoes

公共无效连接(){
// Funcao ligar
CSocket类= NULL;
OUT = NULL;
在= NULL;

尝试{
   CSocket类=新的Socket(10.0.2.2,4444);
   OUT =的新PrintWriter(cSocket.getOutputStream(),TRUE);
   在=新的BufferedReader(新的InputStreamReader(cSocket.getInputStream()));
   text2.setText(Estas conectado融为一体Sucesso的。);
   }
   赶上(IOException异常前){
   //Logger.getLogger(client.class.getName()).log(Level.SEVERE,空,前);
   text2.setText(埃罗娜登录时间!);
   }
   }
//
}
 

解决方案

在这里看到

主机可以使用IP地址来达到 10.0.2.2 从仿真器。

*的编辑回答您的评论:

有关完整性和更好地理解我的回答,请阅读 Android模拟器文档

这些都是 IP地址从模拟器为达到:

  • 10.0.2.1 ,路由器/网关地址。
  • 10.0.2.2 ,特别别名您的主机回环接口(如127.0.0.1在开发机)
  • 10.0.2.3 ,第一个DNS服务器
  • 10.0.2.4 / 10.0.2.5 / 10.0.2.6 ,可选的第二,第三和第四的DNS服务器(如果有的话)
  • 10.0.2.15 ,在模拟设备自身的<​​strong>的网络/以太网接口
  • 127.0.0.1 ,在模拟设备自身的<​​strong>的 loopback接口

这是说,我们有:

  • 常见错误1:访问 127.0.0.1 从仿真器试图接近你的主机。使用 10.0.2.2 ,就像我说的。
  • 常见错误2:试图访问模拟器服务HostComputerIP:appServicePort 。它不会工作,因为你的主机本身(在Windows,Linux,OS等)没有运行在该端口的服务。您需要重定向端口仿真器的控制台以一个端口上的仿真 Android的实例本身(见2所示)。

常见的联网需求:

1模拟器应用程序客户端和本地计算机作为服务器

由于该仿真器NAT'd,我相信你可以直接连接到任何一台计算机的本地网络上。我的意思是,既然的虚拟的路由器访问到这两个网络,它应该能够处理的发送(即emulator->真正的LAN)连接就好了。

例:的我的网络(192.168.0.X)上,我可以连接的的模拟器我的真正的路由器( 192.168.0.254 )只是指出该仿真器的Web浏览器 http://192.168.0.254:port 。我用不同的服务就可以了(欢呼番茄!),我可以访问所有他们每个端口上。无需处理端口转发,如预期的。

按您的code外观上来看,我相信你需要:

  //我假设192.168.0.114是你的服务器,这是
//位于本地网络上运行的服务器应用程序
//在端口9999。
CSocket类=新的Socket(192.168.0.114,9999);
 

2 - 本地计算机作为客户端和模拟器应用为服务器

现在这是一个不同的故事。您需要安装端口重定向中的虚拟的路由器上。最简单的方法是:

远程登录到管理制度(这不是仿真器),从您的主机(计算机,Linux或命令提示符在Windows控制台):

 远程登录本地主机5554
 

在此之后,使用:

  ADB将TCP:将localPort TCP:emulatorPort
 

在此,您将能够对 emulatorPort 服务,你将能够从本地网络的计算机连接到它访问 hostComputerIP:将localPort

这是一路人(包括我在内)的使用,例如,SSHDroid内仿真器。

还有别的吗?

Why my java app for android not connect to server ?

I run the aplication in android emulator, and the server wich port 9999 and host 127.0.0.1 in my pc, but just not connect and i think this method isn't good for android app.

Update: I work with API 8 ... Android 2.2 !

It is work Good ! ! Thanks everybody ;)

It is my source code:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
//Java imports
//import android.util.Log;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MainActivity extends Activity{
//Variaveis Interface
private Button ligar;
private Button enviar;
private EditText text1;
private TextView text2;
//Variaveis
static Socket cSocket;
static PrintWriter out;
static BufferedReader in;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Declaração butões
    ligar = (Button) findViewById(R.id.ligar);
    enviar = (Button) findViewById(R.id.enviar);
    text1 = (EditText) findViewById(R.id.text1);
    text2 = (TextView) findViewById(R.id.text2);

    //Interacao
    ligar.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){
             connect();
        }
    });
    enviar.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){
               out.println("Hello");
               text2.setText("");
        }
    });
  }
  //Outras Funcoes

public void connect(){
//Funcao ligar
cSocket = null;
out = null;
in = null;

try{
   cSocket = new Socket("10.0.2.2",4444);
   out = new PrintWriter(cSocket.getOutputStream(), true);
   in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
   text2.setText("Estas conectado com sucesso.");
   }
   catch (IOException ex) {
   //Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex);
   text2.setText("Erro! Na conexão");
   }                
   }
//
}

解决方案

See here:

Host machine can be reached using IP address 10.0.2.2 from the emulator.

*edit, answer to your comment:

For completeness and to better understand my answer, read the Android Emulator documentation.

These are the IP addresses as reached from the emulator:

  • 10.0.2.1, Router/gateway address.
  • 10.0.2.2, Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
  • 10.0.2.3, First DNS server
  • 10.0.2.4 / 10.0.2.5 / 10.0.2.6, Optional second, third and fourth DNS server (if any)
  • 10.0.2.15, The emulated device's own network/ethernet interface
  • 127.0.0.1, The emulated device's own loopback interface

That said, we have:

  • Common mistake 1: accessing 127.0.0.1 from the emulator trying to reach your host machine. Use 10.0.2.2, as I said.
  • Common mistake 2: Trying to access an emulator service on HostComputerIP:appServicePort. It won't work since your host computer itself (Windows, Linux, OS etc.) is not running a service in that port. You need to redirect a port on the emulator console to a port on an emulated Android instance itself (see 2 below).

Common networking needs:

1- Emulator app as client and local computer as server

Because the emulator is NAT'd, I believe you can connect to any computer on your local network directly. I mean, since the virtual router has access to both networks, it should be able to handle outgoing (i.e., emulator->real lan) connections just fine.

Example: on my network (192.168.0.x), I can connect from the emulator to my real router (192.168.0.254) just pointing the emulator web browser to http://192.168.0.254:port. I use different services on it (hail to Tomato!), and I can access all of them on each port. No need to handle port forwarding, as expected.

By the looks of your code, I believe you need:

// I assume 192.168.0.114 is your server, which is
// located on your local network, running a server application
// on port 9999.
cSocket = new Socket("192.168.0.114",9999);

2- Local computer as client and emulator app as server

Now that's a different story. You need to setup port redirections on the virtual router. The easiest way is:

Telnet into the "management" system (this is not the emulator), from your host (your computer, console on linux or command prompt on Windows):

telnet localhost 5554

After that, use:

adb forward tcp:localPort tcp:emulatorPort

After this, you will be able to have a service on emulatorPort and you will be able to connect to it from computers in the local network by accessing hostComputerIP:localPort.

This is the way people (including me) use, for example, SSHDroid inside an emulator.

Anything else?

这篇关于通过IP到达一个网络设备,并使用Android模拟器口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆