0

I'm going to set a simple socket connection between two android devices using internet. There are two devices and two android app's , one of them is client app and another is server app. When the apps run on a device it's all OK, but when the apps run on two devices the client application doesn't connect to server application.

Client app:

btnSend.setOnClickListener(view -> {
        String msg = etMessage.getText().toString();
        AsyncTask.execute(() -> {
            try {
                DatagramSocket socket = new DatagramSocket();
                InetAddress ip = InetAddress.getByName("100.66.20.245");
                int port = 1020;
                DatagramPacket packet = new DatagramPacket(
                        msg.getBytes(), msg.length(), ip, port
                );
                socket.send(packet);
                runOnUiThread(() -> {
                    Toast.makeText(this, "Message sent", Toast.LENGTH_SHORT).show();
                });
            } catch (Exception e) {
                Log.w(TAG, e.toString());
            }
        });
});

Server app:

Runnable runnable = () -> {
        try {
            int port = 1020;
            DatagramSocket socket = new DatagramSocket(port);
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer,1024);
            runOnUiThread(() -> {
                Toast.makeText(this,"Waiting for client",Toast.LENGTH_SHORT).show();
            });
            socket.receive(packet);
            String msg = new String(packet.getData(),0,packet.getLength());
            runOnUiThread(() -> {
                Toast.makeText(this, "Client msg : " + msg, Toast.LENGTH_SHORT).show();
            });
        } catch (Exception e) {
            Log.w(TAG, e.toString());
        }
};
new Thread(runnable).start();

2 Answers 2

1

Are both devices connected to a network? And is the IP address of client application equal to the IP address of the server?

Sign up to request clarification or add additional context in comments.

15 Comments

No , the devices are not connect to a network and the ip address of client is not equal to the server
How then do you expect a socket to connect?
Then the client should use the public ip of the router. And on the router you should forward the used port to the local ip of your server device.
You can not connect the client and server in two different networks. Both the client and server must be in a network.
In this case, there will be no way to connect the server and the client without internet. Please connect the client and server to a shared network.
|
0

The best solution was the port forwarding with a router.
When you are using a router you can set an ip address to forwarding ports to that ip address. However if you have a better solution, please post a comment :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.