0

Server part(Android)

new Thread(new Runnable() {
            public void run() {
                DatagramSocket s;

                try {
                    s = new DatagramSocket();
                    s.send(new DatagramPacket("aaa".getBytes(), 3, InetAddress.getByName(/* Server ip address*/), 11720));
                    s.close();
                } catch (UnknownHostException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }



                while(true)
                {
                try {
                    ServerSocket ssocket = new ServerSocket(11720);
                    ssocket.accept(); // Cannot make connection!!

                    Toast.makeText(a, "Who's coming", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
            }
        }).start();

Client code (Java, PC)

public static void main(String[] args) {
    try {
        DatagramSocket ds = new DatagramSocket(11720);
        DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);
        ds.close();

        try {
            ds.receive(dp);

            // ds.send(new DatagramPacket("aaaa".getBytes(), 4, dp.getAddress(), 11720));
            Socket socket = new Socket(dp.getAddress(), 11720); // Cannot make connection!!
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

I want to make connection between android server and pc client. First android send a packet to pc. I think server can know using this packet, so I use function getAddress().

Then android open socket using port number 11720 and pc try to connect android. But it cannot connect.

Why this code cannot make connection?


Oh, I just want to know how can I connect from pc to phone. So I change pc and android code.

Firstly, my goal is connect to android using static port(11720). But I cannot make connection using static port when using LTE.

So I check socket information using simple program.

This is in pc.

public static void main(String[] args) {
    try {
        DatagramSocket ds = new DatagramSocket(11720);
        DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);

        while (true)
        {
            ds.receive(dp);
            System.out.println(dp.getSocketAddress());
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

And this is in android.

new Thread(new Runnable() {
            public void run() {
                try {
                    DatagramSocket s = new DatagramSocket(11720);
                    while(true)
                    {
                        s.send(new DatagramPacket("aaa".getBytes(), 3, InetAddress.getByName("143.248.55.131" /* Server address */), 11720));
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();

I execute programs using WiFi and LTE. And result is

/143.248.56.118:11720 <- using WiFi
/143.248.56.118:11720
/143.248.56.118:11720
/117.111.7.124:35645 <- change LTE
/117.111.7.124:35645
/117.111.7.124:35645

When using LTE, socket's port number is changed. so I cannot send packet using static port number.

How can I solve this problem?

1 Answer 1

1

Firstly, why are you complicating by using Datagram Socket unnecessarily? You can simply use TCP without any problems.

Well, your client code(Java PC) might be waiting at:

ds.receive(dp);

And so the socket might not get created.

Although the android program is sending a packet but UDP being unreliable, the packet may not be received by the client code. Also, this can be due to network issues like both cannot get a connection, or there might be a NAT between them which would not let any UDP packet to let in.

So, I would suggest to simply use TCP. Don't complicate by first sending a UDP packet and then connecting with TCP.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.