gpt4 book ai didi

Android UDP 入站流量

转载 作者:行者123 更新时间:2023-11-29 01:59:46 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

关于Android UDP 入站流量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13240938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com