0

hope your having a nice day . im building a Video Call application using XMPP and Jingle and direct ByteStream from phone to phone . in order to do so i noticed i need a stun server to get public ip and port of android devices and send them to the other party , im getting the public ip like this

InetAddress address = Inet4Address.getByName("stun.l.google.com");


                MessageHeader sendMH = new 
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);

                ChangeRequest changeRequest = new ChangeRequest();
                sendMH.addMessageAttribute(changeRequest);

                byte[] data = sendMH.getBytes();


                DatagramSocket s = new DatagramSocket(null);
                localPort = s.getLocalPort();
                s.setReuseAddress(true);

               // s.bind(address);

                DatagramPacket p = new DatagramPacket(data, 
data.length,address,19302);
                s.send(p);



                DatagramPacket rp;

                rp = new DatagramPacket(new byte[32], 32);

                s.receive(rp);
                MessageHeader receiveMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingResponse);

                // System.out.println(receiveMH.getTransactionID().toString() + "Size:"
                // + receiveMH.getTransactionID().length);
                receiveMH.parseAttributes(rp.getData());
                MappedAddress ma = (MappedAddress) receiveMH
                        .getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
                ip = ma.getAddress().toString();
                port = ma.getPort();
                Log.i("XMPP-Stabler",ma.getAddress().toString()+"  "+ma.getPort());

                s.close();

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

then i send the result to the other party and open up a SocketServer on this client using given port , but still i can not connect to this ServerSocket over internet , or probably i am doing some thing wrong ? can you please help me how should i use STUN or TURN results ? thanks

and here is my connecting side of Jingle just in case

otherTransport = (JingleS5BTransport) 
jingle.getContents().get(0).getTransport();

    ArrayList<JingleContent> contents = new ArrayList<>();

    contents.add(content);

        session = (JingleS5BTransportSession) JingleS5BTransportManager.getInstanceFor(connection).transportSession(new JingleSession(connection.getUser(),responderFullId,role,sessionId,contents) {
            @Override
            public XMPPConnection getConnection() {
                return connection;
            }

            @Override
            public void onTransportMethodFailed(String namespace) {
                Log.i("XMPP-Stabler","transport method failed "+namespace);
            }
        });

    session.setTheirProposal(otherTransport);

        session.initiateOutgoingSession(new JingleTransportInitiationCallback() {
            @Override
            public void onSessionInitiated(BytestreamSession bytestreamSession) {
                Log.i("XMPP-Stabler","ON SESSION INITIATED 2!");
                Socks5BytestreamSession session = (Socks5BytestreamSession) bytestreamSession;
            }

            @Override
            public void onException(Exception e) {
                e.printStackTrace();
            }
        });
Reza
  • 321
  • 2
  • 4
  • 14
  • You are missing the hole punching step. Both endpoints of a P2P connection need to do STUN and do the hole-punching in order to establish a connection. https://stackoverflow.com/a/8524609/104458 – selbie Jun 19 '18 at 04:17
  • but hole punching doesnt work with symmertic NAT , which most of users are using , am i right? – Reza Jun 19 '18 at 04:59
  • Symmetric NAT is known to be low (<15%) for consumer home NATs in United States for residential ISPs. Mobile phone networks however, are often symmetric on IPv4, but might have better connectivity on IPv6. It's actually been a long time since I've sampled these numbers. – selbie Jun 19 '18 at 06:05
  • If you have symmetric NAT, you're going to need a relay (TURN) server unless you have something like port prediction or uPNP in place that works. You can use the STUN server to detect what type of NAT you are on. – selbie Jun 19 '18 at 06:05
  • ermm , i know uPNP is router dependant , so not reliable but port prediction ? how to do it ? is it reliable? – Reza Jun 19 '18 at 06:36
  • Hint: sometimes each router just assigns port numbers sequentially. Infer a candidate address if you detect this from the STUN behavior checks. – selbie Jun 19 '18 at 07:55
  • "some times" :)) – Reza Jun 19 '18 at 08:00
  • did you build this successfully? Can you help me with this question? https://stackoverflow.com/questions/62148742/smack-new-jingle – Stefano Mtangoo Jun 02 '20 at 21:02

0 Answers0