Android UDP Communication

后端 未结 3 1021
温柔的废话
温柔的废话 2020-12-16 04:54

I\'ve read many posts on this site on how to receive UDP packets in Android. However, none of this is working for me!

Some basics:

I am testing on my HTC Inc

3条回答
  •  感情败类
    2020-12-16 05:25

    I had a similar problem. On the Android was two sockets (sending/listening), and on a PC server was again two sockets (sending/listening). The phone would ping the PC's known listening socket with the address of the phone's unknown listening socket, so the PC could reply. Nothing I was doing appeared to be getting the address of the listening socket, as the socket would never receive anything.

    This solved my problem: Android: java.net.DatagramSocket.bind: Invalid Argument Exception. Use a channel to create the socket, then binding on null. Now I can use the sending socket on the phone to send a packet containing the port of the listening socket (the IPs are the same) to the PC, obtained with .getLocalPort() The PC reads the byte[], gets the port, and sends packets back to the phones listening port.

提交回复
热议问题