2

我试图在 23000 上保留一个监听 UDP 端口,该应用程序在本地环境中运行良好。但是切换到3G没有机会,流量被阻塞。

我尝试在接收之前“打开”在同一端口上发送 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();

有没有一种简单的方法可以打开通道并在端口 23000 上接收 UDP 数据报?

4

1 回答 1

1

所以,答案就在请求中,而我正在努力接收套接字。我必须将发送方套接字绑定到用于接收数据报的本地端口:

            //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)允许这样做。

于 2012-11-05T22:19:54.043 回答