简体   繁体   English

客户端无法连接到服务器套接字-Android

[英]Client cannot connect to the server socket - Android

I am developing an Android app. 我正在开发一个Android应用程序。 There is a device as server. 有一个设备作为服务器。 Client devices can connect to the server device. 客户端设备可以连接到服务器设备。 I wanna do it via a local area network. 我想通过局域网来做到这一点。 I am not sure it is the best way but I started to do a socket based communication. 我不确定这是最好的方法,但是我开始进行基于套接字的通信。

Here is my server thread: 这是我的服务器线程:

@Override
public void run() {
    Socket socket = null;
    try {
        serverSocket = new ServerSocket(7777);
        while (!Thread.currentThread().isInterrupted()) {
            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And I have an other thread for communication... 我还有另一个话题可以交流...


Now, here is my client thread: 现在,这是我的客户端线程:

    public void run() {
        try {
            InetAddress serverAddress = InetAddress.getByName("10.0.2.2");
            socket = new Socket(serverAddress, 7777);
            writer = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(socket.getOutputStream())), true);
            writer.println("Hello!");
            writer.flush();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The problem is the connection will be timeout when clients try to connect to the server. 问题是客户端尝试连接到服务器时连接将超时。


EDIT: Permissions: 编辑:权限:

<uses-permission 
    android:name="android.permission.INTERNET"/>    
<uses-permission 
    android:name="android.permission.ACCESS_NETWORK_STATE"/>

Finally I solved it. 终于我解决了。 I stopped sucking with Emulator. 我停止使用Emulator进行吮吸。 I got 2 Android device. 我有2个Android设备。 I added the next code to the Server: 我向服务器添加了以下代码:

public static String getIPAddress() {
    try {
        List<NetworkInterface> interfaces = Collections.list(
                NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface networkInterface : interfaces) {
            List<InetAddress> addresses = Collections.list(
                    networkInterface.getInetAddresses());
            for (InetAddress inetAddress : addresses) {
                if (!inetAddress.isLoopbackAddress()) {
                    String sAddress = inetAddress.getHostAddress().toUpperCase();
                    if (InetAddressUtils.isIPv4Address(sAddress)) {
                        return sAddress;
                    } else {
                        int delim = sAddress.indexOf('%');
                        return delim < 0 ? sAddress : sAddress.substring(0,
                                delim);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

Now I can get the Server address what looks like: 192.168.1.X. 现在,我可以获取服务器地址,如下所示:192.168.1.X。 I put it to a TextView on the Server device and the Clients have to enter this ip to connect to the Server. 我将其放在服务器设备上的TextView中,客户端必须输入此ip才能连接到服务器。 It is enough for me, because I want to use it on the local network. 对我来说就足够了,因为我想在本地网络上使用它。

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

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