Search code examples
javaandroidsocketstimeoutserversocket

Client cannot connect to the server socket - Android


I am developing an Android app. 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"/>

Solution

  • Finally I solved it. I stopped sucking with Emulator. I got 2 Android device. 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. I put it to a TextView on the Server device and the Clients have to enter this ip to connect to the Server. It is enough for me, because I want to use it on the local network.