E
E
Ening_apps2018-11-17 19:13:37
Java
Ening_apps, 2018-11-17 19:13:37

How to establish a connection to the ServerSocket of the local computer from a mobile device?

Good day to all.
As part of my pet project, I needed to make a quick data exchange between the server and mobile devices. I have this client code:

public class TestRunnableClientTester implements Runnable {

    static Socket socket;    

    @Override
    public void run() {
        try {
            socket = new Socket("192.168.100.26", 4444);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try (
                DataOutputStream oos = new DataOutputStream(socket.getOutputStream());
                DataInputStream ois = new DataInputStream(socket.getInputStream())) {

            int i = 0;
            while (i < 1000) {
                oos.writeInt(i);

                oos.flush();

                int in = ois.readInt();
                System.out.println(in);
                i++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

On the line , if you put a breakpoint, then the program is not executed further. That is, this line is executed and everything that is very strange. But on the Huawei device, an error is thrown: java.net.ConnectException: failed to connect to /192.168.100.26 (port 4444): connect failed: EHOSTUNREACH (No route to host) The laptop and phones are connected to the same Wi-Fi, Internet access is everywhere. The firewall on the laptop is off. The most interesting thing is that a few days ago this code worked, but now it doesn’t. Already ran out of ideas what could be the matter. Thank you all in advance for your replies!socket = new Socket("192.168.100.26", 4444);


Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JayWay, 2018-11-17
@JayWay

Hello.
Where is this code being executed? Where is the server? If on another device, then the problem may lie in port forwarding on the router. Then, you need to specify the ip of the computer where the server is raised and the port that the server is listening on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question