使用 Android 模拟器通过 IP 和端口访问网络设备
为什么我的 Android 版 Java 应用无法连接到服务器?
Why does my Java app for Android not connect to the server?
我在 Android 模拟器中运行该应用程序,服务器位于端口 9999 和主机 127.0.0.1 在我的电脑中,但它无法连接,我认为这种方法不适用于 Sndroid 应用程序.
I run the application in Android emulator, and the server which is on port 9999 and host 127.0.0.1 in my pc, but it will just not connect and I think this method isn't good for Sndroid app.
更新:我使用 API 8 和 Android 2.2
这是我的源代码:
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");
}
}
//
}
见这里:
可以使用模拟器的 IP 地址 10.0.2.2
访问主机.
Host machine can be reached using IP address 10.0.2.2
from the emulator.
**编辑,回复您的评论:*
**edit, answer to your comment:*
为了完整性和更好地理解我的回答,请阅读 Android 模拟器文档.
For completeness and to better understand my answer, read the Android Emulator documentation.
这些是从模拟器获得的 IP 地址:
These are the IP addresses as reached from the emulator:
-
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
,模拟设备自己的网络/以太网接口 -
127.0.0.1
,模拟设备自己的环回接口莉>
-
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
也就是说,我们有:
- 常见错误 1:从试图访问主机的模拟器访问
127.0.0.1
.正如我所说,使用10.0.2.2
. - 常见错误 2:尝试访问
HostComputerIP:appServicePort
上的模拟器服务.由于您的主机计算机本身(Windows、Linux、OS 等)没有在该端口中运行服务,因此它无法工作.您需要将模拟器控制台上的端口重定向到模拟Android 实例本身上的端口(请参阅下面的 2).
- Common mistake 1: accessing
127.0.0.1
from the emulator trying to reach your host machine. Use10.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).
常见的网络需求:
1- 模拟器应用程序作为客户端,本地计算机作为服务器
因为模拟器是NAT'd,我相信你可以直接连接到本地网络上的任何计算机.我的意思是,由于虚拟路由器可以访问两个网络,它应该能够很好地处理传出(即模拟器->真实局域网)连接.
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.
示例: 在我的网络 (192.168.0.x) 上,我可以将从模拟器连接到我的真实路由器(192.168.0.254
) 只需将模拟器网络浏览器指向 http://192.168.0.254:port
.我在它上面使用了不同的服务(向 Tomato 致敬!),我可以在每个 port
上访问所有这些服务.无需处理端口转发,如预期.
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.
根据您的代码,我相信您需要:
// 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- 本地计算机作为客户端,模拟器应用程序作为服务器
现在是另一回事了.您需要在虚拟机上设置端口重定向 路由器.最简单的方法是:
Now that's a different story. You need to setup port redirections on the virtual router. The easiest way is:
从您的主机(您的计算机、Linux 上的控制台或 Windows 上的命令提示符)Telnet 进入管理"系统(这不是模拟器):
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
之后,使用:
adb forward tcp:localPort tcp:emulatorPort
此后,您将能够在 emulatorPort
上获得服务,并且您将能够通过访问 hostComputerIP:localPort
从本地网络中的计算机连接到它.
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
.
这是人们(包括我)使用的方式,例如在模拟器中使用 SSHDroid.
This is the way people (including me) use, for example, SSHDroid inside an emulator.
还有什么吗?