2012-11-05 82 views
1

我试图保持在23000监听UDP端口,应用程序在当地环境运行良好。 但切换到3G没有机会,流量被阻止。Android UDP入站流量

我试图所述载波信道的相同的端口上发送数据的4字节接收之前“开口”:

   InetAddress serverAddr = InetAddress.getByName(SOULISSIP); 
       DatagramChannel channel = DatagramChannel.open(); 
       socket = channel.socket(); 

       //socket = new DatagramSocket(); 
       socket.setReuseAddress(true); 

       //InetSocketAddress ia = new InetSocketAddress("localhost", SERVERPORT); 
       InetSocketAddress sa = new InetSocketAddress(SERVERPORT); 
       socket.bind(sa); 
       DatagramPacket holepunh = new DatagramPacket(new byte[]{0,1,2,3},4, serverAddr, SERVERPORT); 
       socket.send(holepunh); 

       // create a buffer to copy packet contents into 
       byte[] buf = new byte[200]; 
       // create a packet to receive 


       DatagramPacket packet = new DatagramPacket(buf, buf.length); 

       Log.d("UDP", "***Waiting on packet!"); 
       socket.setSoTimeout((int) opzioni.getDataServiceInterval()); 
       // wait to receive the packet 
       socket.receive(packet); 
       UDPSoulissDecoder.decodevNet(packet); 
       socket.close(); 

是有简单的方法来打开通道和接收UDP数据报上端口23000?

+0

究竟如何可能一个数据报被发送到Android设备,考虑到几乎所有的细胞提供者隐藏手机NAT网关后面,从而使设备从外界无法访问? –

+0

所以我想其他应用程序,如Skype和GTalk使用所有第三个主机打洞? – Shine

+0

是的。该设备将保持一个tcp连接对服务器开放。例如这就是推送电子邮件如何在电话上工作 - 有一个活动的tcp连接持有开放的客户端 - >服务器,所以服务器可以发送推送通知。 –

回答

0

所以,答案是在请求中,而我正在努力接收套接字。我不得不绑定发送方插口到本地端口用于接收数据报

  //send     
      serverAddr = InetAddress.getByName(SOULISSIP); 

      DatagramChannel channel = DatagramChannel.open(); 
      sender = channel.socket(); 
      sender.setReuseAddress(true); 

      //amateur hole punch 
      InetSocketAddress sa = new InetSocketAddress(SERVERPORT); 
      sender.bind(sa); 
      //stuff to send 
      List<Byte> macaco = new ArrayList<Byte>(); 
      macaco = Arrays.asList(pingpayload); 
      ArrayList<Byte> buf = buildVNetFrame(macaco, prefs); 
        //someone help me here... 
      byte[] merd = new byte[buf.size()]; 
      for (int i = 0; i < buf.size(); i++) { 
       merd[i] = (byte) buf.get(i); 
      } 
      packet = new DatagramPacket(merd, merd.length, serverAddr, SOULISSPORT); 
      sender.send(packet); 

接收器和发送器是在单独的线程,使用相同的本地端口。 .setReuseAddress(true)允许这样做。