简体   繁体   English

在Android appEDIT中建立Java服务器和客户端之间的连接

[英]Establishing a connection between Java server and client within Android appEDIT

I have an app in android in which I created an android client and a Java sever. 我有一个Android的应用程序,我在其中创建了一个Android客户端和一个Java服务器。

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. 但是我遇到了以下问题:我的客户端(android部分)使用android环回地址连接到端口6000上的本地机器。

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? 我的服务器(用Java)在端口6000上侦听本地机器 - 但是我必须使用什么IP来获得接受客户端的套接字?

InetSocketAddress serverAddr = new InetSocketAddress(SERVERIP,serverPort);
serverSocket = new ServerSocket();
serverSocket.bind(serverAddr);

So what is the SERVERIP I have to use? 那么我必须使用什么SERVERIP?

UPDATE:My client runns on an emulator!!!!! 更新:我的客户端在模拟器上运行!!!!!

EDIT: 编辑:

public class ClientThread implements Runnable { Object syncToken; 公共类ClientThread实现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";!!!!! 这里是:private String serverIpAddress =“10.0.2.2”; !!!!!

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 IP 10.0.2.2 in Android. 来自http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking :如果要从模拟器内部与本地主机进行通信,请在本地主机上使用IP 127.0.0.1并使用IP 10.0.2.2在Android中10.0.2.2 This should let you communicate between the Android client and the local host server. 这应该允许您在Android客户端和本地主机服务器之间进行通信。

You want to run the server part on the Android? 您想在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. 我猜不是,在这种情况下使用环回地址并不真正起作用,因为Android系统上的环回接口循环回到Android机器本身,它不会路由到外部。

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. 对于serverAddr,使用#InetSocketAddress(int port)构造函数,它指定通配符地址和特定端口,这意味着它侦听机器的所有接口。

Edit: For best results, on the android device use the DNS name of the server to connect to it. 编辑:为了获得最佳效果,在Android设备上使用服务器的DNS名称连接到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM