在 Android 应用程序中建立 Java 服务器和客户端之间的连接编辑
我在 android 中有一个应用程序,在其中创建了一个 android 客户端和一个 Java 服务器。
但我面临以下问题:我的客户端(android 部分)使用 android 环回地址连接到端口 6000 上的本地计算机。
我的服务器(Java 中)在本地计算机的端口 6000 上侦听 - 但我必须使用什么 IP 来获取接受客户端的套接字?
InetSocketAddress serverAddr = new InetSocketAddress(SERVERIP,serverPort);
serverSocket = new ServerSocket();
serverSocket.bind(serverAddr);
那么我必须使用的 SERVERIP 是什么?
更新:我的客户端在模拟器上运行!!!!
编辑:
公共类 ClientThread 实现 Runnable { 对象同步令牌;
public ClientThread(Object syncToken) {
this.syncToken = syncToken;
}
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, 50458);
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
} catch (IOException e) {
System.err
.println("Couldn't get I/O for the connection to host");
}
try {
out = new PrintStream(socket.getOutputStream());
} catch (IOException e) {
System.out.println(e);
}
while (true) {
synchronized (syncToken) {
try {
syncToken.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
这里是: private String serverIpAddress = "10.0.2.2";!!!!!
I have an app in android in which I created an android client and a Java sever.
But I'm confronting the following issue: my client (the android part) connects to the local machine on port 6000 using the android loopback address.
My server (in Java) listens on local machine at the port 6000 - but what is the IP I have to use to get the socket that accepts the clients?
InetSocketAddress serverAddr = new InetSocketAddress(SERVERIP,serverPort);
serverSocket = new ServerSocket();
serverSocket.bind(serverAddr);
So what is the SERVERIP I have to use?
UPDATE:My client runns on an emulator!!!!!
EDIT:
public class ClientThread implements Runnable {
Object syncToken;
public ClientThread(Object syncToken) {
this.syncToken = syncToken;
}
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, 50458);
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
} catch (IOException e) {
System.err
.println("Couldn't get I/O for the connection to host");
}
try {
out = new PrintStream(socket.getOutputStream());
} catch (IOException e) {
System.out.println(e);
}
while (true) {
synchronized (syncToken) {
try {
syncToken.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
And here is: private String serverIpAddress = "10.0.2.2";!!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 http://developer.android.com/guide/developing/devices/ emulator.html#emulatornetworking:如果您想从模拟器内部与本地主机进行通信,请在本地主机上使用 IP
127.0.0.1
并使用 IP10.0.2.2< /code> 在 Android 中。这应该可以让您在 Android 客户端和本地主机服务器之间进行通信。
From http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking: if you want to communicate from within the emulator to the local host, use IP
127.0.0.1
on the local host and use IP10.0.2.2
in Android. This should let you communicate between the Android client and the local host server.你想在Android上运行服务器部分吗?我想不会,在这种情况下使用环回地址实际上是行不通的,因为Android系统上的环回接口会环回到Android机器本身,它不会路由到外部。
You want to run the server part on the Android? I guess not, and in such case using loopback address is not really going to work, as loopback interface on the Android system loops back to the Android machine itself, it is not routed to the outside.
对于serverAddr,使用
#InetSocketAddress(int port)
构造函数,它指定通配符地址和特定端口,这意味着它监听机器的所有接口。编辑:为了获得最佳结果,在 Android 设备上使用服务器的 DNS 名称来连接它。
For the serverAddr, use the
#InetSocketAddress(int port)
constructor, it specifies the wildcard address and a specific port, meaning it listens on all the interfaces of the machine.Edit: For best results, on the android device use the DNS name of the server to connect to it.