0

I am new to WebRTC Android, by using jStun with code below, I am able to get Public IP and Port of my android Devices.

    new Thread() {
        @Override
        public void run() {
            super.run();
            try {
                MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
                ChangeRequest changeRequest = new ChangeRequest();
                sendMH.addMessageAttribute(changeRequest);
                byte[] data = sendMH.getBytes();
                s = new DatagramSocket(4500);
                s.setReuseAddress(true);

                DatagramPacket p = new DatagramPacket(data, data.length, InetAddress.getByName("stun.l.google.com"), 19302);
                s.send(p);

                DatagramPacket rp;
                rp = new DatagramPacket(new byte[32], 32);
                //Recieve Packet from Stun Server
                s.receive(rp);

                MessageHeader receiveMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingResponse);
                receiveMH.parseAttributes(rp.getData());
                final MappedAddress ma = (MappedAddress) receiveMH
                        .getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);

                //Show Public IP and Port On Text View
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView myIp = findViewById(R.id.listeningIp);
                        TextView myPort = findViewById(R.id.listeningPort);
                        myIp.setText(ma.getAddress().toString());
                        myPort.setText(ma.getPort() + "");
                    }
                });


                while (true) {
                    byte[] b = new byte[1024];
                    DatagramPacket rr = new DatagramPacket(b, 0, b.length);
                    //This Recieve Aimed to Recieve from Laptop Packet Sender
                    s.receive(rr);
                    final String sss = new String(rr.getData());
                    Log.e("recieved", sss);

                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }.start();

Public IP and Port of Android Device: 39.41.148.77:4500

But when I send A Packet From Laptop Packet Sender Software to Android(39.41.148.77:4400) It didn't get Received.

Any Guide is appreciated. Thanks

enter image description here

Jawad Zeb
  • 523
  • 4
  • 18
  • How do you know it didn't get received by B ? What is the code B is using to answer ? Are you sure A sent the packet and B received it (via Wireshark for example) ? As it's UDP it can be lost without you knowing. – Sami Tahri May 25 '19 at 21:27
  • @SamiTahri Yes I am Sure that It send Packet to B at same IP and Port. Because I even tried with third party softwares to recursively send packet to Ip:Port but Didn't Recieved At Any of the Device A,B – Jawad Zeb May 25 '19 at 21:30
  • Might be I am wrong In the approach I am Telling you he Scenerio. When I Get Public IP and port of both Device. I just tried to Send Packets to both devices using third Party Tool but didn't Recieved at any of the device. – Jawad Zeb May 25 '19 at 21:32
  • When I know the ip and port of both devices. I tried to send packet with https://packetsender.com/ from Laptop. to both one by one but didn't received at any of them. Device A and B are Android Devices – Jawad Zeb May 25 '19 at 21:34
  • What do you mean by "received" ? – Sami Tahri May 25 '19 at 21:34
  • s.receive(pkt); which is supposed to Receives Packet and show in Toast this Code Dose not Executes and get Stuck at this line. while debugging – Jawad Zeb May 25 '19 at 21:37
  • Sorry for my mistake. I send a packet through Third Device(My Laptop) to A/B. but didn't receive packet on any device A/B – Jawad Zeb May 25 '19 at 21:39
  • Yes, but what I'm saying is how do you know it actually reaches the devices ? Use a sniffer to check that it is ok, or UDPSocket directly. – Sami Tahri May 25 '19 at 21:59
  • @SamiTahri Now I installed Packet Capture on Android but still didn't . Can you please help me in the Discussion Chat. This Research is for My Graduation Project. I appreciate your help – Jawad Zeb May 25 '19 at 22:16

1 Answers1

0

Compleing a STUN binding request completed does not suddenly enable your port to receive traffic from anything other than the public STUN server you just exchanged packets. To fully open the port, both endpoints needs to send packets (multiple times, at the same time) from the same local port to the known ip:port pairs of each other.

Everything you need to know is here:

Programming P2P application

selbie
  • 100,020
  • 15
  • 103
  • 173