How to use the ip and port returned from a STUN server

978 views Asked by At

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();
            }
        });
0

There are 0 answers