我正在尝试将侦听UDP端口保留在23000上,应用程序在本地环境中运行良好。 但切换到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数据报?
答案 0 :(得分: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)
允许这样做。