如何使用从STUN服务器返回的ip和端口

时间:2018-06-16 12:21:33

标签: android xmpp stun turn jingle

希望你过得愉快。即时通讯使用XMPP和Jingle构建视频通话应用程序,并将ByteStream从手机直接发送到手机。为了做到这一点,我注意到我需要一个昏迷的服务器来获取Android设备的公共IP和端口,并将它们发送到另一方,我得到像这样的公共IP

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

然后我将结果发送给另一方并使用给定端口在此客户端上打开SocketServer,但仍然无法通过Internet连接到此ServerSocket,或者我可能做错了什么?你能帮我解决一下我应该如何使用STUN或TURN的结果?感谢

以下是Jingle的连接方,以防万一

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 个答案:

没有答案